4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "../include/lustre_dlm.h"
41 #include "../include/lustre_lite.h"
42 #include <linux/pagemap.h>
43 #include <linux/file.h>
44 #include <linux/mount.h>
45 #include "llite_internal.h"
46 #include "../include/lustre/ll_fiemap.h"
48 #include "../include/cl_object.h"
51 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
53 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
56 static enum llioc_iter
57 ll_iocontrol_call(struct inode *inode, struct file *file,
58 unsigned int cmd, unsigned long arg, int *rcp);
60 static struct ll_file_data *ll_file_data_get(void)
62 struct ll_file_data *fd;
64 fd = kmem_cache_zalloc(ll_file_data_slab, GFP_NOFS);
67 fd->fd_write_failed = false;
71 static void ll_file_data_put(struct ll_file_data *fd)
74 kmem_cache_free(ll_file_data_slab, fd);
77 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
78 struct lustre_handle *fh)
80 op_data->op_fid1 = ll_i2info(inode)->lli_fid;
81 op_data->op_attr.ia_mode = inode->i_mode;
82 op_data->op_attr.ia_atime = inode->i_atime;
83 op_data->op_attr.ia_mtime = inode->i_mtime;
84 op_data->op_attr.ia_ctime = inode->i_ctime;
85 op_data->op_attr.ia_size = i_size_read(inode);
86 op_data->op_attr_blocks = inode->i_blocks;
87 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
88 op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
90 op_data->op_handle = *fh;
92 if (ll_i2info(inode)->lli_flags & LLIF_DATA_MODIFIED)
93 op_data->op_bias |= MDS_DATA_MODIFIED;
97 * Closes the IO epoch and packs all the attributes into @op_data for
100 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
101 struct obd_client_handle *och)
103 op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
104 ATTR_MTIME | ATTR_MTIME_SET |
105 ATTR_CTIME | ATTR_CTIME_SET;
107 if (!(och->och_flags & FMODE_WRITE))
110 if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
111 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
113 ll_ioepoch_close(inode, op_data, &och, 0);
116 ll_pack_inode2opdata(inode, op_data, &och->och_fh);
117 ll_prep_md_op_data(op_data, inode, NULL, NULL,
118 0, 0, LUSTRE_OPC_ANY, NULL);
121 static int ll_close_inode_openhandle(struct obd_export *md_exp,
123 struct obd_client_handle *och,
124 const __u64 *data_version)
126 struct obd_export *exp = ll_i2mdexp(inode);
127 struct md_op_data *op_data;
128 struct ptlrpc_request *req = NULL;
129 struct obd_device *obd = class_exp2obd(exp);
135 * XXX: in case of LMV, is this correct to access
138 CERROR("Invalid MDC connection handle %#llx\n",
139 ll_i2mdexp(inode)->exp_handle.h_cookie);
144 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
146 /* XXX We leak openhandle and request here. */
151 ll_prepare_close(inode, op_data, och);
153 /* Pass in data_version implies release. */
154 op_data->op_bias |= MDS_HSM_RELEASE;
155 op_data->op_data_version = *data_version;
156 op_data->op_lease_handle = och->och_lease_handle;
157 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
159 epoch_close = op_data->op_flags & MF_EPOCH_CLOSE;
160 rc = md_close(md_exp, op_data, och->och_mod, &req);
162 /* This close must have the epoch closed. */
163 LASSERT(epoch_close);
164 /* MDS has instructed us to obtain Size-on-MDS attribute from
165 * OSTs and send setattr to back to MDS.
167 rc = ll_som_update(inode, op_data);
169 CERROR("%s: inode "DFID" mdc Size-on-MDS update failed: rc = %d\n",
170 ll_i2mdexp(inode)->exp_obd->obd_name,
171 PFID(ll_inode2fid(inode)), rc);
175 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
176 ll_i2mdexp(inode)->exp_obd->obd_name,
177 PFID(ll_inode2fid(inode)), rc);
180 /* DATA_MODIFIED flag was successfully sent on close, cancel data
183 if (rc == 0 && (op_data->op_bias & MDS_DATA_MODIFIED)) {
184 struct ll_inode_info *lli = ll_i2info(inode);
186 spin_lock(&lli->lli_lock);
187 lli->lli_flags &= ~LLIF_DATA_MODIFIED;
188 spin_unlock(&lli->lli_lock);
192 rc = ll_objects_destroy(req, inode);
194 CERROR("inode %lu ll_objects destroy: rc = %d\n",
197 if (rc == 0 && op_data->op_bias & MDS_HSM_RELEASE) {
198 struct mdt_body *body;
200 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
201 if (!(body->valid & OBD_MD_FLRELEASED))
205 ll_finish_md_op_data(op_data);
208 if (exp_connect_som(exp) && !epoch_close &&
209 S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
210 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
212 md_clear_open_replay_data(md_exp, och);
213 /* Free @och if it is not waiting for DONE_WRITING. */
214 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
217 if (req) /* This is close request */
218 ptlrpc_req_finished(req);
222 int ll_md_real_close(struct inode *inode, fmode_t fmode)
224 struct ll_inode_info *lli = ll_i2info(inode);
225 struct obd_client_handle **och_p;
226 struct obd_client_handle *och;
230 if (fmode & FMODE_WRITE) {
231 och_p = &lli->lli_mds_write_och;
232 och_usecount = &lli->lli_open_fd_write_count;
233 } else if (fmode & FMODE_EXEC) {
234 och_p = &lli->lli_mds_exec_och;
235 och_usecount = &lli->lli_open_fd_exec_count;
237 LASSERT(fmode & FMODE_READ);
238 och_p = &lli->lli_mds_read_och;
239 och_usecount = &lli->lli_open_fd_read_count;
242 mutex_lock(&lli->lli_och_mutex);
243 if (*och_usecount > 0) {
244 /* There are still users of this handle, so skip
247 mutex_unlock(&lli->lli_och_mutex);
253 mutex_unlock(&lli->lli_och_mutex);
256 /* There might be a race and this handle may already
259 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
266 static int ll_md_close(struct obd_export *md_exp, struct inode *inode,
269 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
270 struct ll_inode_info *lli = ll_i2info(inode);
272 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
273 struct lustre_handle lockh;
274 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_OPEN} };
277 /* clear group lock, if present */
278 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
279 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
281 if (fd->fd_lease_och) {
284 /* Usually the lease is not released when the
285 * application crashed, we need to release here.
287 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
288 CDEBUG(rc ? D_ERROR : D_INODE,
289 "Clean up lease " DFID " %d/%d\n",
290 PFID(&lli->lli_fid), rc, lease_broken);
292 fd->fd_lease_och = NULL;
296 rc = ll_close_inode_openhandle(md_exp, inode, fd->fd_och, NULL);
301 /* Let's see if we have good enough OPEN lock on the file and if
302 * we can skip talking to MDS
305 mutex_lock(&lli->lli_och_mutex);
306 if (fd->fd_omode & FMODE_WRITE) {
308 LASSERT(lli->lli_open_fd_write_count);
309 lli->lli_open_fd_write_count--;
310 } else if (fd->fd_omode & FMODE_EXEC) {
312 LASSERT(lli->lli_open_fd_exec_count);
313 lli->lli_open_fd_exec_count--;
316 LASSERT(lli->lli_open_fd_read_count);
317 lli->lli_open_fd_read_count--;
319 mutex_unlock(&lli->lli_och_mutex);
321 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
322 LDLM_IBITS, &policy, lockmode, &lockh))
323 rc = ll_md_real_close(inode, fd->fd_omode);
326 LUSTRE_FPRIVATE(file) = NULL;
327 ll_file_data_put(fd);
332 /* While this returns an error code, fput() the caller does not, so we need
333 * to make every effort to clean up all of our state here. Also, applications
334 * rarely check close errors and even if an error is returned they will not
335 * re-try the close call.
337 int ll_file_release(struct inode *inode, struct file *file)
339 struct ll_file_data *fd;
340 struct ll_sb_info *sbi = ll_i2sbi(inode);
341 struct ll_inode_info *lli = ll_i2info(inode);
344 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
345 PFID(ll_inode2fid(inode)), inode);
347 if (!is_root_inode(inode))
348 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
349 fd = LUSTRE_FPRIVATE(file);
352 /* The last ref on @file, maybe not be the owner pid of statahead.
353 * Different processes can open the same dir, "ll_opendir_key" means:
354 * it is me that should stop the statahead thread.
356 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd &&
357 lli->lli_opendir_pid != 0)
358 ll_stop_statahead(inode, lli->lli_opendir_key);
360 if (is_root_inode(inode)) {
361 LUSTRE_FPRIVATE(file) = NULL;
362 ll_file_data_put(fd);
366 if (!S_ISDIR(inode->i_mode)) {
367 lov_read_and_clear_async_rc(lli->lli_clob);
368 lli->lli_async_rc = 0;
371 rc = ll_md_close(sbi->ll_md_exp, inode, file);
373 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
374 libcfs_debug_dumplog();
379 static int ll_intent_file_open(struct dentry *dentry, void *lmm,
380 int lmmsize, struct lookup_intent *itp)
382 struct inode *inode = d_inode(dentry);
383 struct ll_sb_info *sbi = ll_i2sbi(inode);
384 struct dentry *parent = dentry->d_parent;
385 const char *name = dentry->d_name.name;
386 const int len = dentry->d_name.len;
387 struct md_op_data *op_data;
388 struct ptlrpc_request *req;
389 __u32 opc = LUSTRE_OPC_ANY;
392 /* Usually we come here only for NFSD, and we want open lock. */
393 /* We can also get here if there was cached open handle in revalidate_it
394 * but it disappeared while we were getting from there to ll_file_open.
395 * But this means this file was closed and immediately opened which
396 * makes a good candidate for using OPEN lock
398 /* If lmmsize & lmm are not 0, we are just setting stripe info
399 * parameters. No need for the open lock
401 if (!lmm && lmmsize == 0) {
402 itp->it_flags |= MDS_OPEN_LOCK;
403 if (itp->it_flags & FMODE_WRITE)
404 opc = LUSTRE_OPC_CREATE;
407 op_data = ll_prep_md_op_data(NULL, d_inode(parent),
411 return PTR_ERR(op_data);
413 itp->it_flags |= MDS_OPEN_BY_FID;
414 rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
415 0 /*unused */, &req, ll_md_blocking_ast, 0);
416 ll_finish_md_op_data(op_data);
418 /* reason for keep own exit path - don`t flood log
419 * with messages with -ESTALE errors.
421 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
422 it_open_error(DISP_OPEN_OPEN, itp))
424 ll_release_openhandle(inode, itp);
428 if (it_disposition(itp, DISP_LOOKUP_NEG)) {
433 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
434 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
435 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
439 rc = ll_prep_inode(&inode, req, NULL, itp);
440 if (!rc && itp->d.lustre.it_lock_mode)
441 ll_set_lock_data(sbi->ll_md_exp, inode, itp, NULL);
444 ptlrpc_req_finished(req);
445 ll_intent_drop_lock(itp);
451 * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
452 * not believe attributes if a few ioepoch holders exist. Attributes for
453 * previous ioepoch if new one is opened are also skipped by MDS.
455 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
457 if (ioepoch && lli->lli_ioepoch != ioepoch) {
458 lli->lli_ioepoch = ioepoch;
459 CDEBUG(D_INODE, "Epoch %llu opened on "DFID"\n",
460 ioepoch, PFID(&lli->lli_fid));
464 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
465 struct obd_client_handle *och)
467 struct ptlrpc_request *req = it->d.lustre.it_data;
468 struct mdt_body *body;
470 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
471 och->och_fh = body->handle;
472 och->och_fid = body->fid1;
473 och->och_lease_handle.cookie = it->d.lustre.it_lock_handle;
474 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
475 och->och_flags = it->it_flags;
477 return md_set_open_replay_data(md_exp, och, it);
480 static int ll_local_open(struct file *file, struct lookup_intent *it,
481 struct ll_file_data *fd, struct obd_client_handle *och)
483 struct inode *inode = file_inode(file);
484 struct ll_inode_info *lli = ll_i2info(inode);
486 LASSERT(!LUSTRE_FPRIVATE(file));
491 struct ptlrpc_request *req = it->d.lustre.it_data;
492 struct mdt_body *body;
495 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
499 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
500 ll_ioepoch_open(lli, body->ioepoch);
503 LUSTRE_FPRIVATE(file) = fd;
504 ll_readahead_init(inode, &fd->fd_ras);
505 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
507 /* ll_cl_context initialize */
508 rwlock_init(&fd->fd_lock);
509 INIT_LIST_HEAD(&fd->fd_lccs);
514 /* Open a file, and (for the very first open) create objects on the OSTs at
515 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
516 * creation or open until ll_lov_setstripe() ioctl is called.
518 * If we already have the stripe MD locally then we don't request it in
519 * md_open(), by passing a lmm_size = 0.
521 * It is up to the application to ensure no other processes open this file
522 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
523 * used. We might be able to avoid races of that sort by getting lli_open_sem
524 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
525 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
527 int ll_file_open(struct inode *inode, struct file *file)
529 struct ll_inode_info *lli = ll_i2info(inode);
530 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
531 .it_flags = file->f_flags };
532 struct obd_client_handle **och_p = NULL;
533 __u64 *och_usecount = NULL;
534 struct ll_file_data *fd;
535 int rc = 0, opendir_set = 0;
537 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
538 PFID(ll_inode2fid(inode)), inode, file->f_flags);
540 it = file->private_data; /* XXX: compat macro */
541 file->private_data = NULL; /* prevent ll_local_open assertion */
543 fd = ll_file_data_get();
550 if (S_ISDIR(inode->i_mode)) {
551 spin_lock(&lli->lli_sa_lock);
552 if (!lli->lli_opendir_key && !lli->lli_sai &&
553 lli->lli_opendir_pid == 0) {
554 lli->lli_opendir_key = fd;
555 lli->lli_opendir_pid = current_pid();
558 spin_unlock(&lli->lli_sa_lock);
561 if (is_root_inode(inode)) {
562 LUSTRE_FPRIVATE(file) = fd;
566 if (!it || !it->d.lustre.it_disposition) {
567 /* Convert f_flags into access mode. We cannot use file->f_mode,
568 * because everything but O_ACCMODE mask was stripped from
571 if ((oit.it_flags + 1) & O_ACCMODE)
573 if (file->f_flags & O_TRUNC)
574 oit.it_flags |= FMODE_WRITE;
576 /* kernel only call f_op->open in dentry_open. filp_open calls
577 * dentry_open after call to open_namei that checks permissions.
578 * Only nfsd_open call dentry_open directly without checking
579 * permissions and because of that this code below is safe.
581 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
582 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
584 /* We do not want O_EXCL here, presumably we opened the file
585 * already? XXX - NFS implications?
587 oit.it_flags &= ~O_EXCL;
589 /* bug20584, if "it_flags" contains O_CREAT, the file will be
590 * created if necessary, then "IT_CREAT" should be set to keep
593 if (oit.it_flags & O_CREAT)
594 oit.it_op |= IT_CREAT;
600 /* Let's see if we have file open on MDS already. */
601 if (it->it_flags & FMODE_WRITE) {
602 och_p = &lli->lli_mds_write_och;
603 och_usecount = &lli->lli_open_fd_write_count;
604 } else if (it->it_flags & FMODE_EXEC) {
605 och_p = &lli->lli_mds_exec_och;
606 och_usecount = &lli->lli_open_fd_exec_count;
608 och_p = &lli->lli_mds_read_och;
609 och_usecount = &lli->lli_open_fd_read_count;
612 mutex_lock(&lli->lli_och_mutex);
613 if (*och_p) { /* Open handle is present */
614 if (it_disposition(it, DISP_OPEN_OPEN)) {
615 /* Well, there's extra open request that we do not need,
616 * let's close it somehow. This will decref request.
618 rc = it_open_error(DISP_OPEN_OPEN, it);
620 mutex_unlock(&lli->lli_och_mutex);
624 ll_release_openhandle(inode, it);
628 rc = ll_local_open(file, it, fd, NULL);
631 mutex_unlock(&lli->lli_och_mutex);
635 LASSERT(*och_usecount == 0);
636 if (!it->d.lustre.it_disposition) {
637 /* We cannot just request lock handle now, new ELC code
638 * means that one of other OPEN locks for this file
639 * could be cancelled, and since blocking ast handler
640 * would attempt to grab och_mutex as well, that would
641 * result in a deadlock
643 mutex_unlock(&lli->lli_och_mutex);
644 it->it_create_mode |= M_CHECK_STALE;
645 rc = ll_intent_file_open(file->f_path.dentry, NULL, 0, it);
646 it->it_create_mode &= ~M_CHECK_STALE;
652 *och_p = kzalloc(sizeof(struct obd_client_handle), GFP_NOFS);
660 /* md_intent_lock() didn't get a request ref if there was an
661 * open error, so don't do cleanup on the request here
664 /* XXX (green): Should not we bail out on any error here, not
667 rc = it_open_error(DISP_OPEN_OPEN, it);
671 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
672 "inode %p: disposition %x, status %d\n", inode,
673 it_disposition(it, ~0), it->d.lustre.it_status);
675 rc = ll_local_open(file, it, fd, *och_p);
679 mutex_unlock(&lli->lli_och_mutex);
682 /* Must do this outside lli_och_mutex lock to prevent deadlock where
683 * different kind of OPEN lock for this same inode gets cancelled
686 if (!S_ISREG(inode->i_mode))
689 if (!lli->lli_has_smd &&
690 (cl_is_lov_delay_create(file->f_flags) ||
691 (file->f_mode & FMODE_WRITE) == 0)) {
692 CDEBUG(D_INODE, "object creation was delayed\n");
695 cl_lov_delay_create_clear(&file->f_flags);
700 if (och_p && *och_p) {
705 mutex_unlock(&lli->lli_och_mutex);
708 if (opendir_set != 0)
709 ll_stop_statahead(inode, lli->lli_opendir_key);
710 ll_file_data_put(fd);
712 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
715 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
716 ptlrpc_req_finished(it->d.lustre.it_data);
717 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
723 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
724 struct ldlm_lock_desc *desc,
725 void *data, int flag)
728 struct lustre_handle lockh;
731 case LDLM_CB_BLOCKING:
732 ldlm_lock2handle(lock, &lockh);
733 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
735 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
739 case LDLM_CB_CANCELING:
747 * Acquire a lease and open the file.
749 static struct obd_client_handle *
750 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
753 struct lookup_intent it = { .it_op = IT_OPEN };
754 struct ll_sb_info *sbi = ll_i2sbi(inode);
755 struct md_op_data *op_data;
756 struct ptlrpc_request *req;
757 struct lustre_handle old_handle = { 0 };
758 struct obd_client_handle *och = NULL;
762 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
763 return ERR_PTR(-EINVAL);
766 struct ll_inode_info *lli = ll_i2info(inode);
767 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
768 struct obd_client_handle **och_p;
771 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
772 return ERR_PTR(-EPERM);
774 /* Get the openhandle of the file */
776 mutex_lock(&lli->lli_och_mutex);
777 if (fd->fd_lease_och) {
778 mutex_unlock(&lli->lli_och_mutex);
783 if (file->f_mode & FMODE_WRITE) {
784 LASSERT(lli->lli_mds_write_och);
785 och_p = &lli->lli_mds_write_och;
786 och_usecount = &lli->lli_open_fd_write_count;
788 LASSERT(lli->lli_mds_read_och);
789 och_p = &lli->lli_mds_read_och;
790 och_usecount = &lli->lli_open_fd_read_count;
792 if (*och_usecount == 1) {
799 mutex_unlock(&lli->lli_och_mutex);
800 if (rc < 0) /* more than 1 opener */
804 old_handle = fd->fd_och->och_fh;
807 och = kzalloc(sizeof(*och), GFP_NOFS);
809 return ERR_PTR(-ENOMEM);
811 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
812 LUSTRE_OPC_ANY, NULL);
813 if (IS_ERR(op_data)) {
814 rc = PTR_ERR(op_data);
818 /* To tell the MDT this openhandle is from the same owner */
819 op_data->op_handle = old_handle;
821 it.it_flags = fmode | open_flags;
822 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
823 rc = md_intent_lock(sbi->ll_md_exp, op_data, NULL, 0, &it, 0, &req,
824 ll_md_blocking_lease_ast,
825 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
826 * it can be cancelled which may mislead applications that the lease is
828 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
829 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
830 * doesn't deal with openhandle, so normal openhandle will be leaked.
832 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
833 ll_finish_md_op_data(op_data);
834 ptlrpc_req_finished(req);
838 if (it_disposition(&it, DISP_LOOKUP_NEG)) {
843 rc = it_open_error(DISP_OPEN_OPEN, &it);
847 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
848 ll_och_fill(sbi->ll_md_exp, &it, och);
850 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */ {
855 /* already get lease, handle lease lock */
856 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
857 if (it.d.lustre.it_lock_mode == 0 ||
858 it.d.lustre.it_lock_bits != MDS_INODELOCK_OPEN) {
859 /* open lock must return for lease */
860 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
861 PFID(ll_inode2fid(inode)), it.d.lustre.it_lock_mode,
862 it.d.lustre.it_lock_bits);
867 ll_intent_release(&it);
871 /* Cancel open lock */
872 if (it.d.lustre.it_lock_mode != 0) {
873 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
874 it.d.lustre.it_lock_mode);
875 it.d.lustre.it_lock_mode = 0;
876 och->och_lease_handle.cookie = 0ULL;
878 rc2 = ll_close_inode_openhandle(sbi->ll_md_exp, inode, och, NULL);
880 CERROR("%s: error closing file "DFID": %d\n",
881 ll_get_fsname(inode->i_sb, NULL, 0),
882 PFID(&ll_i2info(inode)->lli_fid), rc2);
883 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
885 ll_intent_release(&it);
892 * Release lease and close the file.
893 * It will check if the lease has ever broken.
895 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
898 struct ldlm_lock *lock;
899 bool cancelled = true;
902 lock = ldlm_handle2lock(&och->och_lease_handle);
904 lock_res_and_lock(lock);
905 cancelled = ldlm_is_cancel(lock);
906 unlock_res_and_lock(lock);
910 CDEBUG(D_INODE, "lease for " DFID " broken? %d\n",
911 PFID(&ll_i2info(inode)->lli_fid), cancelled);
914 ldlm_cli_cancel(&och->och_lease_handle, 0);
916 *lease_broken = cancelled;
918 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och,
923 /* Fills the obdo with the attributes for the lsm */
924 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
925 struct obdo *obdo, __u64 ioepoch, int dv_flags)
927 struct ptlrpc_request_set *set;
928 struct obd_info oinfo = { };
935 oinfo.oi_oa->o_oi = lsm->lsm_oi;
936 oinfo.oi_oa->o_mode = S_IFREG;
937 oinfo.oi_oa->o_ioepoch = ioepoch;
938 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
939 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
940 OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
941 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
942 OBD_MD_FLGROUP | OBD_MD_FLEPOCH |
943 OBD_MD_FLDATAVERSION;
944 if (dv_flags & (LL_DV_WR_FLUSH | LL_DV_RD_FLUSH)) {
945 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
946 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
947 if (dv_flags & LL_DV_WR_FLUSH)
948 oinfo.oi_oa->o_flags |= OBD_FL_FLUSH;
951 set = ptlrpc_prep_set();
953 CERROR("cannot allocate ptlrpc set: rc = %d\n", -ENOMEM);
956 rc = obd_getattr_async(exp, &oinfo, set);
958 rc = ptlrpc_set_wait(set);
959 ptlrpc_set_destroy(set);
962 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
963 OBD_MD_FLATIME | OBD_MD_FLMTIME |
964 OBD_MD_FLCTIME | OBD_MD_FLSIZE |
965 OBD_MD_FLDATAVERSION | OBD_MD_FLFLAGS);
966 if (dv_flags & LL_DV_WR_FLUSH &&
967 !(oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
968 oinfo.oi_oa->o_flags & OBD_FL_FLUSH))
975 * Performs the getattr on the inode and updates its fields.
976 * If @sync != 0, perform the getattr under the server-side lock.
978 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
979 __u64 ioepoch, int sync)
981 struct lov_stripe_md *lsm;
984 lsm = ccc_inode_lsm_get(inode);
985 rc = ll_lsm_getattr(lsm, ll_i2dtexp(inode),
986 obdo, ioepoch, sync ? LL_DV_RD_FLUSH : 0);
988 struct ost_id *oi = lsm ? &lsm->lsm_oi : &obdo->o_oi;
990 obdo_refresh_inode(inode, obdo, obdo->o_valid);
991 CDEBUG(D_INODE, "objid " DOSTID " size %llu, blocks %llu, blksize %lu\n",
992 POSTID(oi), i_size_read(inode),
993 (unsigned long long)inode->i_blocks,
994 1UL << inode->i_blkbits);
996 ccc_inode_lsm_put(inode, lsm);
1000 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1002 struct ll_inode_info *lli = ll_i2info(inode);
1003 struct cl_object *obj = lli->lli_clob;
1004 struct cl_attr *attr = vvp_env_thread_attr(env);
1010 ll_inode_size_lock(inode);
1012 /* merge timestamps the most recently obtained from mds with
1013 * timestamps obtained from osts
1015 LTIME_S(inode->i_atime) = lli->lli_atime;
1016 LTIME_S(inode->i_mtime) = lli->lli_mtime;
1017 LTIME_S(inode->i_ctime) = lli->lli_ctime;
1019 mtime = LTIME_S(inode->i_mtime);
1020 atime = LTIME_S(inode->i_atime);
1021 ctime = LTIME_S(inode->i_ctime);
1023 cl_object_attr_lock(obj);
1024 rc = cl_object_attr_get(env, obj, attr);
1025 cl_object_attr_unlock(obj);
1028 goto out_size_unlock;
1030 if (atime < attr->cat_atime)
1031 atime = attr->cat_atime;
1033 if (ctime < attr->cat_ctime)
1034 ctime = attr->cat_ctime;
1036 if (mtime < attr->cat_mtime)
1037 mtime = attr->cat_mtime;
1039 CDEBUG(D_VFSTRACE, DFID " updating i_size %llu\n",
1040 PFID(&lli->lli_fid), attr->cat_size);
1042 i_size_write(inode, attr->cat_size);
1044 inode->i_blocks = attr->cat_blocks;
1046 LTIME_S(inode->i_mtime) = mtime;
1047 LTIME_S(inode->i_atime) = atime;
1048 LTIME_S(inode->i_ctime) = ctime;
1051 ll_inode_size_unlock(inode);
1056 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
1059 struct obdo obdo = { 0 };
1062 rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, &obdo, 0, 0);
1064 st->st_size = obdo.o_size;
1065 st->st_blocks = obdo.o_blocks;
1066 st->st_mtime = obdo.o_mtime;
1067 st->st_atime = obdo.o_atime;
1068 st->st_ctime = obdo.o_ctime;
1073 static bool file_is_noatime(const struct file *file)
1075 const struct vfsmount *mnt = file->f_path.mnt;
1076 const struct inode *inode = file_inode(file);
1078 /* Adapted from file_accessed() and touch_atime().*/
1079 if (file->f_flags & O_NOATIME)
1082 if (inode->i_flags & S_NOATIME)
1085 if (IS_NOATIME(inode))
1088 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1091 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1094 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1100 void ll_io_init(struct cl_io *io, const struct file *file, int write)
1102 struct inode *inode = file_inode(file);
1104 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1106 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1107 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC ||
1108 file->f_flags & O_DIRECT ||
1111 io->ci_obj = ll_i2info(inode)->lli_clob;
1112 io->ci_lockreq = CILR_MAYBE;
1113 if (ll_file_nolock(file)) {
1114 io->ci_lockreq = CILR_NEVER;
1115 io->ci_no_srvlock = 1;
1116 } else if (file->f_flags & O_APPEND) {
1117 io->ci_lockreq = CILR_MANDATORY;
1120 io->ci_noatime = file_is_noatime(file);
1124 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1125 struct file *file, enum cl_io_type iot,
1126 loff_t *ppos, size_t count)
1128 struct ll_inode_info *lli = ll_i2info(file_inode(file));
1129 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1133 CDEBUG(D_VFSTRACE, "file: %s, type: %d ppos: %llu, count: %zd\n",
1134 file->f_path.dentry->d_name.name, iot, *ppos, count);
1137 io = vvp_env_thread_io(env);
1138 ll_io_init(io, file, iot == CIT_WRITE);
1140 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1141 struct vvp_io *vio = vvp_env_io(env);
1142 int write_mutex_locked = 0;
1144 vio->vui_fd = LUSTRE_FPRIVATE(file);
1145 vio->vui_io_subtype = args->via_io_subtype;
1147 switch (vio->vui_io_subtype) {
1149 vio->vui_iter = args->u.normal.via_iter;
1150 vio->vui_iocb = args->u.normal.via_iocb;
1151 if ((iot == CIT_WRITE) &&
1152 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1153 if (mutex_lock_interruptible(&lli->
1155 result = -ERESTARTSYS;
1158 write_mutex_locked = 1;
1160 down_read(&lli->lli_trunc_sem);
1163 vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1164 vio->u.splice.vui_flags = args->u.splice.via_flags;
1167 CERROR("Unknown IO type - %u\n", vio->vui_io_subtype);
1170 ll_cl_add(file, env, io);
1171 result = cl_io_loop(env, io);
1172 ll_cl_remove(file, env);
1173 if (args->via_io_subtype == IO_NORMAL)
1174 up_read(&lli->lli_trunc_sem);
1175 if (write_mutex_locked)
1176 mutex_unlock(&lli->lli_write_mutex);
1178 /* cl_io_rw_init() handled IO */
1179 result = io->ci_result;
1182 if (io->ci_nob > 0) {
1183 result = io->ci_nob;
1184 *ppos = io->u.ci_wr.wr.crw_pos;
1188 cl_io_fini(env, io);
1189 /* If any bit been read/written (result != 0), we just return
1190 * short read/write instead of restart io.
1192 if ((result == 0 || result == -ENODATA) && io->ci_need_restart) {
1193 CDEBUG(D_VFSTRACE, "Restart %s on %pD from %lld, count:%zd\n",
1194 iot == CIT_READ ? "read" : "write",
1195 file, *ppos, count);
1196 LASSERTF(io->ci_nob == 0, "%zd\n", io->ci_nob);
1200 if (iot == CIT_READ) {
1202 ll_stats_ops_tally(ll_i2sbi(file_inode(file)),
1203 LPROC_LL_READ_BYTES, result);
1204 } else if (iot == CIT_WRITE) {
1206 ll_stats_ops_tally(ll_i2sbi(file_inode(file)),
1207 LPROC_LL_WRITE_BYTES, result);
1208 fd->fd_write_failed = false;
1209 } else if (result != -ERESTARTSYS) {
1210 fd->fd_write_failed = true;
1213 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1218 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1221 struct vvp_io_args *args;
1225 env = cl_env_get(&refcheck);
1227 return PTR_ERR(env);
1229 args = ll_env_args(env, IO_NORMAL);
1230 args->u.normal.via_iter = to;
1231 args->u.normal.via_iocb = iocb;
1233 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1234 &iocb->ki_pos, iov_iter_count(to));
1235 cl_env_put(env, &refcheck);
1240 * Write to a file (through the page cache).
1242 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1245 struct vvp_io_args *args;
1249 env = cl_env_get(&refcheck);
1251 return PTR_ERR(env);
1253 args = ll_env_args(env, IO_NORMAL);
1254 args->u.normal.via_iter = from;
1255 args->u.normal.via_iocb = iocb;
1257 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1258 &iocb->ki_pos, iov_iter_count(from));
1259 cl_env_put(env, &refcheck);
1264 * Send file content (through pagecache) somewhere with helper
1266 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1267 struct pipe_inode_info *pipe, size_t count,
1271 struct vvp_io_args *args;
1275 env = cl_env_get(&refcheck);
1277 return PTR_ERR(env);
1279 args = ll_env_args(env, IO_SPLICE);
1280 args->u.splice.via_pipe = pipe;
1281 args->u.splice.via_flags = flags;
1283 result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1284 cl_env_put(env, &refcheck);
1288 static int ll_lov_recreate(struct inode *inode, struct ost_id *oi, u32 ost_idx)
1290 struct obd_export *exp = ll_i2dtexp(inode);
1291 struct obd_trans_info oti = { 0 };
1292 struct obdo *oa = NULL;
1295 struct lov_stripe_md *lsm = NULL, *lsm2;
1297 oa = kmem_cache_zalloc(obdo_cachep, GFP_NOFS);
1301 lsm = ccc_inode_lsm_get(inode);
1302 if (!lsm_has_objects(lsm)) {
1307 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1308 (lsm->lsm_stripe_count));
1310 lsm2 = libcfs_kvzalloc(lsm_size, GFP_NOFS);
1317 oa->o_nlink = ost_idx;
1318 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1319 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1320 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1321 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1322 obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
1323 memcpy(lsm2, lsm, lsm_size);
1324 ll_inode_size_lock(inode);
1325 rc = obd_create(NULL, exp, oa, &lsm2, &oti);
1326 ll_inode_size_unlock(inode);
1331 ccc_inode_lsm_put(inode, lsm);
1332 kmem_cache_free(obdo_cachep, oa);
1336 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1338 struct ll_recreate_obj ucreat;
1341 if (!capable(CFS_CAP_SYS_ADMIN))
1344 if (copy_from_user(&ucreat, (struct ll_recreate_obj __user *)arg,
1348 ostid_set_seq_mdt0(&oi);
1349 ostid_set_id(&oi, ucreat.lrc_id);
1350 return ll_lov_recreate(inode, &oi, ucreat.lrc_ost_idx);
1353 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1359 if (!capable(CFS_CAP_SYS_ADMIN))
1362 if (copy_from_user(&fid, (struct lu_fid __user *)arg, sizeof(fid)))
1365 fid_to_ostid(&fid, &oi);
1366 ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1367 return ll_lov_recreate(inode, &oi, ost_idx);
1370 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
1371 __u64 flags, struct lov_user_md *lum,
1374 struct lov_stripe_md *lsm = NULL;
1375 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1378 lsm = ccc_inode_lsm_get(inode);
1380 ccc_inode_lsm_put(inode, lsm);
1381 CDEBUG(D_IOCTL, "stripe already exists for inode "DFID"\n",
1382 PFID(ll_inode2fid(inode)));
1387 ll_inode_size_lock(inode);
1388 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
1391 rc = oit.d.lustre.it_status;
1395 ll_release_openhandle(inode, &oit);
1398 ll_inode_size_unlock(inode);
1399 ll_intent_release(&oit);
1400 ccc_inode_lsm_put(inode, lsm);
1404 ptlrpc_req_finished((struct ptlrpc_request *)oit.d.lustre.it_data);
1408 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1409 struct lov_mds_md **lmmp, int *lmm_size,
1410 struct ptlrpc_request **request)
1412 struct ll_sb_info *sbi = ll_i2sbi(inode);
1413 struct mdt_body *body;
1414 struct lov_mds_md *lmm = NULL;
1415 struct ptlrpc_request *req = NULL;
1416 struct md_op_data *op_data;
1419 rc = ll_get_default_mdsize(sbi, &lmmsize);
1423 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1424 strlen(filename), lmmsize,
1425 LUSTRE_OPC_ANY, NULL);
1426 if (IS_ERR(op_data))
1427 return PTR_ERR(op_data);
1429 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1430 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1431 ll_finish_md_op_data(op_data);
1433 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
1438 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1440 lmmsize = body->eadatasize;
1442 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1448 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1450 if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1451 (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1457 * This is coming from the MDS, so is probably in
1458 * little endian. We convert it to host endian before
1459 * passing it to userspace.
1461 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
1464 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
1465 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
1468 /* if function called for directory - we should
1469 * avoid swab not existent lsm objects
1471 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1472 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1473 if (S_ISREG(body->mode))
1474 lustre_swab_lov_user_md_objects(
1475 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1477 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1478 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1479 if (S_ISREG(body->mode))
1480 lustre_swab_lov_user_md_objects(
1481 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1488 *lmm_size = lmmsize;
1493 static int ll_lov_setea(struct inode *inode, struct file *file,
1496 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1497 struct lov_user_md *lump;
1498 int lum_size = sizeof(struct lov_user_md) +
1499 sizeof(struct lov_user_ost_data);
1502 if (!capable(CFS_CAP_SYS_ADMIN))
1505 lump = libcfs_kvzalloc(lum_size, GFP_NOFS);
1509 if (copy_from_user(lump, (struct lov_user_md __user *)arg, lum_size)) {
1514 rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, lump,
1516 cl_lov_delay_create_clear(&file->f_flags);
1522 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1525 struct lov_user_md_v3 lumv3;
1526 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1527 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1528 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1530 __u64 flags = FMODE_WRITE;
1532 /* first try with v1 which is smaller than v3 */
1533 lum_size = sizeof(struct lov_user_md_v1);
1534 if (copy_from_user(lumv1, lumv1p, lum_size))
1537 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1538 lum_size = sizeof(struct lov_user_md_v3);
1539 if (copy_from_user(&lumv3, lumv3p, lum_size))
1543 rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, lumv1,
1545 cl_lov_delay_create_clear(&file->f_flags);
1547 struct lov_stripe_md *lsm;
1550 put_user(0, &lumv1p->lmm_stripe_count);
1552 ll_layout_refresh(inode, &gen);
1553 lsm = ccc_inode_lsm_get(inode);
1554 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1555 0, lsm, (void __user *)arg);
1556 ccc_inode_lsm_put(inode, lsm);
1561 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1563 struct lov_stripe_md *lsm;
1566 lsm = ccc_inode_lsm_get(inode);
1568 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
1569 lsm, (void __user *)arg);
1570 ccc_inode_lsm_put(inode, lsm);
1575 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1577 struct ll_inode_info *lli = ll_i2info(inode);
1578 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1579 struct ll_grouplock grouplock;
1583 CWARN("group id for group lock must not be 0\n");
1587 if (ll_file_nolock(file))
1590 spin_lock(&lli->lli_lock);
1591 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1592 CWARN("group lock already existed with gid %lu\n",
1593 fd->fd_grouplock.lg_gid);
1594 spin_unlock(&lli->lli_lock);
1597 LASSERT(!fd->fd_grouplock.lg_lock);
1598 spin_unlock(&lli->lli_lock);
1600 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
1601 arg, (file->f_flags & O_NONBLOCK), &grouplock);
1605 spin_lock(&lli->lli_lock);
1606 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1607 spin_unlock(&lli->lli_lock);
1608 CERROR("another thread just won the race\n");
1609 cl_put_grouplock(&grouplock);
1613 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1614 fd->fd_grouplock = grouplock;
1615 spin_unlock(&lli->lli_lock);
1617 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1621 static int ll_put_grouplock(struct inode *inode, struct file *file,
1624 struct ll_inode_info *lli = ll_i2info(inode);
1625 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1626 struct ll_grouplock grouplock;
1628 spin_lock(&lli->lli_lock);
1629 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1630 spin_unlock(&lli->lli_lock);
1631 CWARN("no group lock held\n");
1634 LASSERT(fd->fd_grouplock.lg_lock);
1636 if (fd->fd_grouplock.lg_gid != arg) {
1637 CWARN("group lock %lu doesn't match current id %lu\n",
1638 arg, fd->fd_grouplock.lg_gid);
1639 spin_unlock(&lli->lli_lock);
1643 grouplock = fd->fd_grouplock;
1644 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1645 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1646 spin_unlock(&lli->lli_lock);
1648 cl_put_grouplock(&grouplock);
1649 CDEBUG(D_INFO, "group lock %lu released\n", arg);
1654 * Close inode open handle
1656 * \param inode [in] inode in question
1657 * \param it [in,out] intent which contains open info and result
1660 * \retval <0 failure
1662 int ll_release_openhandle(struct inode *inode, struct lookup_intent *it)
1664 struct obd_client_handle *och;
1669 /* Root ? Do nothing. */
1670 if (is_root_inode(inode))
1673 /* No open handle to close? Move away */
1674 if (!it_disposition(it, DISP_OPEN_OPEN))
1677 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1679 och = kzalloc(sizeof(*och), GFP_NOFS);
1685 ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
1687 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1690 /* this one is in place of ll_file_open */
1691 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1692 ptlrpc_req_finished(it->d.lustre.it_data);
1693 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1699 * Get size for inode for which FIEMAP mapping is requested.
1700 * Make the FIEMAP get_info call and returns the result.
1702 static int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1705 struct obd_export *exp = ll_i2dtexp(inode);
1706 struct lov_stripe_md *lsm = NULL;
1707 struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1708 __u32 vallen = num_bytes;
1711 /* Checks for fiemap flags */
1712 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1713 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1717 /* Check for FIEMAP_FLAG_SYNC */
1718 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1719 rc = filemap_fdatawrite(inode->i_mapping);
1724 lsm = ccc_inode_lsm_get(inode);
1728 /* If the stripe_count > 1 and the application does not understand
1729 * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1731 if (lsm->lsm_stripe_count > 1 &&
1732 !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1737 fm_key.oa.o_oi = lsm->lsm_oi;
1738 fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1740 if (i_size_read(inode) == 0) {
1741 rc = ll_glimpse_size(inode);
1746 obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE);
1747 obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid);
1748 /* If filesize is 0, then there would be no objects for mapping */
1749 if (fm_key.oa.o_size == 0) {
1750 fiemap->fm_mapped_extents = 0;
1755 memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1757 rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen,
1760 CERROR("obd_get_info failed: rc = %d\n", rc);
1763 ccc_inode_lsm_put(inode, lsm);
1767 int ll_fid2path(struct inode *inode, void __user *arg)
1769 struct obd_export *exp = ll_i2mdexp(inode);
1770 const struct getinfo_fid2path __user *gfin = arg;
1771 struct getinfo_fid2path *gfout;
1776 if (!capable(CFS_CAP_DAC_READ_SEARCH) &&
1777 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
1780 /* Only need to get the buflen */
1781 if (get_user(pathlen, &gfin->gf_pathlen))
1784 if (pathlen > PATH_MAX)
1787 outsize = sizeof(*gfout) + pathlen;
1789 gfout = kzalloc(outsize, GFP_NOFS);
1793 if (copy_from_user(gfout, arg, sizeof(*gfout))) {
1798 /* Call mdc_iocontrol */
1799 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1803 if (copy_to_user(arg, gfout, outsize))
1811 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1813 struct ll_user_fiemap *fiemap_s;
1814 size_t num_bytes, ret_bytes;
1815 unsigned int extent_count;
1818 /* Get the extent count so we can calculate the size of
1819 * required fiemap buffer
1821 if (get_user(extent_count,
1822 &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1826 (SIZE_MAX - sizeof(*fiemap_s)) / sizeof(struct ll_fiemap_extent))
1828 num_bytes = sizeof(*fiemap_s) + (extent_count *
1829 sizeof(struct ll_fiemap_extent));
1831 fiemap_s = libcfs_kvzalloc(num_bytes, GFP_NOFS);
1835 /* get the fiemap value */
1836 if (copy_from_user(fiemap_s, (struct ll_user_fiemap __user *)arg,
1837 sizeof(*fiemap_s))) {
1842 /* If fm_extent_count is non-zero, read the first extent since
1843 * it is used to calculate end_offset and device from previous
1847 if (copy_from_user(&fiemap_s->fm_extents[0],
1848 (char __user *)arg + sizeof(*fiemap_s),
1849 sizeof(struct ll_fiemap_extent))) {
1855 rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1859 ret_bytes = sizeof(struct ll_user_fiemap);
1861 if (extent_count != 0)
1862 ret_bytes += (fiemap_s->fm_mapped_extents *
1863 sizeof(struct ll_fiemap_extent));
1865 if (copy_to_user((void __user *)arg, fiemap_s, ret_bytes))
1874 * Read the data_version for inode.
1876 * This value is computed using stripe object version on OST.
1877 * Version is computed using server side locking.
1879 * @param sync if do sync on the OST side;
1881 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
1882 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
1884 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
1886 struct lov_stripe_md *lsm = NULL;
1887 struct ll_sb_info *sbi = ll_i2sbi(inode);
1888 struct obdo *obdo = NULL;
1891 /* If no stripe, we consider version is 0. */
1892 lsm = ccc_inode_lsm_get(inode);
1893 if (!lsm_has_objects(lsm)) {
1895 CDEBUG(D_INODE, "No object for inode\n");
1900 obdo = kzalloc(sizeof(*obdo), GFP_NOFS);
1906 rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, obdo, 0, flags);
1908 if (!(obdo->o_valid & OBD_MD_FLDATAVERSION))
1911 *data_version = obdo->o_data_version;
1916 ccc_inode_lsm_put(inode, lsm);
1921 * Trigger a HSM release request for the provided inode.
1923 int ll_hsm_release(struct inode *inode)
1925 struct cl_env_nest nest;
1927 struct obd_client_handle *och = NULL;
1928 __u64 data_version = 0;
1931 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
1932 ll_get_fsname(inode->i_sb, NULL, 0),
1933 PFID(&ll_i2info(inode)->lli_fid));
1935 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
1941 /* Grab latest data_version and [am]time values */
1942 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
1946 env = cl_env_nested_get(&nest);
1952 ll_merge_attr(env, inode);
1953 cl_env_nested_put(&nest, env);
1955 /* Release the file.
1956 * NB: lease lock handle is released in mdc_hsm_release_pack() because
1957 * we still need it to pack l_remote_handle to MDT.
1959 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och,
1964 if (och && !IS_ERR(och)) /* close the file */
1965 ll_lease_close(och, inode, NULL);
1970 struct ll_swap_stack {
1971 struct iattr ia1, ia2;
1973 struct inode *inode1, *inode2;
1974 bool check_dv1, check_dv2;
1977 static int ll_swap_layouts(struct file *file1, struct file *file2,
1978 struct lustre_swap_layouts *lsl)
1980 struct mdc_swap_layouts msl;
1981 struct md_op_data *op_data;
1984 struct ll_swap_stack *llss = NULL;
1987 llss = kzalloc(sizeof(*llss), GFP_NOFS);
1991 llss->inode1 = file_inode(file1);
1992 llss->inode2 = file_inode(file2);
1994 if (!S_ISREG(llss->inode2->i_mode)) {
1999 if (inode_permission(llss->inode1, MAY_WRITE) ||
2000 inode_permission(llss->inode2, MAY_WRITE)) {
2005 if (llss->inode2->i_sb != llss->inode1->i_sb) {
2010 /* we use 2 bool because it is easier to swap than 2 bits */
2011 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2012 llss->check_dv1 = true;
2014 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2015 llss->check_dv2 = true;
2017 /* we cannot use lsl->sl_dvX directly because we may swap them */
2018 llss->dv1 = lsl->sl_dv1;
2019 llss->dv2 = lsl->sl_dv2;
2021 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2022 if (rc == 0) /* same file, done! */ {
2027 if (rc < 0) { /* sequentialize it */
2028 swap(llss->inode1, llss->inode2);
2030 swap(llss->dv1, llss->dv2);
2031 swap(llss->check_dv1, llss->check_dv2);
2035 if (gid != 0) { /* application asks to flush dirty cache */
2036 rc = ll_get_grouplock(llss->inode1, file1, gid);
2040 rc = ll_get_grouplock(llss->inode2, file2, gid);
2042 ll_put_grouplock(llss->inode1, file1, gid);
2047 /* to be able to restore mtime and atime after swap
2048 * we need to first save them
2051 (SWAP_LAYOUTS_KEEP_MTIME | SWAP_LAYOUTS_KEEP_ATIME)) {
2052 llss->ia1.ia_mtime = llss->inode1->i_mtime;
2053 llss->ia1.ia_atime = llss->inode1->i_atime;
2054 llss->ia1.ia_valid = ATTR_MTIME | ATTR_ATIME;
2055 llss->ia2.ia_mtime = llss->inode2->i_mtime;
2056 llss->ia2.ia_atime = llss->inode2->i_atime;
2057 llss->ia2.ia_valid = ATTR_MTIME | ATTR_ATIME;
2060 /* ultimate check, before swapping the layouts we check if
2061 * dataversion has changed (if requested)
2063 if (llss->check_dv1) {
2064 rc = ll_data_version(llss->inode1, &dv, 0);
2067 if (dv != llss->dv1) {
2073 if (llss->check_dv2) {
2074 rc = ll_data_version(llss->inode2, &dv, 0);
2077 if (dv != llss->dv2) {
2083 /* struct md_op_data is used to send the swap args to the mdt
2084 * only flags is missing, so we use struct mdc_swap_layouts
2085 * through the md_op_data->op_data
2087 /* flags from user space have to be converted before they are send to
2088 * server, no flag is sent today, they are only used on the client
2092 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2093 0, LUSTRE_OPC_ANY, &msl);
2094 if (IS_ERR(op_data)) {
2095 rc = PTR_ERR(op_data);
2099 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2100 sizeof(*op_data), op_data, NULL);
2101 ll_finish_md_op_data(op_data);
2105 ll_put_grouplock(llss->inode2, file2, gid);
2106 ll_put_grouplock(llss->inode1, file1, gid);
2109 /* rc can be set from obd_iocontrol() or from a GOTO(putgl, ...) */
2113 /* clear useless flags */
2114 if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_MTIME)) {
2115 llss->ia1.ia_valid &= ~ATTR_MTIME;
2116 llss->ia2.ia_valid &= ~ATTR_MTIME;
2119 if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_ATIME)) {
2120 llss->ia1.ia_valid &= ~ATTR_ATIME;
2121 llss->ia2.ia_valid &= ~ATTR_ATIME;
2124 /* update time if requested */
2126 if (llss->ia2.ia_valid != 0) {
2127 inode_lock(llss->inode1);
2128 rc = ll_setattr(file1->f_path.dentry, &llss->ia2);
2129 inode_unlock(llss->inode1);
2132 if (llss->ia1.ia_valid != 0) {
2135 inode_lock(llss->inode2);
2136 rc1 = ll_setattr(file2->f_path.dentry, &llss->ia1);
2137 inode_unlock(llss->inode2);
2148 static int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2150 struct md_op_data *op_data;
2153 /* Detect out-of range masks */
2154 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2157 /* Non-root users are forbidden to set or clear flags which are
2158 * NOT defined in HSM_USER_MASK.
2160 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2161 !capable(CFS_CAP_SYS_ADMIN))
2164 /* Detect out-of range archive id */
2165 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2166 (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
2169 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2170 LUSTRE_OPC_ANY, hss);
2171 if (IS_ERR(op_data))
2172 return PTR_ERR(op_data);
2174 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
2175 sizeof(*op_data), op_data, NULL);
2177 ll_finish_md_op_data(op_data);
2182 static int ll_hsm_import(struct inode *inode, struct file *file,
2183 struct hsm_user_import *hui)
2185 struct hsm_state_set *hss = NULL;
2186 struct iattr *attr = NULL;
2189 if (!S_ISREG(inode->i_mode))
2193 hss = kzalloc(sizeof(*hss), GFP_NOFS);
2197 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2198 hss->hss_archive_id = hui->hui_archive_id;
2199 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2200 rc = ll_hsm_state_set(inode, hss);
2204 attr = kzalloc(sizeof(*attr), GFP_NOFS);
2210 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2211 attr->ia_mode |= S_IFREG;
2212 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2213 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2214 attr->ia_size = hui->hui_size;
2215 attr->ia_mtime.tv_sec = hui->hui_mtime;
2216 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2217 attr->ia_atime.tv_sec = hui->hui_atime;
2218 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2220 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2221 ATTR_UID | ATTR_GID |
2222 ATTR_MTIME | ATTR_MTIME_SET |
2223 ATTR_ATIME | ATTR_ATIME_SET;
2227 rc = ll_setattr_raw(file->f_path.dentry, attr, true);
2231 inode_unlock(inode);
2240 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2242 struct inode *inode = file_inode(file);
2243 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2246 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),cmd=%x\n",
2247 PFID(ll_inode2fid(inode)), inode, cmd);
2248 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
2250 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
2251 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
2255 case LL_IOC_GETFLAGS:
2256 /* Get the current value of the file flags */
2257 return put_user(fd->fd_flags, (int __user *)arg);
2258 case LL_IOC_SETFLAGS:
2259 case LL_IOC_CLRFLAGS:
2260 /* Set or clear specific file flags */
2261 /* XXX This probably needs checks to ensure the flags are
2262 * not abused, and to handle any flag side effects.
2264 if (get_user(flags, (int __user *)arg))
2267 if (cmd == LL_IOC_SETFLAGS) {
2268 if ((flags & LL_FILE_IGNORE_LOCK) &&
2269 !(file->f_flags & O_DIRECT)) {
2270 CERROR("%s: unable to disable locking on non-O_DIRECT file\n",
2275 fd->fd_flags |= flags;
2277 fd->fd_flags &= ~flags;
2280 case LL_IOC_LOV_SETSTRIPE:
2281 return ll_lov_setstripe(inode, file, arg);
2282 case LL_IOC_LOV_SETEA:
2283 return ll_lov_setea(inode, file, arg);
2284 case LL_IOC_LOV_SWAP_LAYOUTS: {
2286 struct lustre_swap_layouts lsl;
2288 if (copy_from_user(&lsl, (char __user *)arg,
2289 sizeof(struct lustre_swap_layouts)))
2292 if ((file->f_flags & O_ACCMODE) == 0) /* O_RDONLY */
2295 file2 = fget(lsl.sl_fd);
2300 if ((file2->f_flags & O_ACCMODE) != 0) /* O_WRONLY or O_RDWR */
2301 rc = ll_swap_layouts(file, file2, &lsl);
2305 case LL_IOC_LOV_GETSTRIPE:
2306 return ll_lov_getstripe(inode, arg);
2307 case LL_IOC_RECREATE_OBJ:
2308 return ll_lov_recreate_obj(inode, arg);
2309 case LL_IOC_RECREATE_FID:
2310 return ll_lov_recreate_fid(inode, arg);
2311 case FSFILT_IOC_FIEMAP:
2312 return ll_ioctl_fiemap(inode, arg);
2313 case FSFILT_IOC_GETFLAGS:
2314 case FSFILT_IOC_SETFLAGS:
2315 return ll_iocontrol(inode, file, cmd, arg);
2316 case FSFILT_IOC_GETVERSION_OLD:
2317 case FSFILT_IOC_GETVERSION:
2318 return put_user(inode->i_generation, (int __user *)arg);
2319 case LL_IOC_GROUP_LOCK:
2320 return ll_get_grouplock(inode, file, arg);
2321 case LL_IOC_GROUP_UNLOCK:
2322 return ll_put_grouplock(inode, file, arg);
2323 case IOC_OBD_STATFS:
2324 return ll_obd_statfs(inode, (void __user *)arg);
2326 /* We need to special case any other ioctls we want to handle,
2327 * to send them to the MDS/OST as appropriate and to properly
2328 * network encode the arg field.
2329 case FSFILT_IOC_SETVERSION_OLD:
2330 case FSFILT_IOC_SETVERSION:
2332 case LL_IOC_FLUSHCTX:
2333 return ll_flush_ctx(inode);
2334 case LL_IOC_PATH2FID: {
2335 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2336 sizeof(struct lu_fid)))
2341 case OBD_IOC_FID2PATH:
2342 return ll_fid2path(inode, (void __user *)arg);
2343 case LL_IOC_DATA_VERSION: {
2344 struct ioc_data_version idv;
2347 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
2350 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
2351 rc = ll_data_version(inode, &idv.idv_version, idv.idv_flags);
2352 if (rc == 0 && copy_to_user((char __user *)arg, &idv,
2359 case LL_IOC_GET_MDTIDX: {
2362 mdtidx = ll_get_mdt_idx(inode);
2366 if (put_user(mdtidx, (int __user *)arg))
2371 case OBD_IOC_GETDTNAME:
2372 case OBD_IOC_GETMDNAME:
2373 return ll_get_obd_name(inode, cmd, arg);
2374 case LL_IOC_HSM_STATE_GET: {
2375 struct md_op_data *op_data;
2376 struct hsm_user_state *hus;
2379 hus = kzalloc(sizeof(*hus), GFP_NOFS);
2383 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2384 LUSTRE_OPC_ANY, hus);
2385 if (IS_ERR(op_data)) {
2387 return PTR_ERR(op_data);
2390 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2393 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
2396 ll_finish_md_op_data(op_data);
2400 case LL_IOC_HSM_STATE_SET: {
2401 struct hsm_state_set *hss;
2404 hss = memdup_user((char __user *)arg, sizeof(*hss));
2406 return PTR_ERR(hss);
2408 rc = ll_hsm_state_set(inode, hss);
2413 case LL_IOC_HSM_ACTION: {
2414 struct md_op_data *op_data;
2415 struct hsm_current_action *hca;
2418 hca = kzalloc(sizeof(*hca), GFP_NOFS);
2422 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2423 LUSTRE_OPC_ANY, hca);
2424 if (IS_ERR(op_data)) {
2426 return PTR_ERR(op_data);
2429 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2432 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
2435 ll_finish_md_op_data(op_data);
2439 case LL_IOC_SET_LEASE: {
2440 struct ll_inode_info *lli = ll_i2info(inode);
2441 struct obd_client_handle *och = NULL;
2447 if (!(file->f_mode & FMODE_WRITE))
2452 if (!(file->f_mode & FMODE_READ))
2457 mutex_lock(&lli->lli_och_mutex);
2458 if (fd->fd_lease_och) {
2459 och = fd->fd_lease_och;
2460 fd->fd_lease_och = NULL;
2462 mutex_unlock(&lli->lli_och_mutex);
2465 mode = och->och_flags &
2466 (FMODE_READ|FMODE_WRITE);
2467 rc = ll_lease_close(och, inode, &lease_broken);
2468 if (rc == 0 && lease_broken)
2474 /* return the type of lease or error */
2475 return rc < 0 ? rc : (int)mode;
2480 CDEBUG(D_INODE, "Set lease with mode %d\n", mode);
2482 /* apply for lease */
2483 och = ll_lease_open(inode, file, mode, 0);
2485 return PTR_ERR(och);
2488 mutex_lock(&lli->lli_och_mutex);
2489 if (!fd->fd_lease_och) {
2490 fd->fd_lease_och = och;
2493 mutex_unlock(&lli->lli_och_mutex);
2495 /* impossible now that only excl is supported for now */
2496 ll_lease_close(och, inode, &lease_broken);
2501 case LL_IOC_GET_LEASE: {
2502 struct ll_inode_info *lli = ll_i2info(inode);
2503 struct ldlm_lock *lock = NULL;
2506 mutex_lock(&lli->lli_och_mutex);
2507 if (fd->fd_lease_och) {
2508 struct obd_client_handle *och = fd->fd_lease_och;
2510 lock = ldlm_handle2lock(&och->och_lease_handle);
2512 lock_res_and_lock(lock);
2513 if (!ldlm_is_cancel(lock))
2514 rc = och->och_flags &
2515 (FMODE_READ | FMODE_WRITE);
2516 unlock_res_and_lock(lock);
2517 LDLM_LOCK_PUT(lock);
2520 mutex_unlock(&lli->lli_och_mutex);
2523 case LL_IOC_HSM_IMPORT: {
2524 struct hsm_user_import *hui;
2526 hui = memdup_user((void __user *)arg, sizeof(*hui));
2528 return PTR_ERR(hui);
2530 rc = ll_hsm_import(inode, file, hui);
2538 if (ll_iocontrol_call(inode, file, cmd, arg, &err) ==
2542 return obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
2543 (void __user *)arg);
2548 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
2550 struct inode *inode = file_inode(file);
2551 loff_t retval, eof = 0;
2553 retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
2554 (origin == SEEK_CUR) ? file->f_pos : 0);
2555 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
2556 PFID(ll_inode2fid(inode)), inode, retval, retval, origin);
2557 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
2559 if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
2560 retval = ll_glimpse_size(inode);
2563 eof = i_size_read(inode);
2566 retval = generic_file_llseek_size(file, offset, origin,
2567 ll_file_maxbytes(inode), eof);
2571 static int ll_flush(struct file *file, fl_owner_t id)
2573 struct inode *inode = file_inode(file);
2574 struct ll_inode_info *lli = ll_i2info(inode);
2575 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2578 LASSERT(!S_ISDIR(inode->i_mode));
2580 /* catch async errors that were recorded back when async writeback
2581 * failed for pages in this mapping.
2583 rc = lli->lli_async_rc;
2584 lli->lli_async_rc = 0;
2585 err = lov_read_and_clear_async_rc(lli->lli_clob);
2589 /* The application has been told about write failure already.
2590 * Do not report failure again.
2592 if (fd->fd_write_failed)
2594 return rc ? -EIO : 0;
2598 * Called to make sure a portion of file has been written out.
2599 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
2601 * Return how many pages have been written.
2603 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
2604 enum cl_fsync_mode mode, int ignore_layout)
2606 struct cl_env_nest nest;
2609 struct cl_fsync_io *fio;
2612 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2613 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2616 env = cl_env_nested_get(&nest);
2618 return PTR_ERR(env);
2620 io = vvp_env_thread_io(env);
2621 io->ci_obj = ll_i2info(inode)->lli_clob;
2622 io->ci_ignore_layout = ignore_layout;
2624 /* initialize parameters for sync */
2625 fio = &io->u.ci_fsync;
2626 fio->fi_start = start;
2628 fio->fi_fid = ll_inode2fid(inode);
2629 fio->fi_mode = mode;
2630 fio->fi_nr_written = 0;
2632 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2633 result = cl_io_loop(env, io);
2635 result = io->ci_result;
2637 result = fio->fi_nr_written;
2638 cl_io_fini(env, io);
2639 cl_env_nested_put(&nest, env);
2644 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2646 struct inode *inode = file_inode(file);
2647 struct ll_inode_info *lli = ll_i2info(inode);
2648 struct ptlrpc_request *req;
2651 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2652 PFID(ll_inode2fid(inode)), inode);
2653 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2655 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2658 /* catch async errors that were recorded back when async writeback
2659 * failed for pages in this mapping.
2661 if (!S_ISDIR(inode->i_mode)) {
2662 err = lli->lli_async_rc;
2663 lli->lli_async_rc = 0;
2666 err = lov_read_and_clear_async_rc(lli->lli_clob);
2671 err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
2675 ptlrpc_req_finished(req);
2677 if (S_ISREG(inode->i_mode)) {
2678 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2680 err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
2681 if (rc == 0 && err < 0)
2684 fd->fd_write_failed = true;
2686 fd->fd_write_failed = false;
2689 inode_unlock(inode);
2694 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2696 struct inode *inode = file_inode(file);
2697 struct ll_sb_info *sbi = ll_i2sbi(inode);
2698 struct ldlm_enqueue_info einfo = {
2699 .ei_type = LDLM_FLOCK,
2700 .ei_cb_cp = ldlm_flock_completion_ast,
2701 .ei_cbdata = file_lock,
2703 struct md_op_data *op_data;
2704 struct lustre_handle lockh = {0};
2705 ldlm_policy_data_t flock = { {0} };
2710 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
2711 PFID(ll_inode2fid(inode)), file_lock);
2713 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2715 if (file_lock->fl_flags & FL_FLOCK)
2716 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2717 else if (!(file_lock->fl_flags & FL_POSIX))
2720 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2721 flock.l_flock.pid = file_lock->fl_pid;
2722 flock.l_flock.start = file_lock->fl_start;
2723 flock.l_flock.end = file_lock->fl_end;
2725 /* Somewhat ugly workaround for svc lockd.
2726 * lockd installs custom fl_lmops->lm_compare_owner that checks
2727 * for the fl_owner to be the same (which it always is on local node
2728 * I guess between lockd processes) and then compares pid.
2729 * As such we assign pid to the owner field to make it all work,
2730 * conflict with normal locks is unlikely since pid space and
2731 * pointer space for current->files are not intersecting
2733 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
2734 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2736 switch (file_lock->fl_type) {
2738 einfo.ei_mode = LCK_PR;
2741 /* An unlock request may or may not have any relation to
2742 * existing locks so we may not be able to pass a lock handle
2743 * via a normal ldlm_lock_cancel() request. The request may even
2744 * unlock a byte range in the middle of an existing lock. In
2745 * order to process an unlock request we need all of the same
2746 * information that is given with a normal read or write record
2747 * lock request. To avoid creating another ldlm unlock (cancel)
2748 * message we'll treat a LCK_NL flock request as an unlock.
2750 einfo.ei_mode = LCK_NL;
2753 einfo.ei_mode = LCK_PW;
2756 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n",
2757 file_lock->fl_type);
2772 flags = LDLM_FL_BLOCK_NOWAIT;
2778 flags = LDLM_FL_TEST_LOCK;
2779 /* Save the old mode so that if the mode in the lock changes we
2780 * can decrement the appropriate reader or writer refcount.
2782 file_lock->fl_type = einfo.ei_mode;
2785 CERROR("unknown fcntl lock command: %d\n", cmd);
2789 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2790 LUSTRE_OPC_ANY, NULL);
2791 if (IS_ERR(op_data))
2792 return PTR_ERR(op_data);
2794 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, start=%llu, end=%llu\n",
2795 PFID(ll_inode2fid(inode)), flock.l_flock.pid, flags,
2796 einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2798 rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2799 op_data, &lockh, &flock, 0, NULL /* req */, flags);
2801 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
2802 !(flags & LDLM_FL_TEST_LOCK))
2803 rc2 = locks_lock_file_wait(file, file_lock);
2805 if (rc2 && file_lock->fl_type != F_UNLCK) {
2806 einfo.ei_mode = LCK_NL;
2807 md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2808 op_data, &lockh, &flock, 0, NULL /* req */, flags);
2812 ll_finish_md_op_data(op_data);
2818 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2824 * test if some locks matching bits and l_req_mode are acquired
2825 * - bits can be in different locks
2826 * - if found clear the common lock bits in *bits
2827 * - the bits not found, are kept in *bits
2829 * \param bits [IN] searched lock bits [IN]
2830 * \param l_req_mode [IN] searched lock mode
2831 * \retval boolean, true iff all bits are found
2833 int ll_have_md_lock(struct inode *inode, __u64 *bits,
2834 enum ldlm_mode l_req_mode)
2836 struct lustre_handle lockh;
2837 ldlm_policy_data_t policy;
2838 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
2839 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2847 fid = &ll_i2info(inode)->lli_fid;
2848 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2849 ldlm_lockname[mode]);
2851 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2852 for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2853 policy.l_inodebits.bits = *bits & (1 << i);
2854 if (policy.l_inodebits.bits == 0)
2857 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2858 &policy, mode, &lockh)) {
2859 struct ldlm_lock *lock;
2861 lock = ldlm_handle2lock(&lockh);
2864 ~(lock->l_policy_data.l_inodebits.bits);
2865 LDLM_LOCK_PUT(lock);
2867 *bits &= ~policy.l_inodebits.bits;
2874 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
2875 struct lustre_handle *lockh, __u64 flags,
2876 enum ldlm_mode mode)
2878 ldlm_policy_data_t policy = { .l_inodebits = {bits} };
2882 fid = &ll_i2info(inode)->lli_fid;
2883 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2885 rc = md_lock_match(ll_i2mdexp(inode), flags | LDLM_FL_BLOCK_GRANTED,
2886 fid, LDLM_IBITS, &policy, mode, lockh);
2891 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
2893 /* Already unlinked. Just update nlink and return success */
2894 if (rc == -ENOENT) {
2896 /* This path cannot be hit for regular files unless in
2897 * case of obscure races, so no need to validate size.
2899 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
2901 } else if (rc != 0) {
2902 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
2903 "%s: revalidate FID "DFID" error: rc = %d\n",
2904 ll_get_fsname(inode->i_sb, NULL, 0),
2905 PFID(ll_inode2fid(inode)), rc);
2911 static int __ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
2913 struct inode *inode = d_inode(dentry);
2914 struct ptlrpc_request *req = NULL;
2915 struct obd_export *exp;
2918 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%pd\n",
2919 PFID(ll_inode2fid(inode)), inode, dentry);
2921 exp = ll_i2mdexp(inode);
2923 /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2924 * But under CMD case, it caused some lock issues, should be fixed
2925 * with new CMD ibits lock. See bug 12718
2927 if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) {
2928 struct lookup_intent oit = { .it_op = IT_GETATTR };
2929 struct md_op_data *op_data;
2931 if (ibits == MDS_INODELOCK_LOOKUP)
2932 oit.it_op = IT_LOOKUP;
2934 /* Call getattr by fid, so do not provide name at all. */
2935 op_data = ll_prep_md_op_data(NULL, inode,
2937 LUSTRE_OPC_ANY, NULL);
2938 if (IS_ERR(op_data))
2939 return PTR_ERR(op_data);
2941 oit.it_create_mode |= M_CHECK_STALE;
2942 rc = md_intent_lock(exp, op_data, NULL, 0,
2943 /* we are not interested in name
2947 ll_md_blocking_ast, 0);
2948 ll_finish_md_op_data(op_data);
2949 oit.it_create_mode &= ~M_CHECK_STALE;
2951 rc = ll_inode_revalidate_fini(inode, rc);
2955 rc = ll_revalidate_it_finish(req, &oit, inode);
2957 ll_intent_release(&oit);
2961 /* Unlinked? Unhash dentry, so it is not picked up later by
2962 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2963 * here to preserve get_cwd functionality on 2.6.
2966 if (!d_inode(dentry)->i_nlink)
2967 d_lustre_invalidate(dentry, 0);
2969 ll_lookup_finish_locks(&oit, inode);
2970 } else if (!ll_have_md_lock(d_inode(dentry), &ibits, LCK_MINMODE)) {
2971 struct ll_sb_info *sbi = ll_i2sbi(d_inode(dentry));
2972 u64 valid = OBD_MD_FLGETATTR;
2973 struct md_op_data *op_data;
2976 if (S_ISREG(inode->i_mode)) {
2977 rc = ll_get_default_mdsize(sbi, &ealen);
2980 valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2983 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2984 0, ealen, LUSTRE_OPC_ANY,
2986 if (IS_ERR(op_data))
2987 return PTR_ERR(op_data);
2989 op_data->op_valid = valid;
2990 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2991 ll_finish_md_op_data(op_data);
2993 rc = ll_inode_revalidate_fini(inode, rc);
2997 rc = ll_prep_inode(&inode, req, NULL, NULL);
3000 ptlrpc_req_finished(req);
3004 static int ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
3006 struct inode *inode = d_inode(dentry);
3009 rc = __ll_inode_revalidate(dentry, ibits);
3013 /* if object isn't regular file, don't validate size */
3014 if (!S_ISREG(inode->i_mode)) {
3015 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_atime;
3016 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_mtime;
3017 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_ctime;
3019 /* In case of restore, the MDT has the right size and has
3020 * already send it back without granting the layout lock,
3021 * inode is up-to-date so glimpse is useless.
3022 * Also to glimpse we need the layout, in case of a running
3023 * restore the MDT holds the layout lock so the glimpse will
3024 * block up to the end of restore (getattr will block)
3026 if (!(ll_i2info(inode)->lli_flags & LLIF_FILE_RESTORING))
3027 rc = ll_glimpse_size(inode);
3032 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
3034 struct inode *inode = d_inode(de);
3035 struct ll_sb_info *sbi = ll_i2sbi(inode);
3036 struct ll_inode_info *lli = ll_i2info(inode);
3039 res = ll_inode_revalidate(de, MDS_INODELOCK_UPDATE |
3040 MDS_INODELOCK_LOOKUP);
3041 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
3046 stat->dev = inode->i_sb->s_dev;
3047 if (ll_need_32bit_api(sbi))
3048 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
3050 stat->ino = inode->i_ino;
3051 stat->mode = inode->i_mode;
3052 stat->nlink = inode->i_nlink;
3053 stat->uid = inode->i_uid;
3054 stat->gid = inode->i_gid;
3055 stat->rdev = inode->i_rdev;
3056 stat->atime = inode->i_atime;
3057 stat->mtime = inode->i_mtime;
3058 stat->ctime = inode->i_ctime;
3059 stat->blksize = 1 << inode->i_blkbits;
3061 stat->size = i_size_read(inode);
3062 stat->blocks = inode->i_blocks;
3067 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3068 __u64 start, __u64 len)
3072 struct ll_user_fiemap *fiemap;
3073 unsigned int extent_count = fieinfo->fi_extents_max;
3075 num_bytes = sizeof(*fiemap) + (extent_count *
3076 sizeof(struct ll_fiemap_extent));
3077 fiemap = libcfs_kvzalloc(num_bytes, GFP_NOFS);
3082 fiemap->fm_flags = fieinfo->fi_flags;
3083 fiemap->fm_extent_count = fieinfo->fi_extents_max;
3084 fiemap->fm_start = start;
3085 fiemap->fm_length = len;
3086 if (extent_count > 0 &&
3087 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
3088 sizeof(struct ll_fiemap_extent)) != 0) {
3093 rc = ll_do_fiemap(inode, fiemap, num_bytes);
3095 fieinfo->fi_flags = fiemap->fm_flags;
3096 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
3097 if (extent_count > 0 &&
3098 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
3099 fiemap->fm_mapped_extents *
3100 sizeof(struct ll_fiemap_extent)) != 0) {
3110 struct posix_acl *ll_get_acl(struct inode *inode, int type)
3112 struct ll_inode_info *lli = ll_i2info(inode);
3113 struct posix_acl *acl = NULL;
3115 spin_lock(&lli->lli_lock);
3116 /* VFS' acl_permission_check->check_acl will release the refcount */
3117 acl = posix_acl_dup(lli->lli_posix_acl);
3118 #ifdef CONFIG_FS_POSIX_ACL
3119 forget_cached_acl(inode, type);
3121 spin_unlock(&lli->lli_lock);
3126 int ll_inode_permission(struct inode *inode, int mask)
3130 if (mask & MAY_NOT_BLOCK)
3133 /* as root inode are NOT getting validated in lookup operation,
3134 * need to do it before permission check.
3137 if (is_root_inode(inode)) {
3138 rc = __ll_inode_revalidate(inode->i_sb->s_root,
3139 MDS_INODELOCK_LOOKUP);
3144 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
3145 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
3147 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
3148 rc = generic_permission(inode, mask);
3153 /* -o localflock - only provides locally consistent flock locks */
3154 struct file_operations ll_file_operations = {
3155 .read_iter = ll_file_read_iter,
3156 .write_iter = ll_file_write_iter,
3157 .unlocked_ioctl = ll_file_ioctl,
3158 .open = ll_file_open,
3159 .release = ll_file_release,
3160 .mmap = ll_file_mmap,
3161 .llseek = ll_file_seek,
3162 .splice_read = ll_file_splice_read,
3167 struct file_operations ll_file_operations_flock = {
3168 .read_iter = ll_file_read_iter,
3169 .write_iter = ll_file_write_iter,
3170 .unlocked_ioctl = ll_file_ioctl,
3171 .open = ll_file_open,
3172 .release = ll_file_release,
3173 .mmap = ll_file_mmap,
3174 .llseek = ll_file_seek,
3175 .splice_read = ll_file_splice_read,
3178 .flock = ll_file_flock,
3179 .lock = ll_file_flock
3182 /* These are for -o noflock - to return ENOSYS on flock calls */
3183 struct file_operations ll_file_operations_noflock = {
3184 .read_iter = ll_file_read_iter,
3185 .write_iter = ll_file_write_iter,
3186 .unlocked_ioctl = ll_file_ioctl,
3187 .open = ll_file_open,
3188 .release = ll_file_release,
3189 .mmap = ll_file_mmap,
3190 .llseek = ll_file_seek,
3191 .splice_read = ll_file_splice_read,
3194 .flock = ll_file_noflock,
3195 .lock = ll_file_noflock
3198 const struct inode_operations ll_file_inode_operations = {
3199 .setattr = ll_setattr,
3200 .getattr = ll_getattr,
3201 .permission = ll_inode_permission,
3202 .setxattr = ll_setxattr,
3203 .getxattr = ll_getxattr,
3204 .listxattr = ll_listxattr,
3205 .removexattr = ll_removexattr,
3206 .fiemap = ll_fiemap,
3207 .get_acl = ll_get_acl,
3210 /* dynamic ioctl number support routines */
3211 static struct llioc_ctl_data {
3212 struct rw_semaphore ioc_sem;
3213 struct list_head ioc_head;
3215 __RWSEM_INITIALIZER(llioc.ioc_sem),
3216 LIST_HEAD_INIT(llioc.ioc_head)
3220 struct list_head iocd_list;
3221 unsigned int iocd_size;
3222 llioc_callback_t iocd_cb;
3223 unsigned int iocd_count;
3224 unsigned int iocd_cmd[0];
3227 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
3230 struct llioc_data *in_data = NULL;
3232 if (!cb || !cmd || count > LLIOC_MAX_CMD || count < 0)
3235 size = sizeof(*in_data) + count * sizeof(unsigned int);
3236 in_data = kzalloc(size, GFP_NOFS);
3240 memset(in_data, 0, sizeof(*in_data));
3241 in_data->iocd_size = size;
3242 in_data->iocd_cb = cb;
3243 in_data->iocd_count = count;
3244 memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
3246 down_write(&llioc.ioc_sem);
3247 list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
3248 up_write(&llioc.ioc_sem);
3252 EXPORT_SYMBOL(ll_iocontrol_register);
3254 void ll_iocontrol_unregister(void *magic)
3256 struct llioc_data *tmp;
3261 down_write(&llioc.ioc_sem);
3262 list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
3264 list_del(&tmp->iocd_list);
3265 up_write(&llioc.ioc_sem);
3271 up_write(&llioc.ioc_sem);
3273 CWARN("didn't find iocontrol register block with magic: %p\n", magic);
3275 EXPORT_SYMBOL(ll_iocontrol_unregister);
3277 static enum llioc_iter
3278 ll_iocontrol_call(struct inode *inode, struct file *file,
3279 unsigned int cmd, unsigned long arg, int *rcp)
3281 enum llioc_iter ret = LLIOC_CONT;
3282 struct llioc_data *data;
3283 int rc = -EINVAL, i;
3285 down_read(&llioc.ioc_sem);
3286 list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
3287 for (i = 0; i < data->iocd_count; i++) {
3288 if (cmd != data->iocd_cmd[i])
3291 ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
3295 if (ret == LLIOC_STOP)
3298 up_read(&llioc.ioc_sem);
3305 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
3307 struct ll_inode_info *lli = ll_i2info(inode);
3308 struct cl_env_nest nest;
3315 env = cl_env_nested_get(&nest);
3317 return PTR_ERR(env);
3319 result = cl_conf_set(env, lli->lli_clob, conf);
3320 cl_env_nested_put(&nest, env);
3322 if (conf->coc_opc == OBJECT_CONF_SET) {
3323 struct ldlm_lock *lock = conf->coc_lock;
3326 LASSERT(ldlm_has_layout(lock));
3328 /* it can only be allowed to match after layout is
3329 * applied to inode otherwise false layout would be
3330 * seen. Applying layout should happen before dropping
3333 ldlm_lock_allow_match(lock);
3339 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
3340 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
3343 struct ll_sb_info *sbi = ll_i2sbi(inode);
3344 struct ptlrpc_request *req;
3345 struct mdt_body *body;
3351 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
3352 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
3353 lock->l_lvb_data, lock->l_lvb_len);
3355 if (lock->l_lvb_data && ldlm_is_lvb_ready(lock))
3358 /* if layout lock was granted right away, the layout is returned
3359 * within DLM_LVB of dlm reply; otherwise if the lock was ever
3360 * blocked and then granted via completion ast, we have to fetch
3361 * layout here. Please note that we can't use the LVB buffer in
3362 * completion AST because it doesn't have a large enough buffer
3364 rc = ll_get_default_mdsize(sbi, &lmmsize);
3366 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
3367 OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
3372 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3378 lmmsize = body->eadatasize;
3379 if (lmmsize == 0) /* empty layout */ {
3384 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
3390 lvbdata = libcfs_kvzalloc(lmmsize, GFP_NOFS);
3396 memcpy(lvbdata, lmm, lmmsize);
3397 lock_res_and_lock(lock);
3398 if (lock->l_lvb_data)
3399 kvfree(lock->l_lvb_data);
3401 lock->l_lvb_data = lvbdata;
3402 lock->l_lvb_len = lmmsize;
3403 unlock_res_and_lock(lock);
3406 ptlrpc_req_finished(req);
3411 * Apply the layout to the inode. Layout lock is held and will be released
3414 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
3415 struct inode *inode, __u32 *gen, bool reconf)
3417 struct ll_inode_info *lli = ll_i2info(inode);
3418 struct ll_sb_info *sbi = ll_i2sbi(inode);
3419 struct ldlm_lock *lock;
3420 struct lustre_md md = { NULL };
3421 struct cl_object_conf conf;
3424 bool wait_layout = false;
3426 LASSERT(lustre_handle_is_used(lockh));
3428 lock = ldlm_handle2lock(lockh);
3430 LASSERT(ldlm_has_layout(lock));
3432 LDLM_DEBUG(lock, "File "DFID"(%p) being reconfigured: %d",
3433 PFID(&lli->lli_fid), inode, reconf);
3435 /* in case this is a caching lock and reinstate with new inode */
3436 md_set_lock_data(sbi->ll_md_exp, &lockh->cookie, inode, NULL);
3438 lock_res_and_lock(lock);
3439 lvb_ready = ldlm_is_lvb_ready(lock);
3440 unlock_res_and_lock(lock);
3441 /* checking lvb_ready is racy but this is okay. The worst case is
3442 * that multi processes may configure the file on the same time.
3444 if (lvb_ready || !reconf) {
3447 /* layout_gen must be valid if layout lock is not
3448 * cancelled and stripe has already set
3450 *gen = ll_layout_version_get(lli);
3456 rc = ll_layout_fetch(inode, lock);
3460 /* for layout lock, lmm is returned in lock's lvb.
3461 * lvb_data is immutable if the lock is held so it's safe to access it
3462 * without res lock. See the description in ldlm_lock_decref_internal()
3463 * for the condition to free lvb_data of layout lock
3465 if (lock->l_lvb_data) {
3466 rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm,
3467 lock->l_lvb_data, lock->l_lvb_len);
3469 *gen = LL_LAYOUT_GEN_EMPTY;
3471 *gen = md.lsm->lsm_layout_gen;
3474 CERROR("%s: file " DFID " unpackmd error: %d\n",
3475 ll_get_fsname(inode->i_sb, NULL, 0),
3476 PFID(&lli->lli_fid), rc);
3482 /* set layout to file. Unlikely this will fail as old layout was
3485 memset(&conf, 0, sizeof(conf));
3486 conf.coc_opc = OBJECT_CONF_SET;
3487 conf.coc_inode = inode;
3488 conf.coc_lock = lock;
3489 conf.u.coc_md = &md;
3490 rc = ll_layout_conf(inode, &conf);
3493 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
3495 /* refresh layout failed, need to wait */
3496 wait_layout = rc == -EBUSY;
3499 LDLM_LOCK_PUT(lock);
3500 ldlm_lock_decref(lockh, mode);
3502 /* wait for IO to complete if it's still being used. */
3504 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
3505 ll_get_fsname(inode->i_sb, NULL, 0),
3506 PFID(&lli->lli_fid), inode);
3508 memset(&conf, 0, sizeof(conf));
3509 conf.coc_opc = OBJECT_CONF_WAIT;
3510 conf.coc_inode = inode;
3511 rc = ll_layout_conf(inode, &conf);
3515 CDEBUG(D_INODE, "%s: file="DFID" waiting layout return: %d.\n",
3516 ll_get_fsname(inode->i_sb, NULL, 0),
3517 PFID(&lli->lli_fid), rc);
3523 * This function checks if there exists a LAYOUT lock on the client side,
3524 * or enqueues it if it doesn't have one in cache.
3526 * This function will not hold layout lock so it may be revoked any time after
3527 * this function returns. Any operations depend on layout should be redone
3530 * This function should be called before lov_io_init() to get an uptodate
3531 * layout version, the caller should save the version number and after IO
3532 * is finished, this function should be called again to verify that layout
3533 * is not changed during IO time.
3535 int ll_layout_refresh(struct inode *inode, __u32 *gen)
3537 struct ll_inode_info *lli = ll_i2info(inode);
3538 struct ll_sb_info *sbi = ll_i2sbi(inode);
3539 struct md_op_data *op_data;
3540 struct lookup_intent it;
3541 struct lustre_handle lockh;
3542 enum ldlm_mode mode;
3543 struct ldlm_enqueue_info einfo = {
3544 .ei_type = LDLM_IBITS,
3546 .ei_cb_bl = ll_md_blocking_ast,
3547 .ei_cb_cp = ldlm_completion_ast,
3551 *gen = ll_layout_version_get(lli);
3552 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != LL_LAYOUT_GEN_NONE)
3556 LASSERT(fid_is_sane(ll_inode2fid(inode)));
3557 LASSERT(S_ISREG(inode->i_mode));
3559 /* take layout lock mutex to enqueue layout lock exclusively. */
3560 mutex_lock(&lli->lli_layout_mutex);
3563 /* mostly layout lock is caching on the local side, so try to match
3564 * it before grabbing layout lock mutex.
3566 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
3567 LCK_CR | LCK_CW | LCK_PR | LCK_PW);
3568 if (mode != 0) { /* hit cached lock */
3569 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3573 mutex_unlock(&lli->lli_layout_mutex);
3577 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
3578 0, 0, LUSTRE_OPC_ANY, NULL);
3579 if (IS_ERR(op_data)) {
3580 mutex_unlock(&lli->lli_layout_mutex);
3581 return PTR_ERR(op_data);
3584 /* have to enqueue one */
3585 memset(&it, 0, sizeof(it));
3586 it.it_op = IT_LAYOUT;
3587 lockh.cookie = 0ULL;
3589 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
3590 ll_get_fsname(inode->i_sb, NULL, 0),
3591 PFID(&lli->lli_fid), inode);
3593 rc = md_enqueue(sbi->ll_md_exp, &einfo, &it, op_data, &lockh,
3595 ptlrpc_req_finished(it.d.lustre.it_data);
3596 it.d.lustre.it_data = NULL;
3598 ll_finish_md_op_data(op_data);
3600 mode = it.d.lustre.it_lock_mode;
3601 it.d.lustre.it_lock_mode = 0;
3602 ll_intent_drop_lock(&it);
3605 /* set lock data in case this is a new lock */
3606 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
3607 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3611 mutex_unlock(&lli->lli_layout_mutex);
3617 * This function send a restore request to the MDT
3619 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
3621 struct hsm_user_request *hur;
3624 len = sizeof(struct hsm_user_request) +
3625 sizeof(struct hsm_user_item);
3626 hur = kzalloc(len, GFP_NOFS);
3630 hur->hur_request.hr_action = HUA_RESTORE;
3631 hur->hur_request.hr_archive_id = 0;
3632 hur->hur_request.hr_flags = 0;
3633 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
3634 sizeof(hur->hur_user_item[0].hui_fid));
3635 hur->hur_user_item[0].hui_extent.offset = offset;
3636 hur->hur_user_item[0].hui_extent.length = length;
3637 hur->hur_request.hr_itemcount = 1;
3638 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,