1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Phil Schwan <phil@clusterfs.com>
42 * Author: Andreas Dilger <adilger@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <lustre_dlm.h>
47 #include <lustre_lite.h>
48 #include <linux/pagemap.h>
49 #include <linux/file.h>
50 #include "llite_internal.h"
51 #include <lustre/ll_fiemap.h>
53 #include "cl_object.h"
55 struct ll_file_data *ll_file_data_get(void)
57 struct ll_file_data *fd;
59 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
63 static void ll_file_data_put(struct ll_file_data *fd)
66 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
69 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
70 struct lustre_handle *fh)
72 op_data->op_fid1 = ll_i2info(inode)->lli_fid;
73 op_data->op_attr.ia_mode = inode->i_mode;
74 op_data->op_attr.ia_atime = inode->i_atime;
75 op_data->op_attr.ia_mtime = inode->i_mtime;
76 op_data->op_attr.ia_ctime = inode->i_ctime;
77 op_data->op_attr.ia_size = i_size_read(inode);
78 op_data->op_attr_blocks = inode->i_blocks;
79 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
80 ll_inode_to_ext_flags(inode->i_flags);
81 op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
83 op_data->op_handle = *fh;
84 op_data->op_capa1 = ll_mdscapa_get(inode);
88 * Closes the IO epoch and packs all the attributes into @op_data for
91 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
92 struct obd_client_handle *och)
96 op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
97 ATTR_MTIME_SET | ATTR_CTIME_SET;
99 if (!(och->och_flags & FMODE_WRITE))
102 if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
103 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
105 ll_ioepoch_close(inode, op_data, &och, 0);
108 ll_pack_inode2opdata(inode, op_data, &och->och_fh);
109 ll_prep_md_op_data(op_data, inode, NULL, NULL,
110 0, 0, LUSTRE_OPC_ANY, NULL);
114 static int ll_close_inode_openhandle(struct obd_export *md_exp,
116 struct obd_client_handle *och)
118 struct obd_export *exp = ll_i2mdexp(inode);
119 struct md_op_data *op_data;
120 struct ptlrpc_request *req = NULL;
121 struct obd_device *obd = class_exp2obd(exp);
128 * XXX: in case of LMV, is this correct to access
131 CERROR("Invalid MDC connection handle "LPX64"\n",
132 ll_i2mdexp(inode)->exp_handle.h_cookie);
136 OBD_ALLOC_PTR(op_data);
138 GOTO(out, rc = -ENOMEM); // XXX We leak openhandle and request here.
140 ll_prepare_close(inode, op_data, och);
141 epoch_close = (op_data->op_flags & MF_EPOCH_CLOSE);
142 rc = md_close(md_exp, op_data, och->och_mod, &req);
144 /* This close must have the epoch closed. */
145 LASSERT(epoch_close);
146 /* MDS has instructed us to obtain Size-on-MDS attribute from
147 * OSTs and send setattr to back to MDS. */
148 rc = ll_som_update(inode, op_data);
150 CERROR("inode %lu mdc Size-on-MDS update failed: "
151 "rc = %d\n", inode->i_ino, rc);
155 CERROR("inode %lu mdc close failed: rc = %d\n",
158 ll_finish_md_op_data(op_data);
161 rc = ll_objects_destroy(req, inode);
163 CERROR("inode %lu ll_objects destroy: rc = %d\n",
170 if (exp_connect_som(exp) && !epoch_close &&
171 S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
172 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
174 md_clear_open_replay_data(md_exp, och);
175 /* Free @och if it is not waiting for DONE_WRITING. */
176 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
179 if (req) /* This is close request */
180 ptlrpc_req_finished(req);
184 int ll_md_real_close(struct inode *inode, int flags)
186 struct ll_inode_info *lli = ll_i2info(inode);
187 struct obd_client_handle **och_p;
188 struct obd_client_handle *och;
193 if (flags & FMODE_WRITE) {
194 och_p = &lli->lli_mds_write_och;
195 och_usecount = &lli->lli_open_fd_write_count;
196 } else if (flags & FMODE_EXEC) {
197 och_p = &lli->lli_mds_exec_och;
198 och_usecount = &lli->lli_open_fd_exec_count;
200 LASSERT(flags & FMODE_READ);
201 och_p = &lli->lli_mds_read_och;
202 och_usecount = &lli->lli_open_fd_read_count;
205 cfs_down(&lli->lli_och_sem);
206 if (*och_usecount) { /* There are still users of this handle, so
208 cfs_up(&lli->lli_och_sem);
213 cfs_up(&lli->lli_och_sem);
215 if (och) { /* There might be a race and somebody have freed this och
217 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
224 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
227 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
228 struct ll_inode_info *lli = ll_i2info(inode);
232 /* clear group lock, if present */
233 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
234 ll_put_grouplock(inode, file, fd->fd_grouplock.cg_gid);
236 /* Let's see if we have good enough OPEN lock on the file and if
237 we can skip talking to MDS */
238 if (file->f_dentry->d_inode) { /* Can this ever be false? */
240 int flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
241 struct lustre_handle lockh;
242 struct inode *inode = file->f_dentry->d_inode;
243 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
245 cfs_down(&lli->lli_och_sem);
246 if (fd->fd_omode & FMODE_WRITE) {
248 LASSERT(lli->lli_open_fd_write_count);
249 lli->lli_open_fd_write_count--;
250 } else if (fd->fd_omode & FMODE_EXEC) {
252 LASSERT(lli->lli_open_fd_exec_count);
253 lli->lli_open_fd_exec_count--;
256 LASSERT(lli->lli_open_fd_read_count);
257 lli->lli_open_fd_read_count--;
259 cfs_up(&lli->lli_och_sem);
261 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
262 LDLM_IBITS, &policy, lockmode,
264 rc = ll_md_real_close(file->f_dentry->d_inode,
268 CERROR("Releasing a file %p with negative dentry %p. Name %s",
269 file, file->f_dentry, file->f_dentry->d_name.name);
272 LUSTRE_FPRIVATE(file) = NULL;
273 ll_file_data_put(fd);
274 ll_capa_close(inode);
279 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm);
281 /* While this returns an error code, fput() the caller does not, so we need
282 * to make every effort to clean up all of our state here. Also, applications
283 * rarely check close errors and even if an error is returned they will not
284 * re-try the close call.
286 int ll_file_release(struct inode *inode, struct file *file)
288 struct ll_file_data *fd;
289 struct ll_sb_info *sbi = ll_i2sbi(inode);
290 struct ll_inode_info *lli = ll_i2info(inode);
291 struct lov_stripe_md *lsm = lli->lli_smd;
295 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
296 inode->i_generation, inode);
298 #ifdef CONFIG_FS_POSIX_ACL
299 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
300 inode == inode->i_sb->s_root->d_inode) {
301 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
304 if (unlikely(fd->fd_flags & LL_FILE_RMTACL)) {
305 fd->fd_flags &= ~LL_FILE_RMTACL;
306 rct_del(&sbi->ll_rct, cfs_curproc_pid());
307 et_search_free(&sbi->ll_et, cfs_curproc_pid());
312 if (inode->i_sb->s_root != file->f_dentry)
313 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
314 fd = LUSTRE_FPRIVATE(file);
317 /* The last ref on @file, maybe not the the owner pid of statahead.
318 * Different processes can open the same dir, "ll_opendir_key" means:
319 * it is me that should stop the statahead thread. */
320 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd &&
321 lli->lli_opendir_pid != 0)
322 ll_stop_statahead(inode, lli->lli_opendir_key);
324 if (inode->i_sb->s_root == file->f_dentry) {
325 LUSTRE_FPRIVATE(file) = NULL;
326 ll_file_data_put(fd);
330 if (!S_ISDIR(inode->i_mode)) {
332 lov_test_and_clear_async_rc(lsm);
333 lli->lli_async_rc = 0;
336 rc = ll_md_close(sbi->ll_md_exp, inode, file);
338 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
339 libcfs_debug_dumplog();
344 static int ll_intent_file_open(struct file *file, void *lmm,
345 int lmmsize, struct lookup_intent *itp)
347 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
348 struct dentry *parent = file->f_dentry->d_parent;
349 const char *name = file->f_dentry->d_name.name;
350 const int len = file->f_dentry->d_name.len;
351 struct md_op_data *op_data;
352 struct ptlrpc_request *req;
353 __u32 opc = LUSTRE_OPC_ANY;
360 /* Usually we come here only for NFSD, and we want open lock.
361 But we can also get here with pre 2.6.15 patchless kernels, and in
362 that case that lock is also ok */
363 /* We can also get here if there was cached open handle in revalidate_it
364 * but it disappeared while we were getting from there to ll_file_open.
365 * But this means this file was closed and immediatelly opened which
366 * makes a good candidate for using OPEN lock */
367 /* If lmmsize & lmm are not 0, we are just setting stripe info
368 * parameters. No need for the open lock */
369 if (lmm == NULL && lmmsize == 0) {
370 itp->it_flags |= MDS_OPEN_LOCK;
371 if (itp->it_flags & FMODE_WRITE)
372 opc = LUSTRE_OPC_CREATE;
375 op_data = ll_prep_md_op_data(NULL, parent->d_inode,
376 file->f_dentry->d_inode, name, len,
379 RETURN(PTR_ERR(op_data));
381 rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
382 0 /*unused */, &req, ll_md_blocking_ast, 0);
383 ll_finish_md_op_data(op_data);
385 /* reason for keep own exit path - don`t flood log
386 * with messages with -ESTALE errors.
388 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
389 it_open_error(DISP_OPEN_OPEN, itp))
391 ll_release_openhandle(file->f_dentry, itp);
395 if (it_disposition(itp, DISP_LOOKUP_NEG))
396 GOTO(out, rc = -ENOENT);
398 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
399 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
400 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
404 rc = ll_prep_inode(&file->f_dentry->d_inode, req, NULL);
405 if (!rc && itp->d.lustre.it_lock_mode)
406 ll_set_lock_data(sbi->ll_md_exp, file->f_dentry->d_inode,
410 ptlrpc_req_finished(itp->d.lustre.it_data);
411 it_clear_disposition(itp, DISP_ENQ_COMPLETE);
412 ll_intent_drop_lock(itp);
418 * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
419 * not believe attributes if a few ioepoch holders exist. Attributes for
420 * previous ioepoch if new one is opened are also skipped by MDS.
422 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
424 if (ioepoch && lli->lli_ioepoch != ioepoch) {
425 lli->lli_ioepoch = ioepoch;
426 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
427 ioepoch, PFID(&lli->lli_fid));
431 static int ll_och_fill(struct obd_export *md_exp, struct ll_inode_info *lli,
432 struct lookup_intent *it, struct obd_client_handle *och)
434 struct ptlrpc_request *req = it->d.lustre.it_data;
435 struct mdt_body *body;
439 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
440 LASSERT(body != NULL); /* reply already checked out */
442 memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
443 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
444 och->och_fid = lli->lli_fid;
445 och->och_flags = it->it_flags;
446 ll_ioepoch_open(lli, body->ioepoch);
448 return md_set_open_replay_data(md_exp, och, req);
451 int ll_local_open(struct file *file, struct lookup_intent *it,
452 struct ll_file_data *fd, struct obd_client_handle *och)
454 struct inode *inode = file->f_dentry->d_inode;
455 struct ll_inode_info *lli = ll_i2info(inode);
458 LASSERT(!LUSTRE_FPRIVATE(file));
463 struct ptlrpc_request *req = it->d.lustre.it_data;
464 struct mdt_body *body;
467 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, lli, it, och);
471 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
472 if ((it->it_flags & FMODE_WRITE) &&
473 (body->valid & OBD_MD_FLSIZE))
474 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
475 lli->lli_ioepoch, PFID(&lli->lli_fid));
478 LUSTRE_FPRIVATE(file) = fd;
479 ll_readahead_init(inode, &fd->fd_ras);
480 fd->fd_omode = it->it_flags;
484 /* Open a file, and (for the very first open) create objects on the OSTs at
485 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
486 * creation or open until ll_lov_setstripe() ioctl is called. We grab
487 * lli_open_sem to ensure no other process will create objects, send the
488 * stripe MD to the MDS, or try to destroy the objects if that fails.
490 * If we already have the stripe MD locally then we don't request it in
491 * md_open(), by passing a lmm_size = 0.
493 * It is up to the application to ensure no other processes open this file
494 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
495 * used. We might be able to avoid races of that sort by getting lli_open_sem
496 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
497 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
499 int ll_file_open(struct inode *inode, struct file *file)
501 struct ll_inode_info *lli = ll_i2info(inode);
502 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
503 .it_flags = file->f_flags };
504 struct lov_stripe_md *lsm;
505 struct obd_client_handle **och_p = NULL;
506 __u64 *och_usecount = NULL;
507 struct ll_file_data *fd;
508 int rc = 0, opendir_set = 0;
511 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
512 inode->i_generation, inode, file->f_flags);
514 it = file->private_data; /* XXX: compat macro */
515 file->private_data = NULL; /* prevent ll_local_open assertion */
517 fd = ll_file_data_get();
519 GOTO(out_och_free, rc = -ENOMEM);
522 if (S_ISDIR(inode->i_mode)) {
523 cfs_spin_lock(&lli->lli_sa_lock);
524 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL &&
525 lli->lli_opendir_pid == 0) {
526 lli->lli_opendir_key = fd;
527 lli->lli_opendir_pid = cfs_curproc_pid();
530 cfs_spin_unlock(&lli->lli_sa_lock);
533 if (inode->i_sb->s_root == file->f_dentry) {
534 LUSTRE_FPRIVATE(file) = fd;
538 if (!it || !it->d.lustre.it_disposition) {
539 /* Convert f_flags into access mode. We cannot use file->f_mode,
540 * because everything but O_ACCMODE mask was stripped from
542 if ((oit.it_flags + 1) & O_ACCMODE)
544 if (file->f_flags & O_TRUNC)
545 oit.it_flags |= FMODE_WRITE;
547 /* kernel only call f_op->open in dentry_open. filp_open calls
548 * dentry_open after call to open_namei that checks permissions.
549 * Only nfsd_open call dentry_open directly without checking
550 * permissions and because of that this code below is safe. */
551 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
552 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
554 /* We do not want O_EXCL here, presumably we opened the file
555 * already? XXX - NFS implications? */
556 oit.it_flags &= ~O_EXCL;
558 /* bug20584, if "it_flags" contains O_CREAT, the file will be
559 * created if necessary, then "IT_CREAT" should be set to keep
560 * consistent with it */
561 if (oit.it_flags & O_CREAT)
562 oit.it_op |= IT_CREAT;
568 /* Let's see if we have file open on MDS already. */
569 if (it->it_flags & FMODE_WRITE) {
570 och_p = &lli->lli_mds_write_och;
571 och_usecount = &lli->lli_open_fd_write_count;
572 } else if (it->it_flags & FMODE_EXEC) {
573 och_p = &lli->lli_mds_exec_och;
574 och_usecount = &lli->lli_open_fd_exec_count;
576 och_p = &lli->lli_mds_read_och;
577 och_usecount = &lli->lli_open_fd_read_count;
580 cfs_down(&lli->lli_och_sem);
581 if (*och_p) { /* Open handle is present */
582 if (it_disposition(it, DISP_OPEN_OPEN)) {
583 /* Well, there's extra open request that we do not need,
584 let's close it somehow. This will decref request. */
585 rc = it_open_error(DISP_OPEN_OPEN, it);
587 cfs_up(&lli->lli_och_sem);
588 GOTO(out_openerr, rc);
591 ll_release_openhandle(file->f_dentry, it);
592 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
597 rc = ll_local_open(file, it, fd, NULL);
600 cfs_up(&lli->lli_och_sem);
601 GOTO(out_openerr, rc);
604 LASSERT(*och_usecount == 0);
605 if (!it->d.lustre.it_disposition) {
606 /* We cannot just request lock handle now, new ELC code
607 means that one of other OPEN locks for this file
608 could be cancelled, and since blocking ast handler
609 would attempt to grab och_sem as well, that would
610 result in a deadlock */
611 cfs_up(&lli->lli_och_sem);
612 it->it_create_mode |= M_CHECK_STALE;
613 rc = ll_intent_file_open(file, NULL, 0, it);
614 it->it_create_mode &= ~M_CHECK_STALE;
616 GOTO(out_openerr, rc);
620 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
622 GOTO(out_och_free, rc = -ENOMEM);
626 /* md_intent_lock() didn't get a request ref if there was an
627 * open error, so don't do cleanup on the request here
629 /* XXX (green): Should not we bail out on any error here, not
630 * just open error? */
631 rc = it_open_error(DISP_OPEN_OPEN, it);
633 GOTO(out_och_free, rc);
635 LASSERT(it_disposition(it, DISP_ENQ_OPEN_REF));
637 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
638 rc = ll_local_open(file, it, fd, *och_p);
640 GOTO(out_och_free, rc);
642 cfs_up(&lli->lli_och_sem);
645 /* Must do this outside lli_och_sem lock to prevent deadlock where
646 different kind of OPEN lock for this same inode gets cancelled
647 by ldlm_cancel_lru */
648 if (!S_ISREG(inode->i_mode))
649 GOTO(out_och_free, rc);
655 if (file->f_flags & O_LOV_DELAY_CREATE ||
656 !(file->f_mode & FMODE_WRITE)) {
657 CDEBUG(D_INODE, "object creation was delayed\n");
658 GOTO(out_och_free, rc);
661 file->f_flags &= ~O_LOV_DELAY_CREATE;
662 GOTO(out_och_free, rc);
665 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
666 ptlrpc_req_finished(it->d.lustre.it_data);
667 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
671 if (och_p && *och_p) {
672 OBD_FREE(*och_p, sizeof (struct obd_client_handle));
673 *och_p = NULL; /* OBD_FREE writes some magic there */
676 cfs_up(&lli->lli_och_sem);
679 if (opendir_set != 0)
680 ll_stop_statahead(inode, lli->lli_opendir_key);
682 ll_file_data_put(fd);
688 /* Fills the obdo with the attributes for the lsm */
689 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
690 struct obd_capa *capa, struct obdo *obdo,
691 __u64 ioepoch, int sync)
693 struct ptlrpc_request_set *set;
694 struct obd_info oinfo = { { { 0 } } };
699 LASSERT(lsm != NULL);
703 oinfo.oi_oa->o_id = lsm->lsm_object_id;
704 oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
705 oinfo.oi_oa->o_mode = S_IFREG;
706 oinfo.oi_oa->o_ioepoch = ioepoch;
707 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
708 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
709 OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
710 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
711 OBD_MD_FLGROUP | OBD_MD_FLEPOCH;
712 oinfo.oi_capa = capa;
714 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
715 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
718 set = ptlrpc_prep_set();
720 CERROR("can't allocate ptlrpc set\n");
723 rc = obd_getattr_async(exp, &oinfo, set);
725 rc = ptlrpc_set_wait(set);
726 ptlrpc_set_destroy(set);
729 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
730 OBD_MD_FLATIME | OBD_MD_FLMTIME |
731 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
736 * Performs the getattr on the inode and updates its fields.
737 * If @sync != 0, perform the getattr under the server-side lock.
739 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
740 __u64 ioepoch, int sync)
742 struct ll_inode_info *lli = ll_i2info(inode);
743 struct obd_capa *capa = ll_mdscapa_get(inode);
747 rc = ll_lsm_getattr(lli->lli_smd, ll_i2dtexp(inode),
748 capa, obdo, ioepoch, sync);
751 obdo_refresh_inode(inode, obdo, obdo->o_valid);
753 "objid "LPX64" size %llu, blocks %llu, blksize %lu\n",
754 lli->lli_smd->lsm_object_id, i_size_read(inode),
755 (unsigned long long)inode->i_blocks,
756 (unsigned long)ll_inode_blksize(inode));
761 int ll_merge_lvb(struct inode *inode)
763 struct ll_inode_info *lli = ll_i2info(inode);
764 struct ll_sb_info *sbi = ll_i2sbi(inode);
770 ll_inode_size_lock(inode, 1);
771 inode_init_lvb(inode, &lvb);
773 /* merge timestamps the most resently obtained from mds with
774 timestamps obtained from osts */
775 lvb.lvb_atime = lli->lli_lvb.lvb_atime;
776 lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
777 lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
778 rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
779 cl_isize_write_nolock(inode, lvb.lvb_size);
781 CDEBUG(D_VFSTRACE, DFID" updating i_size "LPU64"\n",
782 PFID(&lli->lli_fid), lvb.lvb_size);
783 inode->i_blocks = lvb.lvb_blocks;
785 LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
786 LTIME_S(inode->i_atime) = lvb.lvb_atime;
787 LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
788 ll_inode_size_unlock(inode, 1);
793 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
796 struct obdo obdo = { 0 };
799 rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
801 st->st_size = obdo.o_size;
802 st->st_blocks = obdo.o_blocks;
803 st->st_mtime = obdo.o_mtime;
804 st->st_atime = obdo.o_atime;
805 st->st_ctime = obdo.o_ctime;
810 void ll_io_init(struct cl_io *io, const struct file *file, int write)
812 struct inode *inode = file->f_dentry->d_inode;
814 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
816 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
817 io->ci_obj = ll_i2info(inode)->lli_clob;
818 io->ci_lockreq = CILR_MAYBE;
819 if (ll_file_nolock(file)) {
820 io->ci_lockreq = CILR_NEVER;
821 io->ci_no_srvlock = 1;
822 } else if (file->f_flags & O_APPEND) {
823 io->ci_lockreq = CILR_MANDATORY;
827 static ssize_t ll_file_io_generic(const struct lu_env *env,
828 struct vvp_io_args *args, struct file *file,
829 enum cl_io_type iot, loff_t *ppos, size_t count)
831 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
836 io = ccc_env_thread_io(env);
837 ll_io_init(io, file, iot == CIT_WRITE);
839 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
840 struct vvp_io *vio = vvp_env_io(env);
841 struct ccc_io *cio = ccc_env_io(env);
842 int write_sem_locked = 0;
844 cio->cui_fd = LUSTRE_FPRIVATE(file);
845 vio->cui_io_subtype = args->via_io_subtype;
847 switch (vio->cui_io_subtype) {
849 cio->cui_iov = args->u.normal.via_iov;
850 cio->cui_nrsegs = args->u.normal.via_nrsegs;
851 cio->cui_tot_nrsegs = cio->cui_nrsegs;
852 #ifndef HAVE_FILE_WRITEV
853 cio->cui_iocb = args->u.normal.via_iocb;
855 if ((iot == CIT_WRITE) &&
856 !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
857 if(cfs_down_interruptible(&lli->lli_write_sem))
858 GOTO(out, result = -ERESTARTSYS);
859 write_sem_locked = 1;
860 } else if (iot == CIT_READ) {
861 cfs_down_read(&lli->lli_trunc_sem);
865 vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
866 vio->u.sendfile.cui_target = args->u.sendfile.via_target;
869 vio->u.splice.cui_pipe = args->u.splice.via_pipe;
870 vio->u.splice.cui_flags = args->u.splice.via_flags;
873 CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
876 result = cl_io_loop(env, io);
877 if (write_sem_locked)
878 cfs_up(&lli->lli_write_sem);
879 else if (args->via_io_subtype == IO_NORMAL && iot == CIT_READ)
880 cfs_up_read(&lli->lli_trunc_sem);
882 /* cl_io_rw_init() handled IO */
883 result = io->ci_result;
886 if (io->ci_nob > 0) {
888 *ppos = io->u.ci_wr.wr.crw_pos;
894 if (iot == CIT_READ) {
896 ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
897 LPROC_LL_READ_BYTES, result);
898 } else if (iot == CIT_WRITE) {
900 ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
901 LPROC_LL_WRITE_BYTES, result);
902 lli->lli_write_rc = 0;
904 lli->lli_write_rc = result;
913 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
915 static int ll_file_get_iov_count(const struct iovec *iov,
916 unsigned long *nr_segs, size_t *count)
921 for (seg = 0; seg < *nr_segs; seg++) {
922 const struct iovec *iv = &iov[seg];
925 * If any segment has a negative length, or the cumulative
926 * length ever wraps negative then return -EINVAL.
929 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
931 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
936 cnt -= iv->iov_len; /* This segment is no good */
943 #ifdef HAVE_FILE_READV
944 static ssize_t ll_file_readv(struct file *file, const struct iovec *iov,
945 unsigned long nr_segs, loff_t *ppos)
948 struct vvp_io_args *args;
954 result = ll_file_get_iov_count(iov, &nr_segs, &count);
958 env = cl_env_get(&refcheck);
960 RETURN(PTR_ERR(env));
962 args = vvp_env_args(env, IO_NORMAL);
963 args->u.normal.via_iov = (struct iovec *)iov;
964 args->u.normal.via_nrsegs = nr_segs;
966 result = ll_file_io_generic(env, args, file, CIT_READ, ppos, count);
967 cl_env_put(env, &refcheck);
971 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
975 struct iovec *local_iov;
980 env = cl_env_get(&refcheck);
982 RETURN(PTR_ERR(env));
984 local_iov = &vvp_env_info(env)->vti_local_iov;
985 local_iov->iov_base = (void __user *)buf;
986 local_iov->iov_len = count;
987 result = ll_file_readv(file, local_iov, 1, ppos);
988 cl_env_put(env, &refcheck);
993 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
994 unsigned long nr_segs, loff_t pos)
997 struct vvp_io_args *args;
1003 result = ll_file_get_iov_count(iov, &nr_segs, &count);
1007 env = cl_env_get(&refcheck);
1009 RETURN(PTR_ERR(env));
1011 args = vvp_env_args(env, IO_NORMAL);
1012 args->u.normal.via_iov = (struct iovec *)iov;
1013 args->u.normal.via_nrsegs = nr_segs;
1014 args->u.normal.via_iocb = iocb;
1016 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1017 &iocb->ki_pos, count);
1018 cl_env_put(env, &refcheck);
1022 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1026 struct iovec *local_iov;
1027 struct kiocb *kiocb;
1032 env = cl_env_get(&refcheck);
1034 RETURN(PTR_ERR(env));
1036 local_iov = &vvp_env_info(env)->vti_local_iov;
1037 kiocb = &vvp_env_info(env)->vti_kiocb;
1038 local_iov->iov_base = (void __user *)buf;
1039 local_iov->iov_len = count;
1040 init_sync_kiocb(kiocb, file);
1041 kiocb->ki_pos = *ppos;
1042 kiocb->ki_left = count;
1044 result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1045 *ppos = kiocb->ki_pos;
1047 cl_env_put(env, &refcheck);
1053 * Write to a file (through the page cache).
1055 #ifdef HAVE_FILE_WRITEV
1056 static ssize_t ll_file_writev(struct file *file, const struct iovec *iov,
1057 unsigned long nr_segs, loff_t *ppos)
1060 struct vvp_io_args *args;
1066 result = ll_file_get_iov_count(iov, &nr_segs, &count);
1070 env = cl_env_get(&refcheck);
1072 RETURN(PTR_ERR(env));
1074 args = vvp_env_args(env, IO_NORMAL);
1075 args->u.normal.via_iov = (struct iovec *)iov;
1076 args->u.normal.via_nrsegs = nr_segs;
1078 result = ll_file_io_generic(env, args, file, CIT_WRITE, ppos, count);
1079 cl_env_put(env, &refcheck);
1083 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1087 struct iovec *local_iov;
1092 env = cl_env_get(&refcheck);
1094 RETURN(PTR_ERR(env));
1096 local_iov = &vvp_env_info(env)->vti_local_iov;
1097 local_iov->iov_base = (void __user *)buf;
1098 local_iov->iov_len = count;
1100 result = ll_file_writev(file, local_iov, 1, ppos);
1101 cl_env_put(env, &refcheck);
1105 #else /* AIO stuff */
1106 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1107 unsigned long nr_segs, loff_t pos)
1110 struct vvp_io_args *args;
1116 result = ll_file_get_iov_count(iov, &nr_segs, &count);
1120 env = cl_env_get(&refcheck);
1122 RETURN(PTR_ERR(env));
1124 args = vvp_env_args(env, IO_NORMAL);
1125 args->u.normal.via_iov = (struct iovec *)iov;
1126 args->u.normal.via_nrsegs = nr_segs;
1127 args->u.normal.via_iocb = iocb;
1129 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1130 &iocb->ki_pos, count);
1131 cl_env_put(env, &refcheck);
1135 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1139 struct iovec *local_iov;
1140 struct kiocb *kiocb;
1145 env = cl_env_get(&refcheck);
1147 RETURN(PTR_ERR(env));
1149 local_iov = &vvp_env_info(env)->vti_local_iov;
1150 kiocb = &vvp_env_info(env)->vti_kiocb;
1151 local_iov->iov_base = (void __user *)buf;
1152 local_iov->iov_len = count;
1153 init_sync_kiocb(kiocb, file);
1154 kiocb->ki_pos = *ppos;
1155 kiocb->ki_left = count;
1157 result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1158 *ppos = kiocb->ki_pos;
1160 cl_env_put(env, &refcheck);
1166 #ifdef HAVE_KERNEL_SENDFILE
1168 * Send file content (through pagecache) somewhere with helper
1170 static ssize_t ll_file_sendfile(struct file *in_file, loff_t *ppos,size_t count,
1171 read_actor_t actor, void *target)
1174 struct vvp_io_args *args;
1179 env = cl_env_get(&refcheck);
1181 RETURN(PTR_ERR(env));
1183 args = vvp_env_args(env, IO_SENDFILE);
1184 args->u.sendfile.via_target = target;
1185 args->u.sendfile.via_actor = actor;
1187 result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1188 cl_env_put(env, &refcheck);
1193 #ifdef HAVE_KERNEL_SPLICE_READ
1195 * Send file content (through pagecache) somewhere with helper
1197 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1198 struct pipe_inode_info *pipe, size_t count,
1202 struct vvp_io_args *args;
1207 env = cl_env_get(&refcheck);
1209 RETURN(PTR_ERR(env));
1211 args = vvp_env_args(env, IO_SPLICE);
1212 args->u.splice.via_pipe = pipe;
1213 args->u.splice.via_flags = flags;
1215 result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1216 cl_env_put(env, &refcheck);
1221 static int ll_lov_recreate(struct inode *inode, obd_id id, obd_seq seq,
1224 struct obd_export *exp = ll_i2dtexp(inode);
1225 struct obd_trans_info oti = { 0 };
1226 struct obdo *oa = NULL;
1229 struct lov_stripe_md *lsm, *lsm2;
1236 ll_inode_size_lock(inode, 0);
1237 lsm = ll_i2info(inode)->lli_smd;
1239 GOTO(out, rc = -ENOENT);
1240 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1241 (lsm->lsm_stripe_count));
1243 OBD_ALLOC_LARGE(lsm2, lsm_size);
1245 GOTO(out, rc = -ENOMEM);
1249 oa->o_nlink = ost_idx;
1250 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1251 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1252 obdo_from_inode(oa, inode, &ll_i2info(inode)->lli_fid, OBD_MD_FLTYPE |
1253 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1254 memcpy(lsm2, lsm, lsm_size);
1255 rc = obd_create(exp, oa, &lsm2, &oti);
1257 OBD_FREE_LARGE(lsm2, lsm_size);
1260 ll_inode_size_unlock(inode, 0);
1265 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1267 struct ll_recreate_obj ucreat;
1270 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1273 if (cfs_copy_from_user(&ucreat, (struct ll_recreate_obj *)arg,
1274 sizeof(struct ll_recreate_obj)))
1277 RETURN(ll_lov_recreate(inode, ucreat.lrc_id, 0,
1278 ucreat.lrc_ost_idx));
1281 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1288 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1291 if (cfs_copy_from_user(&fid, (struct lu_fid *)arg,
1292 sizeof(struct lu_fid)))
1295 id = fid_oid(&fid) | ((fid_seq(&fid) & 0xffff) << 32);
1296 ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1297 RETURN(ll_lov_recreate(inode, id, 0, ost_idx));
1300 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1301 int flags, struct lov_user_md *lum, int lum_size)
1303 struct lov_stripe_md *lsm;
1304 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1308 ll_inode_size_lock(inode, 0);
1309 lsm = ll_i2info(inode)->lli_smd;
1311 ll_inode_size_unlock(inode, 0);
1312 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1317 rc = ll_intent_file_open(file, lum, lum_size, &oit);
1320 rc = oit.d.lustre.it_status;
1322 GOTO(out_req_free, rc);
1324 ll_release_openhandle(file->f_dentry, &oit);
1327 ll_inode_size_unlock(inode, 0);
1328 ll_intent_release(&oit);
1331 ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1335 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1336 struct lov_mds_md **lmmp, int *lmm_size,
1337 struct ptlrpc_request **request)
1339 struct ll_sb_info *sbi = ll_i2sbi(inode);
1340 struct mdt_body *body;
1341 struct lov_mds_md *lmm = NULL;
1342 struct ptlrpc_request *req = NULL;
1343 struct md_op_data *op_data;
1346 rc = ll_get_max_mdsize(sbi, &lmmsize);
1350 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1351 strlen(filename), lmmsize,
1352 LUSTRE_OPC_ANY, NULL);
1353 if (IS_ERR(op_data))
1354 RETURN(PTR_ERR(op_data));
1356 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1357 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1358 ll_finish_md_op_data(op_data);
1360 CDEBUG(D_INFO, "md_getattr_name failed "
1361 "on %s: rc %d\n", filename, rc);
1365 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1366 LASSERT(body != NULL); /* checked by mdc_getattr_name */
1368 lmmsize = body->eadatasize;
1370 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1372 GOTO(out, rc = -ENODATA);
1375 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1376 LASSERT(lmm != NULL);
1378 if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1379 (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1380 GOTO(out, rc = -EPROTO);
1384 * This is coming from the MDS, so is probably in
1385 * little endian. We convert it to host endian before
1386 * passing it to userspace.
1388 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1389 /* if function called for directory - we should
1390 * avoid swab not existent lsm objects */
1391 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1392 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1393 if (S_ISREG(body->mode))
1394 lustre_swab_lov_user_md_objects(
1395 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1396 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1397 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1398 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1399 if (S_ISREG(body->mode))
1400 lustre_swab_lov_user_md_objects(
1401 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1402 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1408 *lmm_size = lmmsize;
1413 static int ll_lov_setea(struct inode *inode, struct file *file,
1416 int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1417 struct lov_user_md *lump;
1418 int lum_size = sizeof(struct lov_user_md) +
1419 sizeof(struct lov_user_ost_data);
1423 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1426 OBD_ALLOC_LARGE(lump, lum_size);
1430 if (cfs_copy_from_user(lump, (struct lov_user_md *)arg, lum_size)) {
1431 OBD_FREE_LARGE(lump, lum_size);
1435 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1437 OBD_FREE_LARGE(lump, lum_size);
1441 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1444 struct lov_user_md_v3 lumv3;
1445 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1446 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1447 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1450 int flags = FMODE_WRITE;
1453 /* first try with v1 which is smaller than v3 */
1454 lum_size = sizeof(struct lov_user_md_v1);
1455 if (cfs_copy_from_user(lumv1, lumv1p, lum_size))
1458 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1459 lum_size = sizeof(struct lov_user_md_v3);
1460 if (cfs_copy_from_user(&lumv3, lumv3p, lum_size))
1464 rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1466 put_user(0, &lumv1p->lmm_stripe_count);
1467 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1468 0, ll_i2info(inode)->lli_smd,
1474 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1476 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1481 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
1486 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1488 struct ll_inode_info *lli = ll_i2info(inode);
1489 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1490 struct ccc_grouplock grouplock;
1494 if (ll_file_nolock(file))
1495 RETURN(-EOPNOTSUPP);
1497 cfs_spin_lock(&lli->lli_lock);
1498 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1499 CWARN("group lock already existed with gid %lu\n",
1500 fd->fd_grouplock.cg_gid);
1501 cfs_spin_unlock(&lli->lli_lock);
1504 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1505 cfs_spin_unlock(&lli->lli_lock);
1507 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1508 arg, (file->f_flags & O_NONBLOCK), &grouplock);
1512 cfs_spin_lock(&lli->lli_lock);
1513 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1514 cfs_spin_unlock(&lli->lli_lock);
1515 CERROR("another thread just won the race\n");
1516 cl_put_grouplock(&grouplock);
1520 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1521 fd->fd_grouplock = grouplock;
1522 cfs_spin_unlock(&lli->lli_lock);
1524 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1528 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1530 struct ll_inode_info *lli = ll_i2info(inode);
1531 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1532 struct ccc_grouplock grouplock;
1535 cfs_spin_lock(&lli->lli_lock);
1536 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1537 cfs_spin_unlock(&lli->lli_lock);
1538 CWARN("no group lock held\n");
1541 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1543 if (fd->fd_grouplock.cg_gid != arg) {
1544 CWARN("group lock %lu doesn't match current id %lu\n",
1545 arg, fd->fd_grouplock.cg_gid);
1546 cfs_spin_unlock(&lli->lli_lock);
1550 grouplock = fd->fd_grouplock;
1551 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1552 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1553 cfs_spin_unlock(&lli->lli_lock);
1555 cl_put_grouplock(&grouplock);
1556 CDEBUG(D_INFO, "group lock %lu released\n", arg);
1561 * Close inode open handle
1563 * \param dentry [in] dentry which contains the inode
1564 * \param it [in,out] intent which contains open info and result
1567 * \retval <0 failure
1569 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1571 struct inode *inode = dentry->d_inode;
1572 struct obd_client_handle *och;
1578 /* Root ? Do nothing. */
1579 if (dentry->d_inode->i_sb->s_root == dentry)
1582 /* No open handle to close? Move away */
1583 if (!it_disposition(it, DISP_OPEN_OPEN))
1586 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1588 OBD_ALLOC(och, sizeof(*och));
1590 GOTO(out, rc = -ENOMEM);
1592 ll_och_fill(ll_i2sbi(inode)->ll_md_exp,
1593 ll_i2info(inode), it, och);
1595 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1598 /* this one is in place of ll_file_open */
1599 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1600 ptlrpc_req_finished(it->d.lustre.it_data);
1601 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1607 * Get size for inode for which FIEMAP mapping is requested.
1608 * Make the FIEMAP get_info call and returns the result.
1610 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1613 struct obd_export *exp = ll_i2dtexp(inode);
1614 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1615 struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1616 int vallen = num_bytes;
1620 /* Checks for fiemap flags */
1621 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1622 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1626 /* Check for FIEMAP_FLAG_SYNC */
1627 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1628 rc = filemap_fdatawrite(inode->i_mapping);
1633 /* If the stripe_count > 1 and the application does not understand
1634 * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1636 if (lsm->lsm_stripe_count > 1 &&
1637 !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1640 fm_key.oa.o_id = lsm->lsm_object_id;
1641 fm_key.oa.o_seq = lsm->lsm_object_seq;
1642 fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1644 obdo_from_inode(&fm_key.oa, inode, &ll_i2info(inode)->lli_fid,
1646 /* If filesize is 0, then there would be no objects for mapping */
1647 if (fm_key.oa.o_size == 0) {
1648 fiemap->fm_mapped_extents = 0;
1652 memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1654 rc = obd_get_info(exp, sizeof(fm_key), &fm_key, &vallen, fiemap, lsm);
1656 CERROR("obd_get_info failed: rc = %d\n", rc);
1661 int ll_fid2path(struct obd_export *exp, void *arg)
1663 struct getinfo_fid2path *gfout, *gfin;
1667 /* Need to get the buflen */
1668 OBD_ALLOC_PTR(gfin);
1671 if (cfs_copy_from_user(gfin, arg, sizeof(*gfin))) {
1676 outsize = sizeof(*gfout) + gfin->gf_pathlen;
1677 OBD_ALLOC(gfout, outsize);
1678 if (gfout == NULL) {
1682 memcpy(gfout, gfin, sizeof(*gfout));
1685 /* Call mdc_iocontrol */
1686 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1689 if (cfs_copy_to_user(arg, gfout, outsize))
1693 OBD_FREE(gfout, outsize);
1697 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1699 struct ll_user_fiemap *fiemap_s;
1700 size_t num_bytes, ret_bytes;
1701 unsigned int extent_count;
1704 /* Get the extent count so we can calculate the size of
1705 * required fiemap buffer */
1706 if (get_user(extent_count,
1707 &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1709 num_bytes = sizeof(*fiemap_s) + (extent_count *
1710 sizeof(struct ll_fiemap_extent));
1712 OBD_ALLOC_LARGE(fiemap_s, num_bytes);
1713 if (fiemap_s == NULL)
1716 /* get the fiemap value */
1717 if (copy_from_user(fiemap_s,(struct ll_user_fiemap __user *)arg,
1719 GOTO(error, rc = -EFAULT);
1721 /* If fm_extent_count is non-zero, read the first extent since
1722 * it is used to calculate end_offset and device from previous
1725 if (copy_from_user(&fiemap_s->fm_extents[0],
1726 (char __user *)arg + sizeof(*fiemap_s),
1727 sizeof(struct ll_fiemap_extent)))
1728 GOTO(error, rc = -EFAULT);
1731 rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1735 ret_bytes = sizeof(struct ll_user_fiemap);
1737 if (extent_count != 0)
1738 ret_bytes += (fiemap_s->fm_mapped_extents *
1739 sizeof(struct ll_fiemap_extent));
1741 if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1745 OBD_FREE_LARGE(fiemap_s, num_bytes);
1749 #ifdef HAVE_UNLOCKED_IOCTL
1750 long ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1752 struct inode *inode = file->f_dentry->d_inode;
1754 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1758 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1763 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1764 inode->i_generation, inode, cmd);
1765 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1767 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1768 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1772 case LL_IOC_GETFLAGS:
1773 /* Get the current value of the file flags */
1774 return put_user(fd->fd_flags, (int *)arg);
1775 case LL_IOC_SETFLAGS:
1776 case LL_IOC_CLRFLAGS:
1777 /* Set or clear specific file flags */
1778 /* XXX This probably needs checks to ensure the flags are
1779 * not abused, and to handle any flag side effects.
1781 if (get_user(flags, (int *) arg))
1784 if (cmd == LL_IOC_SETFLAGS) {
1785 if ((flags & LL_FILE_IGNORE_LOCK) &&
1786 !(file->f_flags & O_DIRECT)) {
1787 CERROR("%s: unable to disable locking on "
1788 "non-O_DIRECT file\n", current->comm);
1792 fd->fd_flags |= flags;
1794 fd->fd_flags &= ~flags;
1797 case LL_IOC_LOV_SETSTRIPE:
1798 RETURN(ll_lov_setstripe(inode, file, arg));
1799 case LL_IOC_LOV_SETEA:
1800 RETURN(ll_lov_setea(inode, file, arg));
1801 case LL_IOC_LOV_GETSTRIPE:
1802 RETURN(ll_lov_getstripe(inode, arg));
1803 case LL_IOC_RECREATE_OBJ:
1804 RETURN(ll_lov_recreate_obj(inode, arg));
1805 case LL_IOC_RECREATE_FID:
1806 RETURN(ll_lov_recreate_fid(inode, arg));
1807 case FSFILT_IOC_FIEMAP:
1808 RETURN(ll_ioctl_fiemap(inode, arg));
1809 case FSFILT_IOC_GETFLAGS:
1810 case FSFILT_IOC_SETFLAGS:
1811 RETURN(ll_iocontrol(inode, file, cmd, arg));
1812 case FSFILT_IOC_GETVERSION_OLD:
1813 case FSFILT_IOC_GETVERSION:
1814 RETURN(put_user(inode->i_generation, (int *)arg));
1815 case LL_IOC_GROUP_LOCK:
1816 RETURN(ll_get_grouplock(inode, file, arg));
1817 case LL_IOC_GROUP_UNLOCK:
1818 RETURN(ll_put_grouplock(inode, file, arg));
1819 case IOC_OBD_STATFS:
1820 RETURN(ll_obd_statfs(inode, (void *)arg));
1822 /* We need to special case any other ioctls we want to handle,
1823 * to send them to the MDS/OST as appropriate and to properly
1824 * network encode the arg field.
1825 case FSFILT_IOC_SETVERSION_OLD:
1826 case FSFILT_IOC_SETVERSION:
1828 case LL_IOC_FLUSHCTX:
1829 RETURN(ll_flush_ctx(inode));
1830 case LL_IOC_PATH2FID: {
1831 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1832 sizeof(struct lu_fid)))
1837 case OBD_IOC_FID2PATH:
1838 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1839 case LL_IOC_GET_MDTIDX: {
1842 mdtidx = ll_get_mdt_idx(inode);
1846 if (put_user((int)mdtidx, (int*)arg))
1851 case OBD_IOC_GETDTNAME:
1852 case OBD_IOC_GETMDNAME:
1853 RETURN(ll_get_obd_name(inode, cmd, arg));
1858 ll_iocontrol_call(inode, file, cmd, arg, &err))
1861 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1867 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1869 struct inode *inode = file->f_dentry->d_inode;
1872 retval = offset + ((origin == 2) ? i_size_read(inode) :
1873 (origin == 1) ? file->f_pos : 0);
1874 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%llu=%#llx(%s)\n",
1875 inode->i_ino, inode->i_generation, inode, retval, retval,
1876 origin == 2 ? "SEEK_END": origin == 1 ? "SEEK_CUR" : "SEEK_SET");
1877 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
1879 if (origin == 2) { /* SEEK_END */
1882 rc = ll_glimpse_size(inode);
1886 offset += i_size_read(inode);
1887 } else if (origin == 1) { /* SEEK_CUR */
1888 offset += file->f_pos;
1892 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1893 if (offset != file->f_pos) {
1894 file->f_pos = offset;
1902 #ifdef HAVE_FLUSH_OWNER_ID
1903 int ll_flush(struct file *file, fl_owner_t id)
1905 int ll_flush(struct file *file)
1908 struct inode *inode = file->f_dentry->d_inode;
1909 struct ll_inode_info *lli = ll_i2info(inode);
1910 struct lov_stripe_md *lsm = lli->lli_smd;
1913 LASSERT(!S_ISDIR(inode->i_mode));
1915 /* the application should know write failure already. */
1916 if (lli->lli_write_rc)
1919 /* catch async errors that were recorded back when async writeback
1920 * failed for pages in this mapping. */
1921 rc = lli->lli_async_rc;
1922 lli->lli_async_rc = 0;
1924 err = lov_test_and_clear_async_rc(lsm);
1929 return rc ? -EIO : 0;
1932 #ifndef HAVE_FILE_FSYNC_2ARGS
1933 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1935 int ll_fsync(struct file *file, int data)
1938 struct inode *inode = file->f_dentry->d_inode;
1939 struct ll_inode_info *lli = ll_i2info(inode);
1940 struct lov_stripe_md *lsm = lli->lli_smd;
1941 struct ptlrpc_request *req;
1942 struct obd_capa *oc;
1945 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1946 inode->i_generation, inode);
1947 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
1949 /* fsync's caller has already called _fdata{sync,write}, we want
1950 * that IO to finish before calling the osc and mdc sync methods */
1951 rc = filemap_fdatawait(inode->i_mapping);
1953 /* catch async errors that were recorded back when async writeback
1954 * failed for pages in this mapping. */
1955 if (!S_ISDIR(inode->i_mode)) {
1956 err = lli->lli_async_rc;
1957 lli->lli_async_rc = 0;
1961 err = lov_test_and_clear_async_rc(lsm);
1967 oc = ll_mdscapa_get(inode);
1968 err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
1974 ptlrpc_req_finished(req);
1977 struct obd_info *oinfo;
1979 OBD_ALLOC_PTR(oinfo);
1981 RETURN(rc ? rc : -ENOMEM);
1982 OBDO_ALLOC(oinfo->oi_oa);
1983 if (!oinfo->oi_oa) {
1984 OBD_FREE_PTR(oinfo);
1985 RETURN(rc ? rc : -ENOMEM);
1987 oinfo->oi_oa->o_id = lsm->lsm_object_id;
1988 oinfo->oi_oa->o_seq = lsm->lsm_object_seq;
1989 oinfo->oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1990 obdo_from_inode(oinfo->oi_oa, inode, &ll_i2info(inode)->lli_fid,
1991 OBD_MD_FLTYPE | OBD_MD_FLATIME |
1992 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1995 oinfo->oi_capa = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
1996 err = obd_sync_rqset(ll_i2sbi(inode)->ll_dt_exp, oinfo, 0,
1998 capa_put(oinfo->oi_capa);
2001 OBDO_FREE(oinfo->oi_oa);
2002 OBD_FREE_PTR(oinfo);
2003 lli->lli_write_rc = rc < 0 ? rc : 0;
2009 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2011 struct inode *inode = file->f_dentry->d_inode;
2012 struct ll_sb_info *sbi = ll_i2sbi(inode);
2013 struct ldlm_enqueue_info einfo = { .ei_type = LDLM_FLOCK,
2014 .ei_cb_cp =ldlm_flock_completion_ast,
2015 .ei_cbdata = file_lock };
2016 struct md_op_data *op_data;
2017 struct lustre_handle lockh = {0};
2018 ldlm_policy_data_t flock = {{0}};
2023 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
2024 inode->i_ino, file_lock);
2026 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2028 if (file_lock->fl_flags & FL_FLOCK) {
2029 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2030 /* flocks are whole-file locks */
2031 flock.l_flock.end = OFFSET_MAX;
2032 /* For flocks owner is determined by the local file desctiptor*/
2033 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
2034 } else if (file_lock->fl_flags & FL_POSIX) {
2035 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2036 flock.l_flock.start = file_lock->fl_start;
2037 flock.l_flock.end = file_lock->fl_end;
2041 flock.l_flock.pid = file_lock->fl_pid;
2043 /* Somewhat ugly workaround for svc lockd.
2044 * lockd installs custom fl_lmops->fl_compare_owner that checks
2045 * for the fl_owner to be the same (which it always is on local node
2046 * I guess between lockd processes) and then compares pid.
2047 * As such we assign pid to the owner field to make it all work,
2048 * conflict with normal locks is unlikely since pid space and
2049 * pointer space for current->files are not intersecting */
2050 if (file_lock->fl_lmops && file_lock->fl_lmops->fl_compare_owner)
2051 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2053 switch (file_lock->fl_type) {
2055 einfo.ei_mode = LCK_PR;
2058 /* An unlock request may or may not have any relation to
2059 * existing locks so we may not be able to pass a lock handle
2060 * via a normal ldlm_lock_cancel() request. The request may even
2061 * unlock a byte range in the middle of an existing lock. In
2062 * order to process an unlock request we need all of the same
2063 * information that is given with a normal read or write record
2064 * lock request. To avoid creating another ldlm unlock (cancel)
2065 * message we'll treat a LCK_NL flock request as an unlock. */
2066 einfo.ei_mode = LCK_NL;
2069 einfo.ei_mode = LCK_PW;
2072 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n",
2073 file_lock->fl_type);
2088 flags = LDLM_FL_BLOCK_NOWAIT;
2094 flags = LDLM_FL_TEST_LOCK;
2095 /* Save the old mode so that if the mode in the lock changes we
2096 * can decrement the appropriate reader or writer refcount. */
2097 file_lock->fl_type = einfo.ei_mode;
2100 CERROR("unknown fcntl lock command: %d\n", cmd);
2104 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2105 LUSTRE_OPC_ANY, NULL);
2106 if (IS_ERR(op_data))
2107 RETURN(PTR_ERR(op_data));
2109 CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
2110 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
2111 flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2113 rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2114 op_data, &lockh, &flock, 0, NULL /* req */, flags);
2116 ll_finish_md_op_data(op_data);
2118 if ((file_lock->fl_flags & FL_FLOCK) &&
2119 (rc == 0 || file_lock->fl_type == F_UNLCK))
2120 ll_flock_lock_file_wait(file, file_lock, (cmd == F_SETLKW));
2121 #ifdef HAVE_F_OP_FLOCK
2122 if ((file_lock->fl_flags & FL_POSIX) &&
2123 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2124 !(flags & LDLM_FL_TEST_LOCK))
2125 posix_lock_file_wait(file, file_lock);
2131 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2139 * test if some locks matching bits and l_req_mode are acquired
2140 * - bits can be in different locks
2141 * - if found clear the common lock bits in *bits
2142 * - the bits not found, are kept in *bits
2144 * \param bits [IN] searched lock bits [IN]
2145 * \param l_req_mode [IN] searched lock mode
2146 * \retval boolean, true iff all bits are found
2148 int ll_have_md_lock(struct inode *inode, __u64 *bits, ldlm_mode_t l_req_mode)
2150 struct lustre_handle lockh;
2151 ldlm_policy_data_t policy;
2152 ldlm_mode_t mode = (l_req_mode == LCK_MINMODE) ?
2153 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2162 fid = &ll_i2info(inode)->lli_fid;
2163 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2164 ldlm_lockname[mode]);
2166 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2167 for (i = 0; i < MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2168 policy.l_inodebits.bits = *bits & (1 << i);
2169 if (policy.l_inodebits.bits == 0)
2172 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2173 &policy, mode, &lockh)) {
2174 struct ldlm_lock *lock;
2176 lock = ldlm_handle2lock(&lockh);
2179 ~(lock->l_policy_data.l_inodebits.bits);
2180 LDLM_LOCK_PUT(lock);
2182 *bits &= ~policy.l_inodebits.bits;
2189 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2190 struct lustre_handle *lockh)
2192 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2198 fid = &ll_i2info(inode)->lli_fid;
2199 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2201 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
2202 rc = md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2203 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2207 static int ll_inode_revalidate_fini(struct inode *inode, int rc) {
2208 if (rc == -ENOENT) { /* Already unlinked. Just update nlink
2209 * and return success */
2211 /* This path cannot be hit for regular files unless in
2212 * case of obscure races, so no need to to validate
2214 if (!S_ISREG(inode->i_mode) &&
2215 !S_ISDIR(inode->i_mode))
2220 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2228 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2231 struct inode *inode = dentry->d_inode;
2232 struct ptlrpc_request *req = NULL;
2233 struct obd_export *exp;
2238 CERROR("REPORT THIS LINE TO PETER\n");
2242 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2243 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2245 exp = ll_i2mdexp(inode);
2247 /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2248 * But under CMD case, it caused some lock issues, should be fixed
2249 * with new CMD ibits lock. See bug 12718 */
2250 if (exp->exp_connect_flags & OBD_CONNECT_ATTRFID) {
2251 struct lookup_intent oit = { .it_op = IT_GETATTR };
2252 struct md_op_data *op_data;
2254 if (ibits == MDS_INODELOCK_LOOKUP)
2255 oit.it_op = IT_LOOKUP;
2257 /* Call getattr by fid, so do not provide name at all. */
2258 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2259 dentry->d_inode, NULL, 0, 0,
2260 LUSTRE_OPC_ANY, NULL);
2261 if (IS_ERR(op_data))
2262 RETURN(PTR_ERR(op_data));
2264 oit.it_create_mode |= M_CHECK_STALE;
2265 rc = md_intent_lock(exp, op_data, NULL, 0,
2266 /* we are not interested in name
2269 ll_md_blocking_ast, 0);
2270 ll_finish_md_op_data(op_data);
2271 oit.it_create_mode &= ~M_CHECK_STALE;
2273 rc = ll_inode_revalidate_fini(inode, rc);
2277 rc = ll_revalidate_it_finish(req, &oit, dentry);
2279 ll_intent_release(&oit);
2283 /* Unlinked? Unhash dentry, so it is not picked up later by
2284 do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2285 here to preserve get_cwd functionality on 2.6.
2287 if (!dentry->d_inode->i_nlink) {
2288 cfs_spin_lock(&ll_lookup_lock);
2289 spin_lock(&dcache_lock);
2290 ll_drop_dentry(dentry);
2291 spin_unlock(&dcache_lock);
2292 cfs_spin_unlock(&ll_lookup_lock);
2295 ll_lookup_finish_locks(&oit, dentry);
2296 } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
2297 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2298 obd_valid valid = OBD_MD_FLGETATTR;
2299 struct md_op_data *op_data;
2302 if (S_ISREG(inode->i_mode)) {
2303 rc = ll_get_max_mdsize(sbi, &ealen);
2306 valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2309 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2310 0, ealen, LUSTRE_OPC_ANY,
2312 if (IS_ERR(op_data))
2313 RETURN(PTR_ERR(op_data));
2315 op_data->op_valid = valid;
2316 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2317 * capa for this inode. Because we only keep capas of dirs
2319 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2320 ll_finish_md_op_data(op_data);
2322 rc = ll_inode_revalidate_fini(inode, rc);
2326 rc = ll_prep_inode(&inode, req, NULL);
2329 ptlrpc_req_finished(req);
2333 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2336 struct inode *inode = dentry->d_inode;
2340 rc = __ll_inode_revalidate_it(dentry, it, ibits);
2342 /* if object not yet allocated, don't validate size */
2343 if (rc == 0 && ll_i2info(dentry->d_inode)->lli_smd == NULL) {
2344 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2345 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2346 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2350 /* ll_glimpse_size will prefer locally cached writes if they extend
2354 rc = ll_glimpse_size(inode);
2359 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2360 struct lookup_intent *it, struct kstat *stat)
2362 struct inode *inode = de->d_inode;
2363 struct ll_sb_info *sbi = ll_i2sbi(inode);
2364 struct ll_inode_info *lli = ll_i2info(inode);
2367 res = ll_inode_revalidate_it(de, it, MDS_INODELOCK_UPDATE |
2368 MDS_INODELOCK_LOOKUP);
2369 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
2374 stat->dev = inode->i_sb->s_dev;
2375 if (ll_need_32bit_api(sbi))
2376 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
2378 stat->ino = inode->i_ino;
2379 stat->mode = inode->i_mode;
2380 stat->nlink = inode->i_nlink;
2381 stat->uid = inode->i_uid;
2382 stat->gid = inode->i_gid;
2383 stat->rdev = kdev_t_to_nr(inode->i_rdev);
2384 stat->atime = inode->i_atime;
2385 stat->mtime = inode->i_mtime;
2386 stat->ctime = inode->i_ctime;
2387 #ifdef HAVE_INODE_BLKSIZE
2388 stat->blksize = inode->i_blksize;
2390 stat->blksize = 1 << inode->i_blkbits;
2393 stat->size = i_size_read(inode);
2394 stat->blocks = inode->i_blocks;
2398 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2400 struct lookup_intent it = { .it_op = IT_GETATTR };
2402 return ll_getattr_it(mnt, de, &it, stat);
2405 #ifdef HAVE_LINUX_FIEMAP_H
2406 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2407 __u64 start, __u64 len)
2411 struct ll_user_fiemap *fiemap;
2412 unsigned int extent_count = fieinfo->fi_extents_max;
2414 num_bytes = sizeof(*fiemap) + (extent_count *
2415 sizeof(struct ll_fiemap_extent));
2416 OBD_ALLOC_LARGE(fiemap, num_bytes);
2421 fiemap->fm_flags = fieinfo->fi_flags;
2422 fiemap->fm_extent_count = fieinfo->fi_extents_max;
2423 fiemap->fm_start = start;
2424 fiemap->fm_length = len;
2425 memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2426 sizeof(struct ll_fiemap_extent));
2428 rc = ll_do_fiemap(inode, fiemap, num_bytes);
2430 fieinfo->fi_flags = fiemap->fm_flags;
2431 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2432 memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2433 fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2435 OBD_FREE_LARGE(fiemap, num_bytes);
2442 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2443 lustre_check_acl(struct inode *inode, int mask, unsigned int flags)
2445 lustre_check_acl(struct inode *inode, int mask)
2448 #ifdef CONFIG_FS_POSIX_ACL
2449 struct ll_inode_info *lli = ll_i2info(inode);
2450 struct posix_acl *acl;
2454 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2455 if (flags & IPERM_FLAG_RCU)
2458 cfs_spin_lock(&lli->lli_lock);
2459 acl = posix_acl_dup(lli->lli_posix_acl);
2460 cfs_spin_unlock(&lli->lli_lock);
2465 rc = posix_acl_permission(inode, acl, mask);
2466 posix_acl_release(acl);
2474 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2475 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
2477 # ifdef HAVE_INODE_PERMISION_2ARGS
2478 int ll_inode_permission(struct inode *inode, int mask)
2480 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2487 /* as root inode are NOT getting validated in lookup operation,
2488 * need to do it before permission check. */
2490 if (inode == inode->i_sb->s_root->d_inode) {
2491 struct lookup_intent it = { .it_op = IT_LOOKUP };
2493 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2494 MDS_INODELOCK_LOOKUP);
2499 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2500 inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2502 if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2503 return lustre_check_remote_perm(inode, mask);
2505 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2506 rc = ll_generic_permission(inode, mask, flags, lustre_check_acl);
2511 #ifdef HAVE_FILE_READV
2512 #define READ_METHOD readv
2513 #define READ_FUNCTION ll_file_readv
2514 #define WRITE_METHOD writev
2515 #define WRITE_FUNCTION ll_file_writev
2517 #define READ_METHOD aio_read
2518 #define READ_FUNCTION ll_file_aio_read
2519 #define WRITE_METHOD aio_write
2520 #define WRITE_FUNCTION ll_file_aio_write
2523 /* -o localflock - only provides locally consistent flock locks */
2524 struct file_operations ll_file_operations = {
2525 .read = ll_file_read,
2526 .READ_METHOD = READ_FUNCTION,
2527 .write = ll_file_write,
2528 .WRITE_METHOD = WRITE_FUNCTION,
2529 #ifdef HAVE_UNLOCKED_IOCTL
2530 .unlocked_ioctl = ll_file_ioctl,
2532 .ioctl = ll_file_ioctl,
2534 .open = ll_file_open,
2535 .release = ll_file_release,
2536 .mmap = ll_file_mmap,
2537 .llseek = ll_file_seek,
2538 #ifdef HAVE_KERNEL_SENDFILE
2539 .sendfile = ll_file_sendfile,
2541 #ifdef HAVE_KERNEL_SPLICE_READ
2542 .splice_read = ll_file_splice_read,
2548 struct file_operations ll_file_operations_flock = {
2549 .read = ll_file_read,
2550 .READ_METHOD = READ_FUNCTION,
2551 .write = ll_file_write,
2552 .WRITE_METHOD = WRITE_FUNCTION,
2553 #ifdef HAVE_UNLOCKED_IOCTL
2554 .unlocked_ioctl = ll_file_ioctl,
2556 .ioctl = ll_file_ioctl,
2558 .open = ll_file_open,
2559 .release = ll_file_release,
2560 .mmap = ll_file_mmap,
2561 .llseek = ll_file_seek,
2562 #ifdef HAVE_KERNEL_SENDFILE
2563 .sendfile = ll_file_sendfile,
2565 #ifdef HAVE_KERNEL_SPLICE_READ
2566 .splice_read = ll_file_splice_read,
2570 #ifdef HAVE_F_OP_FLOCK
2571 .flock = ll_file_flock,
2573 .lock = ll_file_flock
2576 /* These are for -o noflock - to return ENOSYS on flock calls */
2577 struct file_operations ll_file_operations_noflock = {
2578 .read = ll_file_read,
2579 .READ_METHOD = READ_FUNCTION,
2580 .write = ll_file_write,
2581 .WRITE_METHOD = WRITE_FUNCTION,
2582 #ifdef HAVE_UNLOCKED_IOCTL
2583 .unlocked_ioctl = ll_file_ioctl,
2585 .ioctl = ll_file_ioctl,
2587 .open = ll_file_open,
2588 .release = ll_file_release,
2589 .mmap = ll_file_mmap,
2590 .llseek = ll_file_seek,
2591 #ifdef HAVE_KERNEL_SENDFILE
2592 .sendfile = ll_file_sendfile,
2594 #ifdef HAVE_KERNEL_SPLICE_READ
2595 .splice_read = ll_file_splice_read,
2599 #ifdef HAVE_F_OP_FLOCK
2600 .flock = ll_file_noflock,
2602 .lock = ll_file_noflock
2605 struct inode_operations ll_file_inode_operations = {
2606 .setattr = ll_setattr,
2607 .truncate = ll_truncate,
2608 .getattr = ll_getattr,
2609 .permission = ll_inode_permission,
2610 .setxattr = ll_setxattr,
2611 .getxattr = ll_getxattr,
2612 .listxattr = ll_listxattr,
2613 .removexattr = ll_removexattr,
2614 #ifdef HAVE_LINUX_FIEMAP_H
2615 .fiemap = ll_fiemap,
2619 /* dynamic ioctl number support routins */
2620 static struct llioc_ctl_data {
2621 cfs_rw_semaphore_t ioc_sem;
2622 cfs_list_t ioc_head;
2624 __RWSEM_INITIALIZER(llioc.ioc_sem),
2625 CFS_LIST_HEAD_INIT(llioc.ioc_head)
2630 cfs_list_t iocd_list;
2631 unsigned int iocd_size;
2632 llioc_callback_t iocd_cb;
2633 unsigned int iocd_count;
2634 unsigned int iocd_cmd[0];
2637 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
2640 struct llioc_data *in_data = NULL;
2643 if (cb == NULL || cmd == NULL ||
2644 count > LLIOC_MAX_CMD || count < 0)
2647 size = sizeof(*in_data) + count * sizeof(unsigned int);
2648 OBD_ALLOC(in_data, size);
2649 if (in_data == NULL)
2652 memset(in_data, 0, sizeof(*in_data));
2653 in_data->iocd_size = size;
2654 in_data->iocd_cb = cb;
2655 in_data->iocd_count = count;
2656 memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
2658 cfs_down_write(&llioc.ioc_sem);
2659 cfs_list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
2660 cfs_up_write(&llioc.ioc_sem);
2665 void ll_iocontrol_unregister(void *magic)
2667 struct llioc_data *tmp;
2672 cfs_down_write(&llioc.ioc_sem);
2673 cfs_list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
2675 unsigned int size = tmp->iocd_size;
2677 cfs_list_del(&tmp->iocd_list);
2678 cfs_up_write(&llioc.ioc_sem);
2680 OBD_FREE(tmp, size);
2684 cfs_up_write(&llioc.ioc_sem);
2686 CWARN("didn't find iocontrol register block with magic: %p\n", magic);
2689 EXPORT_SYMBOL(ll_iocontrol_register);
2690 EXPORT_SYMBOL(ll_iocontrol_unregister);
2692 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
2693 unsigned int cmd, unsigned long arg, int *rcp)
2695 enum llioc_iter ret = LLIOC_CONT;
2696 struct llioc_data *data;
2697 int rc = -EINVAL, i;
2699 cfs_down_read(&llioc.ioc_sem);
2700 cfs_list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
2701 for (i = 0; i < data->iocd_count; i++) {
2702 if (cmd != data->iocd_cmd[i])
2705 ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
2709 if (ret == LLIOC_STOP)
2712 cfs_up_read(&llioc.ioc_sem);