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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Directory code for lustre client.
37 #include <linux/pagemap.h>
39 #include <linux/version.h>
40 #include <linux/security.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43 #include <linux/uaccess.h>
44 #include <linux/buffer_head.h> // for wait_on_buffer
45 #include <linux/pagevec.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_lib.h>
53 #include <lustre_dlm.h>
54 #include <lustre_compat.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
58 #include <libcfs/libcfs_crypto.h>
60 #include "llite_internal.h"
63 * (new) readdir implementation overview.
65 * Original lustre readdir implementation cached exact copy of raw directory
66 * pages on the client. These pages were indexed in client page cache by
67 * logical offset in the directory file. This design, while very simple and
68 * intuitive had some inherent problems:
70 * . it implies that byte offset to the directory entry serves as a
71 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72 * ext3/htree directory entries may move due to splits, and more
75 * . it is incompatible with the design of split directories for cmd3,
76 * that assumes that names are distributed across nodes based on their
77 * hash, and so readdir should be done in hash order.
79 * New readdir implementation does readdir in hash order, and uses hash of a
80 * file name as a telldir/seekdir cookie. This led to number of complications:
82 * . hash is not unique, so it cannot be used to index cached directory
83 * pages on the client (note, that it requires a whole pageful of hash
84 * collided entries to cause two pages to have identical hashes);
86 * . hash is not unique, so it cannot, strictly speaking, be used as an
87 * entry cookie. ext3/htree has the same problem and lustre implementation
88 * mimics their solution: seekdir(hash) positions directory at the first
89 * entry with the given hash.
95 * Client caches directory pages using hash of the first entry as an index. As
96 * noted above hash is not unique, so this solution doesn't work as is:
97 * special processing is needed for "page hash chains" (i.e., sequences of
98 * pages filled with entries all having the same hash value).
100 * First, such chains have to be detected. To this end, server returns to the
101 * client the hash of the first entry on the page next to one returned. When
102 * client detects that this hash is the same as hash of the first entry on the
103 * returned page, page hash collision has to be handled. Pages in the
104 * hash chain, except first one, are termed "overflow pages".
106 * Proposed (unimplimented) solution to index uniqueness problem is to
107 * not cache overflow pages. Instead, when page hash collision is
108 * detected, all overflow pages from emerging chain should be
109 * immediately requested from the server and placed in a special data
110 * structure. This data structure can be used by ll_readdir() to
111 * process entries from overflow pages. When readdir invocation
112 * finishes, overflow pages are discarded. If page hash collision chain
113 * weren't completely processed, next call to readdir will again detect
114 * page hash collision, again read overflow pages in, process next
115 * portion of entries and again discard the pages. This is not as
116 * wasteful as it looks, because, given reasonable hash, page hash
117 * collisions are extremely rare.
119 * 1. directory positioning
121 * When seekdir(hash) is called.
123 * seekdir() sets the location in the directory stream from which the next
124 * readdir() call will start. mdc_page_locate() is used to find page with
125 * starting hash and will issue RPC to fetch that page. If there is a hash
126 * collision the concerned page is removed.
131 * identification of and access to overflow pages
135 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
136 * a header lu_dirpage which describes the start/end hash, and whether this
137 * page is empty (contains no dir entry) or hash collide with next page.
138 * After client receives reply, several pages will be integrated into dir page
139 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
140 * lu_dirpage for this integrated page will be adjusted. See
141 * mdc_adjust_dirpages().
144 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
145 __u64 offset, int *partial_readdir_rc)
147 struct md_readdir_info mrinfo = {
148 .mr_blocking_ast = ll_md_blocking_ast };
152 rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
156 if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
157 *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
162 void ll_release_page(struct inode *inode, struct page *page,
167 /* Always remove the page for striped dir, because the page is
168 * built from temporarily in LMV layer */
169 if (inode && ll_dir_striped(inode)) {
176 if (likely(page->mapping != NULL))
177 cfs_delete_from_page_cache(page);
183 #ifdef HAVE_DIR_CONTEXT
184 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
185 struct dir_context *ctx, int *partial_readdir_rc)
188 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
189 void *cookie, filldir_t filldir, int *partial_readdir_rc)
192 struct ll_sb_info *sbi = ll_i2sbi(inode);
194 bool is_api32 = ll_need_32bit_api(sbi);
195 bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
198 struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
202 if (IS_ENCRYPTED(inode)) {
203 rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
208 page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
210 while (rc == 0 && !done) {
211 struct lu_dirpage *dp;
212 struct lu_dirent *ent;
221 hash = MDS_DIR_END_OFF;
222 dp = page_address(page);
223 for (ent = lu_dirent_start(dp); ent != NULL && !done;
224 ent = lu_dirent_next(ent)) {
231 hash = le64_to_cpu(ent->lde_hash);
232 if (hash < pos) /* Skip until we find target hash */
235 namelen = le16_to_cpu(ent->lde_namelen);
236 if (namelen == 0) /* Skip dummy record */
239 if (is_api32 && is_hash64)
243 fid_le_to_cpu(&fid, &ent->lde_fid);
244 ino = cl_fid_build_ino(&fid, is_api32);
245 type = S_DT(lu_dirent_type_get(ent));
246 /* For ll_nfs_get_name_filldir(), it will try to access
247 * 'ent' through 'lde_name', so the parameter 'name'
248 * for 'filldir()' must be part of the 'ent'. */
249 #ifdef HAVE_DIR_CONTEXT
251 if (!IS_ENCRYPTED(inode)) {
252 done = !dir_emit(ctx, ent->lde_name, namelen,
255 /* Directory is encrypted */
256 int save_len = lltr.len;
257 struct llcrypt_str de_name =
258 LLTR_INIT(ent->lde_name, namelen);
260 rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
268 done = !dir_emit(ctx, de_name.name, de_name.len,
272 /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
273 * IS_ENCRYPTED is brought by kernel 4.14.
274 * So there is no need to handle encryption case here.
276 done = filldir(cookie, ent->lde_name, namelen, lhash,
283 ll_release_page(inode, page, false);
287 next = le64_to_cpu(dp->ldp_hash_end);
289 if (pos == MDS_DIR_END_OFF) {
290 /* End of directory reached. */
292 ll_release_page(inode, page, false);
294 /* Normal case: continue to the next page.*/
295 ll_release_page(inode, page,
296 le32_to_cpu(dp->ldp_flags) &
299 page = ll_get_dir_page(inode, op_data, pos,
303 #ifdef HAVE_DIR_CONTEXT
308 llcrypt_fname_free_buffer(&lltr);
312 #ifdef HAVE_DIR_CONTEXT
313 static int ll_iterate(struct file *filp, struct dir_context *ctx)
315 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
318 struct inode *inode = file_inode(filp);
319 struct ll_file_data *lfd = filp->private_data;
320 struct ll_sb_info *sbi = ll_i2sbi(inode);
321 bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
322 int api32 = ll_need_32bit_api(sbi);
323 struct md_op_data *op_data;
324 struct lu_fid pfid = { 0 };
325 ktime_t kstart = ktime_get();
326 /* result of possible partial readdir */
327 int partial_readdir_rc = 0;
333 LASSERT(lfd != NULL);
337 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
338 PFID(ll_inode2fid(inode)),
339 inode, (unsigned long)pos, i_size_read(inode), api32);
341 if (IS_ENCRYPTED(inode)) {
342 rc = llcrypt_prepare_readdir(inode);
343 if (rc && rc != -ENOKEY)
347 if (pos == MDS_DIR_END_OFF)
351 if (unlikely(ll_dir_striped(inode))) {
352 struct dentry *parent = dget_parent(file_dentry(filp));
353 struct inode *i_dir = d_inode(parent);
355 /* Only needed for striped dir to fill ..see lmv_read_page() */
357 struct obd_export *exp = ll_i2mdexp(i_dir);
358 __u64 ibits = MDS_INODELOCK_LOOKUP;
360 if (ll_have_md_lock(exp, i_dir, &ibits, LCK_MINMODE))
361 pfid = *ll_inode2fid(i_dir);
365 /* If it can not find in cache, do lookup .. on the master
367 if (fid_is_zero(&pfid)) {
368 rc = ll_dir_get_parent_fid(inode, &pfid);
374 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
375 LUSTRE_OPC_ANY, inode);
377 GOTO(out, rc = PTR_ERR(op_data));
379 /* foreign dirs are browsed out of Lustre */
380 if (unlikely(lmv_dir_foreign(op_data->op_lso1))) {
381 ll_finish_md_op_data(op_data);
385 op_data->op_fid3 = pfid;
387 #ifdef HAVE_DIR_CONTEXT
389 rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
392 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
393 &partial_readdir_rc);
396 if (!lfd->fd_partial_readdir_rc)
397 lfd->fd_partial_readdir_rc = partial_readdir_rc;
399 if (pos == MDS_DIR_END_OFF) {
401 pos = LL_DIR_END_OFF_32BIT;
403 pos = LL_DIR_END_OFF;
408 #ifdef HAVE_DIR_CONTEXT
413 ll_finish_md_op_data(op_data);
417 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
418 ktime_us_delta(ktime_get(), kstart));
424 * Create striped directory with specified stripe(@lump)
426 * \param[in] dparent the parent of the directory.
427 * \param[in] lump the specified stripes.
428 * \param[in] dirname the name of the directory.
429 * \param[in] mode the specified mode of the directory.
431 * \retval =0 if striped directory is being created successfully.
432 * <0 if the creation is failed.
434 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
435 size_t len, const char *dirname, umode_t mode,
438 struct inode *parent = dparent->d_inode;
439 struct ptlrpc_request *request = NULL;
440 struct md_op_data *op_data;
441 struct ll_sb_info *sbi = ll_i2sbi(parent);
442 struct inode *inode = NULL;
443 struct dentry dentry = {
447 .len = strlen(dirname),
448 .hash = ll_full_name_hash(dparent, dirname,
451 .d_sb = dparent->d_sb,
453 bool encrypt = false;
458 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
461 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
463 "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
464 PFID(ll_inode2fid(parent)), parent, dirname,
465 (int)lump->lum_stripe_offset, lump->lum_stripe_count,
466 lump->lum_hash_type);
468 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
471 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
472 PFID(ll_inode2fid(parent)), parent, dirname,
473 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
476 if (lump->lum_stripe_count > 1 &&
477 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
480 if (IS_DEADDIR(parent) &&
481 !CFS_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
484 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
485 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
487 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
488 lump->lum_hash_type = cfs_fail_val;
489 } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
490 enum lmv_hash_type type = lump->lum_hash_type &
493 if (type >= LMV_HASH_TYPE_CRUSH ||
494 type == LMV_HASH_TYPE_UNKNOWN)
495 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
496 LMV_HASH_TYPE_FNV_1A_64;
499 hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
500 if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
503 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
504 lustre_swab_lmv_user_md(lump);
506 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
507 mode &= ~current_umask();
508 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
509 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
510 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
513 RETURN(PTR_ERR(op_data));
515 op_data->op_dir_depth = ll_i2info(parent)->lli_inherit_depth ?:
516 ll_i2info(parent)->lli_dir_depth;
518 if (ll_sbi_has_encrypt(sbi) &&
519 (IS_ENCRYPTED(parent) ||
520 unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
521 err = llcrypt_prepare_readdir(parent);
523 GOTO(out_op_data, err);
524 if (!llcrypt_has_encryption_key(parent))
525 GOTO(out_op_data, err = -ENOKEY);
529 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
530 /* selinux_dentry_init_security() uses dentry->d_parent and name
531 * to determine the security context for the file. So our fake
532 * dentry should be real enough for this purpose. */
533 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
534 &op_data->op_file_secctx_name,
535 &op_data->op_file_secctx_name_size,
536 &op_data->op_file_secctx,
537 &op_data->op_file_secctx_size,
538 &op_data->op_file_secctx_slot);
540 GOTO(out_op_data, err);
544 err = llcrypt_inherit_context(parent, NULL, op_data, false);
546 GOTO(out_op_data, err);
549 op_data->op_cli_flags |= CLI_SET_MEA;
551 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
553 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
554 from_kuid(&init_user_ns, current_fsuid()),
555 from_kgid(&init_user_ns, current_fsgid()),
556 current_cap(), 0, &request);
558 GOTO(out_request, err);
560 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
562 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
564 GOTO(out_inode, err);
566 dentry.d_inode = inode;
568 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags))
569 err = ll_inode_notifysecctx(inode, op_data->op_file_secctx,
570 op_data->op_file_secctx_size);
572 err = ll_inode_init_security(&dentry, inode, parent);
575 GOTO(out_inode, err);
578 err = ll_set_encflags(inode, op_data->op_file_encctx,
579 op_data->op_file_encctx_size, false);
581 GOTO(out_inode, err);
587 ptlrpc_req_finished(request);
589 ll_finish_md_op_data(op_data);
594 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
597 struct ll_sb_info *sbi = ll_i2sbi(inode);
598 struct md_op_data *op_data;
599 struct ptlrpc_request *req = NULL;
605 switch (lump->lmm_magic) {
606 case LOV_USER_MAGIC_V1:
607 lum_size = sizeof(struct lov_user_md_v1);
609 case LOV_USER_MAGIC_V3:
610 lum_size = sizeof(struct lov_user_md_v3);
612 case LOV_USER_MAGIC_COMP_V1:
613 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
615 case LMV_USER_MAGIC: {
616 struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
618 /* MDS < 2.14 doesn't support 'crush' hash type, and
619 * cannot handle unknown hash if client doesn't set a
620 * valid one. switch to fnv_1a_64.
622 if (!(exp_connect_flags2(sbi->ll_md_exp) &
623 OBD_CONNECT2_CRUSH)) {
624 enum lmv_hash_type type = lmv->lum_hash_type &
627 if (type >= LMV_HASH_TYPE_CRUSH ||
628 type == LMV_HASH_TYPE_UNKNOWN)
630 (lmv->lum_hash_type ^ type) |
631 LMV_HASH_TYPE_FNV_1A_64;
633 if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
634 lustre_swab_lmv_user_md(lmv);
635 lum_size = sizeof(*lmv);
638 case LOV_USER_MAGIC_SPECIFIC: {
639 struct lov_user_md_v3 *v3 =
640 (struct lov_user_md_v3 *)lump;
641 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
643 lum_size = lov_user_md_size(v3->lmm_stripe_count,
644 LOV_USER_MAGIC_SPECIFIC);
649 "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
650 lump->lmm_magic, LOV_USER_MAGIC_V1,
655 /* This is coming from userspace, so should be in
656 * local endian. But the MDS would like it in little
657 * endian, so we swab it before we send it.
659 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
660 le32_to_cpu(LOV_MAGIC_MAGIC))
661 lustre_swab_lov_user_md(lump, 0);
663 lum_size = sizeof(struct lov_user_md_v1);
666 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
667 LUSTRE_OPC_ANY, NULL);
669 RETURN(PTR_ERR(op_data));
671 /* swabbing is done in lov_setstripe() on server side */
672 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
673 ll_finish_md_op_data(op_data);
674 ptlrpc_req_finished(req);
681 /* get default LMV from client cache */
682 static int ll_dir_get_default_lmv(struct inode *inode, struct lmv_user_md *lum)
684 struct ll_inode_info *lli = ll_i2info(inode);
685 const struct lmv_stripe_md *lsm;
686 bool fs_dmv_got = false;
691 if (lli->lli_def_lsm_obj) {
692 down_read(&lli->lli_lsm_sem);
693 lsm = &lli->lli_def_lsm_obj->lso_lsm;
695 lum->lum_magic = lsm->lsm_md_magic;
696 lum->lum_stripe_count = lsm->lsm_md_stripe_count;
697 lum->lum_stripe_offset = lsm->lsm_md_master_mdt_index;
698 lum->lum_hash_type = lsm->lsm_md_hash_type;
699 lum->lum_max_inherit = lsm->lsm_md_max_inherit;
700 lum->lum_max_inherit_rr = lsm->lsm_md_max_inherit_rr;
703 up_read(&lli->lli_lsm_sem);
706 if (rc == -ENODATA && !is_root_inode(inode) && !fs_dmv_got) {
707 lli = ll_i2info(inode->i_sb->s_root->d_inode);
712 if (!rc && fs_dmv_got) {
713 lli = ll_i2info(inode);
714 if (lum->lum_max_inherit != LMV_INHERIT_UNLIMITED) {
715 if (lum->lum_max_inherit == LMV_INHERIT_NONE ||
716 lum->lum_max_inherit < LMV_INHERIT_END ||
717 lum->lum_max_inherit > LMV_INHERIT_MAX ||
718 lum->lum_max_inherit <= lli->lli_dir_depth)
719 GOTO(out, rc = -ENODATA);
721 lum->lum_max_inherit -= lli->lli_dir_depth;
724 if (lum->lum_max_inherit_rr != LMV_INHERIT_RR_UNLIMITED) {
725 if (lum->lum_max_inherit_rr == LMV_INHERIT_NONE ||
726 lum->lum_max_inherit_rr < LMV_INHERIT_RR_END ||
727 lum->lum_max_inherit_rr > LMV_INHERIT_RR_MAX ||
728 lum->lum_max_inherit_rr <= lli->lli_dir_depth)
729 lum->lum_max_inherit_rr = LMV_INHERIT_RR_NONE;
731 if (lum->lum_max_inherit_rr > lli->lli_dir_depth)
732 lum->lum_max_inherit_rr -= lli->lli_dir_depth;
739 int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
740 struct ptlrpc_request **request, u64 valid,
741 enum get_default_layout_type type)
743 struct ll_sb_info *sbi = ll_i2sbi(inode);
744 struct mdt_body *body;
745 struct lov_mds_md *lmm = NULL;
746 struct ptlrpc_request *req = NULL;
747 int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
748 struct md_op_data *op_data;
754 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
755 LUSTRE_OPC_ANY, NULL);
757 RETURN(PTR_ERR(op_data));
759 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
761 if (type == GET_DEFAULT_LAYOUT_ROOT) {
762 lu_root_fid(&op_data->op_fid1);
763 fid = op_data->op_fid1;
765 fid = *ll_inode2fid(inode);
768 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
769 ll_finish_md_op_data(op_data);
771 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
776 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
777 LASSERT(body != NULL);
779 lmm_size = body->mbo_eadatasize;
781 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
783 GOTO(out, rc = -ENODATA);
786 lmm = req_capsule_server_sized_get(&req->rq_pill,
787 &RMF_MDT_MD, lmm_size);
788 LASSERT(lmm != NULL);
790 /* This is coming from the MDS, so is probably in
791 * little endian. We convert it to host endian before
792 * passing it to userspace.
794 /* We don't swab objects for directories */
795 switch (le32_to_cpu(lmm->lmm_magic)) {
798 case LOV_MAGIC_COMP_V1:
799 case LOV_USER_MAGIC_SPECIFIC:
800 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
801 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
804 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
805 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
808 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
809 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
811 case LMV_MAGIC_FOREIGN: {
812 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
814 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
815 __swab32s(&lfm->lfm_magic);
816 __swab32s(&lfm->lfm_length);
817 __swab32s(&lfm->lfm_type);
818 __swab32s(&lfm->lfm_flags);
824 CERROR("%s: unknown magic: %lX: rc = %d\n", sbi->ll_fsname,
825 (unsigned long)lmm->lmm_magic, rc);
829 *plmm_size = lmm_size;
835 * This function will be used to get default LOV/LMV/Default LMV
836 * @valid will be used to indicate which stripe it will retrieve.
837 * If the directory does not have its own default layout, then the
838 * function will request the default layout from root FID.
839 * OBD_MD_MEA LMV stripe EA
840 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
841 * otherwise Default LOV EA.
842 * Each time, it can only retrieve 1 stripe EA
844 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
845 struct ptlrpc_request **request,
846 struct ptlrpc_request **root_request,
849 struct ptlrpc_request *req = NULL;
850 struct ptlrpc_request *root_req = NULL;
851 struct lov_mds_md *lmm = NULL;
856 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
858 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
859 !(valid & OBD_MD_MEA) && root_request != NULL) {
860 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
861 &lmm_size, &root_req, valid,
862 GET_DEFAULT_LAYOUT_ROOT);
868 *plmm_size = lmm_size;
870 if (root_request != NULL)
871 *root_request = root_req;
877 * This function will be used to get default LOV/LMV/Default LMV
878 * @valid will be used to indicate which stripe it will retrieve
879 * OBD_MD_MEA LMV stripe EA
880 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
881 * otherwise Default LOV EA.
882 * Each time, it can only retrieve 1 stripe EA
884 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
885 struct ptlrpc_request **request, u64 valid)
887 struct ptlrpc_request *req = NULL;
888 struct lov_mds_md *lmm = NULL;
893 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
897 *plmm_size = lmm_size;
903 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
905 struct md_op_data *op_data;
910 OBD_ALLOC_PTR(op_data);
914 op_data->op_flags |= MF_GET_MDT_IDX;
915 op_data->op_fid1 = *fid;
916 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
917 mdt_index = op_data->op_mds;
918 OBD_FREE_PTR(op_data);
926 * Get MDT index for the inode.
928 int ll_get_mdt_idx(struct inode *inode)
930 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
934 * Generic handler to do any pre-copy work.
936 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
937 * first information for it that real work has started.
939 * Moreover, for a ARCHIVE request, it will sample the file data version and
940 * store it in \a copy.
942 * \return 0 on success.
944 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
946 struct ll_sb_info *sbi = ll_s2sbi(sb);
947 struct hsm_progress_kernel hpk;
952 /* Forge a hsm_progress based on data from copy. */
953 hpk.hpk_fid = copy->hc_hai.hai_fid;
954 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
955 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
956 hpk.hpk_extent.length = 0;
959 hpk.hpk_data_version = 0;
962 /* For archive request, we need to read the current file version. */
963 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
965 __u64 data_version = 0;
967 /* Get inode for this fid */
968 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
970 hpk.hpk_flags |= HP_FLAG_RETRY;
971 /* hpk_errval is >= 0 */
972 hpk.hpk_errval = -PTR_ERR(inode);
973 GOTO(progress, rc = PTR_ERR(inode));
976 /* Read current file data version */
977 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
980 CDEBUG(D_HSM, "Could not read file data version of "
981 DFID" (rc = %d). Archive request ("
982 "%#llx) could not be done.\n",
983 PFID(©->hc_hai.hai_fid), rc,
984 copy->hc_hai.hai_cookie);
985 hpk.hpk_flags |= HP_FLAG_RETRY;
986 /* hpk_errval must be >= 0 */
987 hpk.hpk_errval = -rc;
991 /* Store in the hsm_copy for later copytool use.
992 * Always modified even if no lsm. */
993 copy->hc_data_version = data_version;
997 /* On error, the request should be considered as completed */
998 if (hpk.hpk_errval > 0)
999 hpk.hpk_flags |= HP_FLAG_COMPLETED;
1001 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1004 /* Return first error */
1005 RETURN(rc != 0 ? rc : rc2);
1009 * Generic handler to do any post-copy work.
1011 * It will send the last hsm_progress update to coordinator to inform it
1012 * that copy is finished and whether it was successful or not.
1015 * - for ARCHIVE request, it will sample the file data version and compare it
1016 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
1017 * will be considered as failed.
1018 * - for RESTORE request, it will sample the file data version and send it to
1019 * coordinator which is useful if the file was imported as 'released'.
1021 * \return 0 on success.
1023 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
1025 struct ll_sb_info *sbi = ll_s2sbi(sb);
1026 struct hsm_progress_kernel hpk;
1031 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
1032 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
1033 * initialized if copy_end was called with copy == NULL.
1036 /* Forge a hsm_progress based on data from copy. */
1037 hpk.hpk_fid = copy->hc_hai.hai_fid;
1038 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1039 hpk.hpk_extent = copy->hc_hai.hai_extent;
1040 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1041 hpk.hpk_errval = copy->hc_errval;
1042 hpk.hpk_data_version = 0;
1044 /* For archive request, we need to check the file data was not changed.
1046 * For restore request, we need to send the file data version, this is
1047 * useful when the file was created using hsm_import.
1049 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1050 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1051 (copy->hc_errval == 0)) {
1052 struct inode *inode;
1053 __u64 data_version = 0;
1055 /* Get lsm for this fid */
1056 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1057 if (IS_ERR(inode)) {
1058 hpk.hpk_flags |= HP_FLAG_RETRY;
1059 /* hpk_errval must be >= 0 */
1060 hpk.hpk_errval = -PTR_ERR(inode);
1061 GOTO(progress, rc = PTR_ERR(inode));
1064 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1068 "Could not read file data version. Request could not be confirmed.\n");
1069 if (hpk.hpk_errval == 0)
1070 hpk.hpk_errval = -rc;
1074 /* Store in the hsm_copy for later copytool use.
1075 * Always modified even if no lsm. */
1076 hpk.hpk_data_version = data_version;
1078 /* File could have been stripped during archiving, so we need
1079 * to check anyway. */
1080 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1081 (copy->hc_data_version != data_version)) {
1082 CDEBUG(D_HSM, "File data version mismatched. "
1083 "File content was changed during archiving. "
1084 DFID", start:%#llx current:%#llx\n",
1085 PFID(©->hc_hai.hai_fid),
1086 copy->hc_data_version, data_version);
1087 /* File was changed, send error to cdt. Do not ask for
1088 * retry because if a file is modified frequently,
1089 * the cdt will loop on retried archive requests.
1090 * The policy engine will ask for a new archive later
1091 * when the file will not be modified for some tunable
1093 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1095 /* hpk_errval must be >= 0 */
1096 hpk.hpk_errval = -rc;
1103 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1106 /* Return first error */
1107 RETURN(rc != 0 ? rc : rc2);
1111 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1112 const struct lustre_kernelcomm __user *data)
1114 struct lustre_kernelcomm *lk;
1115 struct lustre_kernelcomm *tmp;
1116 size_t size = sizeof(*lk);
1121 /* copy data from userspace to get numbers of archive_id */
1122 OBD_ALLOC(lk, size);
1126 if (copy_from_user(lk, data, size))
1127 GOTO(out_lk, rc = -EFAULT);
1129 if (lk->lk_flags & LK_FLG_STOP)
1132 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1133 __u32 archive_mask = lk->lk_data_count;
1136 /* old hsm agent to old MDS */
1137 if (!exp_connect_archive_id_array(exp))
1140 /* old hsm agent to new MDS */
1141 lk->lk_flags |= LK_FLG_DATANR;
1143 if (archive_mask == 0)
1146 count = hweight32(archive_mask);
1147 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1148 OBD_ALLOC(tmp, new_size);
1150 GOTO(out_lk, rc = -ENOMEM);
1152 memcpy(tmp, lk, size);
1153 tmp->lk_data_count = count;
1159 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1160 if (BIT(i) & archive_mask) {
1161 lk->lk_data[count] = i + 1;
1168 /* new hsm agent to new mds */
1169 if (lk->lk_data_count > 0) {
1170 new_size = offsetof(struct lustre_kernelcomm,
1171 lk_data[lk->lk_data_count]);
1172 OBD_ALLOC(tmp, new_size);
1174 GOTO(out_lk, rc = -ENOMEM);
1180 if (copy_from_user(lk, data, size))
1181 GOTO(out_lk, rc = -EFAULT);
1184 /* new hsm agent to old MDS */
1185 if (!exp_connect_archive_id_array(exp)) {
1188 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1189 GOTO(out_lk, rc = -EINVAL);
1191 for (i = 0; i < lk->lk_data_count; i++) {
1192 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1194 CERROR("%s: archive id %d requested but only [0 - %zu] supported: rc = %d\n",
1195 exp->exp_obd->obd_name, lk->lk_data[i],
1196 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1200 if (lk->lk_data[i] == 0) {
1205 archives |= (1 << (lk->lk_data[i] - 1));
1207 lk->lk_flags &= ~LK_FLG_DATANR;
1208 lk->lk_data_count = archives;
1211 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1217 static int check_owner(int type, int id)
1221 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1225 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1234 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1236 struct ll_sb_info *sbi = ll_s2sbi(sb);
1237 int cmd = qctl->qc_cmd;
1238 int type = qctl->qc_type;
1239 int id = qctl->qc_id;
1240 int valid = qctl->qc_valid;
1248 case LUSTRE_Q_SETDEFAULT:
1249 case LUSTRE_Q_SETQUOTAPOOL:
1250 case LUSTRE_Q_SETINFOPOOL:
1251 case LUSTRE_Q_SETDEFAULT_POOL:
1252 case LUSTRE_Q_DELETEQID:
1253 case LUSTRE_Q_RESETQID:
1254 if (!capable(CAP_SYS_ADMIN))
1257 if (sb->s_flags & SB_RDONLY)
1261 case LUSTRE_Q_GETDEFAULT:
1262 case LUSTRE_Q_GETQUOTAPOOL:
1263 case LUSTRE_Q_GETDEFAULT_POOL:
1264 if (check_owner(type, id) &&
1265 (!capable(CAP_SYS_ADMIN)))
1269 case LUSTRE_Q_GETINFOPOOL:
1272 CERROR("%s: unsupported quotactl op: %#x: rc = %d\n",
1273 sbi->ll_fsname, cmd, -EOPNOTSUPP);
1274 RETURN(-EOPNOTSUPP);
1277 if (valid != QC_GENERAL) {
1278 if (cmd == Q_GETINFO)
1279 qctl->qc_cmd = Q_GETOINFO;
1280 else if (cmd == Q_GETQUOTA ||
1281 cmd == LUSTRE_Q_GETQUOTAPOOL)
1282 qctl->qc_cmd = Q_GETOQUOTA;
1288 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1289 sizeof(*qctl), qctl, NULL);
1292 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1293 sizeof(*qctl), qctl, NULL);
1296 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1297 sizeof(*qctl), qctl, NULL);
1299 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1301 sizeof(*qctl), qctl, NULL);
1312 struct obd_quotactl *oqctl;
1313 int oqctl_len = sizeof(*oqctl);
1315 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1316 oqctl_len += LOV_MAXPOOLNAME + 1;
1318 OBD_ALLOC(oqctl, oqctl_len);
1322 QCTL_COPY(oqctl, qctl);
1323 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1325 OBD_FREE(oqctl, oqctl_len);
1328 /* If QIF_SPACE is not set, client should collect the
1329 * space usage from OSSs by itself
1331 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1332 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1333 !oqctl->qc_dqblk.dqb_curspace) {
1334 struct obd_quotactl *oqctl_tmp;
1335 int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1337 OBD_ALLOC(oqctl_tmp, qctl_len);
1338 if (oqctl_tmp == NULL)
1339 GOTO(out, rc = -ENOMEM);
1341 if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1342 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1343 memcpy(oqctl_tmp->qc_poolname,
1345 LOV_MAXPOOLNAME + 1);
1347 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1349 oqctl_tmp->qc_id = oqctl->qc_id;
1350 oqctl_tmp->qc_type = oqctl->qc_type;
1352 /* collect space usage from OSTs */
1353 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1354 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1355 if (!rc || rc == -EREMOTEIO) {
1356 oqctl->qc_dqblk.dqb_curspace =
1357 oqctl_tmp->qc_dqblk.dqb_curspace;
1358 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1361 /* collect space & inode usage from MDTs */
1362 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1363 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1364 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1365 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1366 if (!rc || rc == -EREMOTEIO) {
1367 oqctl->qc_dqblk.dqb_curspace +=
1368 oqctl_tmp->qc_dqblk.dqb_curspace;
1369 oqctl->qc_dqblk.dqb_curinodes =
1370 oqctl_tmp->qc_dqblk.dqb_curinodes;
1371 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1373 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1376 OBD_FREE(oqctl_tmp, qctl_len);
1379 QCTL_COPY(qctl, oqctl);
1380 OBD_FREE(oqctl, oqctl_len);
1386 static int ll_rmfid(struct file *file, void __user *arg)
1388 const struct fid_array __user *ufa = arg;
1389 struct inode *inode = file_inode(file);
1390 struct ll_sb_info *sbi = ll_i2sbi(inode);
1391 struct fid_array *lfa = NULL, *lfa_new = NULL;
1392 int i, rc, *rcs = NULL;
1394 bool lfa_flag = false; /* lfa already free'ed */
1398 if (!capable(CAP_DAC_READ_SEARCH) &&
1399 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1401 /* Only need to get the buflen */
1402 if (get_user(nr, &ufa->fa_nr))
1404 /* DoS protection */
1405 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1408 size = offsetof(struct fid_array, fa_fids[nr]);
1409 OBD_ALLOC(lfa, size);
1412 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1414 GOTO(free_lfa, rc = -ENOMEM);
1416 if (copy_from_user(lfa, arg, size))
1417 GOTO(free_rcs, rc = -EFAULT);
1419 /* In case of subdirectory mount, we need to make sure all the files
1420 * for which we want to remove FID are visible in the namespace.
1422 if (!fid_is_root(&sbi->ll_root_fid)) {
1423 int path_len = PATH_MAX, linkno;
1424 struct getinfo_fid2path *gf;
1425 int idx, last_idx = nr - 1;
1429 OBD_ALLOC(lfa_new, size);
1431 GOTO(free_rcs, rc = -ENOMEM);
1434 gf = kmalloc(sizeof(*gf) + path_len + 1, GFP_NOFS);
1436 GOTO(free_lfa_new, rc = -ENOMEM);
1438 for (idx = 0; idx < nr; idx++) {
1441 memset(gf, 0, sizeof(*gf) + path_len + 1);
1442 gf->gf_fid = lfa->fa_fids[idx];
1443 gf->gf_pathlen = path_len;
1444 gf->gf_linkno = linkno;
1445 rc = __ll_fid2path(inode, gf,
1446 sizeof(*gf) + gf->gf_pathlen,
1448 if (rc == -ENAMETOOLONG) {
1449 struct getinfo_fid2path *tmpgf;
1451 path_len += PATH_MAX;
1452 tmpgf = krealloc(gf,
1453 sizeof(*gf) + path_len + 1,
1457 GOTO(free_lfa_new, rc = -ENOMEM);
1464 if (gf->gf_linkno == linkno)
1466 linkno = gf->gf_linkno;
1470 /* All the links for this fid are visible in the
1471 * mounted subdir. So add it to the list of fids
1474 lfa_new->fa_fids[lfa_new->fa_nr++] =
1477 /* At least one link for this fid is not visible
1478 * in the mounted subdir. So add it at the end
1479 * of the list that will be hidden to lower
1480 * layers, and set -ENOENT as ret code.
1482 lfa_new->fa_fids[last_idx] = lfa->fa_fids[idx];
1483 rcs[last_idx--] = rc;
1487 OBD_FREE(lfa, size);
1491 if (lfa->fa_nr == 0)
1492 GOTO(free_rcs, rc = rcs[nr - 1]);
1494 /* Call mdc_iocontrol */
1495 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1498 for (i = 0; i < nr; i++)
1500 lfa->fa_fids[i].f_ver = rcs[i];
1501 if (copy_to_user(arg, lfa, size))
1506 OBD_FREE(lfa_new, size);
1508 OBD_FREE_PTR_ARRAY(rcs, nr);
1511 OBD_FREE(lfa, size);
1516 /* This function tries to get a single name component,
1517 * to send to the server. No actual path traversal involved,
1518 * so we limit to NAME_MAX */
1519 static char *ll_getname(const char __user *filename)
1524 OBD_ALLOC(tmp, NAME_MAX + 1);
1527 return ERR_PTR(-ENOMEM);
1529 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1532 else if (len > NAME_MAX)
1533 ret = -ENAMETOOLONG;
1536 OBD_FREE(tmp, NAME_MAX + 1);
1542 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
1544 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1546 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1548 struct dentry *dentry = file_dentry(file);
1549 struct inode *inode = file_inode(file);
1550 struct ll_sb_info *sbi = ll_i2sbi(inode);
1551 struct obd_ioctl_data *data = NULL;
1552 void __user *uarg = (void __user *)arg;
1556 CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
1557 PFID(ll_inode2fid(inode)), inode, cmd, arg);
1559 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1560 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1563 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1565 case IOC_MDC_LOOKUP: {
1566 int namelen, len = 0;
1569 rc = obd_ioctl_getdata(&data, &len, uarg);
1573 filename = data->ioc_inlbuf1;
1574 namelen = strlen(filename);
1576 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1577 GOTO(out_free, rc = -EINVAL);
1580 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1582 CERROR("%s: lookup %.*s failed: rc = %d\n",
1583 sbi->ll_fsname, namelen, filename, rc);
1587 OBD_FREE_LARGE(data, len);
1590 case LL_IOC_LMV_SETSTRIPE: {
1591 struct lmv_user_md *lum;
1596 bool createonly = false;
1600 rc = obd_ioctl_getdata(&data, &len, uarg);
1604 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1605 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1606 GOTO(lmv_out_free, rc = -EINVAL);
1608 filename = data->ioc_inlbuf1;
1609 namelen = data->ioc_inllen1;
1612 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1613 GOTO(lmv_out_free, rc = -EINVAL);
1615 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1616 lumlen = data->ioc_inllen2;
1618 if (!lmv_user_magic_supported(lum->lum_magic)) {
1619 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1620 lum->lum_magic, -EINVAL);
1621 GOTO(lmv_out_free, rc = -EINVAL);
1624 if ((lum->lum_magic == LMV_USER_MAGIC ||
1625 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1626 lumlen < sizeof(*lum)) {
1627 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1628 filename, lumlen, lum->lum_magic, -EINVAL);
1629 GOTO(lmv_out_free, rc = -EINVAL);
1632 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1633 lumlen < sizeof(struct lmv_foreign_md)) {
1634 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1635 filename, lum->lum_magic, lumlen, -EFAULT);
1636 GOTO(lmv_out_free, rc = -EINVAL);
1639 mode = data->ioc_type;
1640 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1641 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1644 OBD_FREE_LARGE(data, len);
1648 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1649 struct lmv_user_md lum;
1650 struct lmv_user_md __user *ulump = uarg;
1653 if (copy_from_user(&lum, ulump, sizeof(lum)))
1656 if (lum.lum_magic != LMV_USER_MAGIC)
1659 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1663 case LL_IOC_LOV_SETSTRIPE_NEW:
1664 case LL_IOC_LOV_SETSTRIPE: {
1665 struct lov_user_md_v3 *lumv3 = NULL;
1666 struct lov_user_md_v1 lumv1;
1667 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1668 struct lov_user_md_v1 __user *lumv1p = uarg;
1669 struct lov_user_md_v3 __user *lumv3p = uarg;
1671 int set_default = 0;
1673 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1674 sizeof(struct lov_comp_md_v1));
1675 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1676 /* first try with v1 which is smaller than v3 */
1677 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1680 if (is_root_inode(inode))
1683 switch (lumv1.lmm_magic) {
1684 case LOV_USER_MAGIC_V3:
1685 case LOV_USER_MAGIC_SPECIFIC:
1686 lum_size = ll_lov_user_md_size(&lumv1);
1689 OBD_ALLOC(lumv3, lum_size);
1692 if (copy_from_user(lumv3, lumv3p, lum_size))
1693 GOTO(out, rc = -EFAULT);
1694 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1696 case LOV_USER_MAGIC_V1:
1699 GOTO(out, rc = -EOPNOTSUPP);
1702 /* in v1 and v3 cases lumv1 points to data */
1703 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1706 OBD_FREE(lumv3, lum_size);
1709 case LL_IOC_LMV_GETSTRIPE: {
1710 struct lmv_user_md __user *ulmv = uarg;
1711 struct lmv_user_md lum;
1712 struct ptlrpc_request *request = NULL;
1713 union lmv_mds_md *lmm = NULL;
1716 struct lmv_user_md *tmp = NULL;
1720 int max_stripe_count;
1724 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1727 /* get default LMV */
1728 if (lum.lum_magic == LMV_USER_MAGIC &&
1729 lum.lum_type != LMV_TYPE_RAW) {
1730 rc = ll_dir_get_default_lmv(inode, &lum);
1734 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1740 max_stripe_count = lum.lum_stripe_count;
1741 /* lum_magic will indicate which stripe the ioctl will like
1742 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1743 * is for default LMV stripe */
1744 if (lum.lum_magic == LMV_MAGIC_V1)
1745 valid |= OBD_MD_MEA;
1746 else if (lum.lum_magic == LMV_USER_MAGIC)
1747 valid |= OBD_MD_DEFAULT_MEA;
1751 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1752 &request, NULL, valid);
1754 GOTO(finish_req, rc);
1756 /* get default LMV in raw mode */
1757 if (lum.lum_magic == LMV_USER_MAGIC) {
1758 if (copy_to_user(ulmv, lmm, lmmsize))
1759 GOTO(finish_req, rc = -EFAULT);
1760 GOTO(finish_req, rc);
1763 /* if foreign LMV case, fake stripes number */
1764 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1765 struct lmv_foreign_md *lfm;
1767 lfm = (struct lmv_foreign_md *)lmm;
1768 if (lfm->lfm_length < XATTR_SIZE_MAX -
1769 offsetof(typeof(*lfm), lfm_value)) {
1770 __u32 size = lfm->lfm_length +
1771 offsetof(typeof(*lfm), lfm_value);
1773 stripe_count = lmv_foreign_to_md_stripes(size);
1775 CERROR("%s: invalid %d foreign size returned: rc = %d\n",
1776 sbi->ll_fsname, lfm->lfm_length,
1781 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1783 if (max_stripe_count < stripe_count) {
1784 lum.lum_stripe_count = stripe_count;
1785 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1786 GOTO(finish_req, rc = -EFAULT);
1787 GOTO(finish_req, rc = -E2BIG);
1790 /* enough room on user side and foreign case */
1791 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1792 struct lmv_foreign_md *lfm;
1795 lfm = (struct lmv_foreign_md *)lmm;
1796 size = lfm->lfm_length +
1797 offsetof(struct lmv_foreign_md, lfm_value);
1798 if (copy_to_user(ulmv, lfm, size))
1799 GOTO(finish_req, rc = -EFAULT);
1800 GOTO(finish_req, rc);
1803 lum_size = lmv_user_md_size(stripe_count,
1804 LMV_USER_MAGIC_SPECIFIC);
1805 OBD_ALLOC(tmp, lum_size);
1807 GOTO(finish_req, rc = -ENOMEM);
1809 mdt_index = ll_get_mdt_idx(inode);
1811 GOTO(out_tmp, rc = -ENOMEM);
1813 tmp->lum_magic = LMV_MAGIC_V1;
1814 tmp->lum_stripe_count = 0;
1815 tmp->lum_stripe_offset = mdt_index;
1816 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1817 for (i = 0; i < stripe_count; i++) {
1820 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1821 if (fid_is_sane(&fid)) {
1822 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1824 GOTO(out_tmp, rc = mdt_index);
1826 tmp->lum_objects[i].lum_mds = mdt_index;
1827 tmp->lum_objects[i].lum_fid = fid;
1830 tmp->lum_stripe_count++;
1833 if (copy_to_user(ulmv, tmp, lum_size))
1834 GOTO(out_tmp, rc = -EFAULT);
1836 OBD_FREE(tmp, lum_size);
1838 ptlrpc_req_finished(request);
1841 case LL_IOC_REMOVE_ENTRY: {
1842 char *filename = NULL;
1846 /* Here is a little hack to avoid sending REINT_RMENTRY to
1847 * unsupported server, which might crash the server(LU-2730),
1848 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1849 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1850 * server will support REINT_RMENTRY XXX*/
1851 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1852 RETURN(-EOPNOTSUPP);
1854 filename = ll_getname(uarg);
1855 if (IS_ERR(filename))
1856 RETURN(PTR_ERR(filename));
1858 namelen = strlen(filename);
1860 GOTO(out_rmdir, rc = -EINVAL);
1862 rc = ll_rmdir_entry(inode, filename, namelen);
1865 ll_putname(filename);
1869 RETURN(ll_rmfid(file, uarg));
1870 case LL_IOC_LOV_SWAP_LAYOUTS:
1872 case LL_IOC_LOV_GETSTRIPE:
1873 case LL_IOC_LOV_GETSTRIPE_NEW:
1874 case LL_IOC_MDC_GETINFO_V1:
1875 case LL_IOC_MDC_GETINFO_V2:
1876 case IOC_MDC_GETFILEINFO_V1:
1877 case IOC_MDC_GETFILEINFO_V2:
1878 case IOC_MDC_GETFILESTRIPE: {
1879 struct ptlrpc_request *request = NULL;
1880 struct ptlrpc_request *root_request = NULL;
1881 struct lov_user_md __user *lump;
1882 struct lov_mds_md *lmm = NULL;
1883 struct mdt_body *body;
1884 char *filename = NULL;
1885 lstat_t __user *statp = NULL;
1886 lstatx_t __user *stxp = NULL;
1887 __u64 __user *flagsp = NULL;
1888 __u32 __user *lmmsizep = NULL;
1889 struct lu_fid __user *fidp = NULL;
1893 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1894 cmd == IOC_MDC_GETFILEINFO_V2 ||
1895 cmd == IOC_MDC_GETFILESTRIPE) {
1896 filename = ll_getname(uarg);
1897 if (IS_ERR(filename))
1898 RETURN(PTR_ERR(filename));
1900 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1901 &lmmsize, &request);
1903 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1909 body = req_capsule_server_get(&request->rq_pill,
1911 LASSERT(body != NULL);
1916 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1917 cmd == LL_IOC_MDC_GETINFO_V1 ||
1918 cmd == IOC_MDC_GETFILEINFO_V2 ||
1919 cmd == LL_IOC_MDC_GETINFO_V2)) {
1927 if (cmd == IOC_MDC_GETFILESTRIPE ||
1928 cmd == LL_IOC_LOV_GETSTRIPE ||
1929 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1931 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1932 cmd == LL_IOC_MDC_GETINFO_V1){
1933 struct lov_user_mds_data_v1 __user *lmdp;
1936 statp = &lmdp->lmd_st;
1937 lump = &lmdp->lmd_lmm;
1939 struct lov_user_mds_data __user *lmdp;
1942 fidp = &lmdp->lmd_fid;
1943 stxp = &lmdp->lmd_stx;
1944 flagsp = &lmdp->lmd_flags;
1945 lmmsizep = &lmdp->lmd_lmmsize;
1946 lump = &lmdp->lmd_lmm;
1950 /* If the file has no striping then zero out *lump so
1951 * that the caller isn't confused by garbage. */
1952 if (clear_user(lump, sizeof(*lump)))
1953 GOTO(out_req, rc = -EFAULT);
1954 } else if (copy_to_user(lump, lmm, lmmsize)) {
1955 if (copy_to_user(lump, lmm, sizeof(*lump)))
1956 GOTO(out_req, rc = -EFAULT);
1959 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1961 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1962 cmd == LL_IOC_MDC_GETINFO_V1) {
1965 st.st_dev = inode->i_sb->s_dev;
1966 st.st_mode = body->mbo_mode;
1967 st.st_nlink = body->mbo_nlink;
1968 st.st_uid = body->mbo_uid;
1969 st.st_gid = body->mbo_gid;
1970 st.st_rdev = body->mbo_rdev;
1971 if (llcrypt_require_key(inode) == -ENOKEY)
1972 st.st_size = round_up(st.st_size,
1973 LUSTRE_ENCRYPTION_UNIT_SIZE);
1975 st.st_size = body->mbo_size;
1976 st.st_blksize = PAGE_SIZE;
1977 st.st_blocks = body->mbo_blocks;
1978 st.st_atime = body->mbo_atime;
1979 st.st_mtime = body->mbo_mtime;
1980 st.st_ctime = body->mbo_ctime;
1981 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1984 if (copy_to_user(statp, &st, sizeof(st)))
1985 GOTO(out_req, rc = -EFAULT);
1986 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1987 cmd == LL_IOC_MDC_GETINFO_V2) {
1988 lstatx_t stx = { 0 };
1989 __u64 valid = body->mbo_valid;
1991 stx.stx_blksize = PAGE_SIZE;
1992 stx.stx_nlink = body->mbo_nlink;
1993 stx.stx_uid = body->mbo_uid;
1994 stx.stx_gid = body->mbo_gid;
1995 stx.stx_mode = body->mbo_mode;
1996 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1998 if (llcrypt_require_key(inode) == -ENOKEY)
1999 stx.stx_size = round_up(stx.stx_size,
2000 LUSTRE_ENCRYPTION_UNIT_SIZE);
2002 stx.stx_size = body->mbo_size;
2003 stx.stx_blocks = body->mbo_blocks;
2004 stx.stx_atime.tv_sec = body->mbo_atime;
2005 stx.stx_ctime.tv_sec = body->mbo_ctime;
2006 stx.stx_mtime.tv_sec = body->mbo_mtime;
2007 stx.stx_btime.tv_sec = body->mbo_btime;
2008 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
2009 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
2010 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
2011 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
2012 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
2014 stx.stx_attributes_mask = STATX_ATTR_IMMUTABLE |
2016 #ifdef HAVE_LUSTRE_CRYPTO
2017 stx.stx_attributes_mask |= STATX_ATTR_ENCRYPTED;
2019 if (body->mbo_valid & OBD_MD_FLFLAGS) {
2020 stx.stx_attributes |= body->mbo_flags;
2021 /* if Lustre specific LUSTRE_ENCRYPT_FL flag is
2022 * set, also set ext4 equivalent to please statx
2024 if (body->mbo_flags & LUSTRE_ENCRYPT_FL)
2025 stx.stx_attributes |= STATX_ATTR_ENCRYPTED;
2028 /* For a striped directory, the size and blocks returned
2029 * from MDT is not correct.
2030 * The size and blocks are aggregated by client across
2032 * Thus for a striped directory, do not return the valid
2033 * FLSIZE and FLBLOCKS flags to the caller.
2034 * However, this whould be better decided by the MDS
2035 * instead of the client.
2037 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
2038 ll_dir_striped(inode))
2039 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
2041 if (flagsp && copy_to_user(flagsp, &valid,
2043 GOTO(out_req, rc = -EFAULT);
2045 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
2047 GOTO(out_req, rc = -EFAULT);
2049 if (!(valid & OBD_MD_FLSIZE))
2050 stx.stx_mask &= ~STATX_SIZE;
2051 if (!(valid & OBD_MD_FLBLOCKS))
2052 stx.stx_mask &= ~STATX_BLOCKS;
2054 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
2055 GOTO(out_req, rc = -EFAULT);
2057 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2059 GOTO(out_req, rc = -EFAULT);
2064 ptlrpc_req_finished(request);
2065 ptlrpc_req_finished(root_request);
2067 ll_putname(filename);
2070 case OBD_IOC_QUOTACTL: {
2071 struct if_quotactl *qctl;
2072 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2074 OBD_ALLOC(qctl, qctl_len);
2078 if (copy_from_user(qctl, uarg, sizeof(*qctl)))
2079 GOTO(out_quotactl, rc = -EFAULT);
2081 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2082 char __user *from = uarg +
2083 offsetof(typeof(*qctl), qc_poolname);
2084 if (copy_from_user(qctl->qc_poolname, from,
2085 LOV_MAXPOOLNAME + 1))
2086 GOTO(out_quotactl, rc = -EFAULT);
2089 rc = quotactl_ioctl(inode->i_sb, qctl);
2090 if ((rc == 0 || rc == -ENODATA) &&
2091 copy_to_user(uarg, qctl, sizeof(*qctl)))
2094 OBD_FREE(qctl, qctl_len);
2097 case LL_IOC_GETOBDCOUNT: {
2099 struct obd_export *exp;
2101 if (copy_from_user(&count, uarg, sizeof(count)))
2104 /* get ost count when count is zero, get mdt count otherwise */
2105 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2106 vallen = sizeof(count);
2107 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2108 KEY_TGT_COUNT, &vallen, &count);
2110 CERROR("%s: get target count failed: rc = %d\n",
2111 sbi->ll_fsname, rc);
2115 if (copy_to_user(uarg, &count, sizeof(count)))
2120 case LL_IOC_GET_CONNECT_FLAGS:
2121 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, uarg));
2122 case LL_IOC_FID2MDTIDX: {
2123 struct obd_export *exp = ll_i2mdexp(inode);
2127 if (copy_from_user(&fid, uarg, sizeof(fid)))
2130 /* Call mdc_iocontrol */
2131 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2132 (__u32 __user *)&index);
2138 case LL_IOC_HSM_REQUEST: {
2139 struct hsm_user_request *hur;
2146 /* We don't know the true size yet; copy the fixed-size part */
2147 if (copy_from_user(hur, uarg, sizeof(*hur))) {
2152 /* Compute the whole struct size */
2153 totalsize = hur_len(hur);
2158 /* Final size will be more than double totalsize */
2159 if (totalsize >= MDS_MAXREQSIZE / 3)
2162 OBD_ALLOC_LARGE(hur, totalsize);
2166 /* Copy the whole struct */
2167 if (copy_from_user(hur, uarg, totalsize))
2168 GOTO(out_hur, rc = -EFAULT);
2170 if (hur->hur_request.hr_action == HUA_RELEASE) {
2171 const struct lu_fid *fid;
2175 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2176 fid = &hur->hur_user_item[i].hui_fid;
2177 f = search_inode_for_lustre(inode->i_sb, fid);
2183 rc = ll_hsm_release(f);
2189 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2193 OBD_FREE_LARGE(hur, totalsize);
2197 case LL_IOC_HSM_PROGRESS: {
2198 struct hsm_progress_kernel hpk;
2199 struct hsm_progress hp;
2201 if (copy_from_user(&hp, uarg, sizeof(hp)))
2204 hpk.hpk_fid = hp.hp_fid;
2205 hpk.hpk_cookie = hp.hp_cookie;
2206 hpk.hpk_extent = hp.hp_extent;
2207 hpk.hpk_flags = hp.hp_flags;
2208 hpk.hpk_errval = hp.hp_errval;
2209 hpk.hpk_data_version = 0;
2211 /* File may not exist in Lustre; all progress
2212 * reported to Lustre root */
2213 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2217 case LL_IOC_HSM_CT_START:
2218 if (!capable(CAP_SYS_ADMIN))
2221 rc = copy_and_ct_start(cmd, sbi->ll_md_exp, uarg);
2224 case LL_IOC_HSM_COPY_START: {
2225 struct hsm_copy *copy;
2228 OBD_ALLOC_PTR(copy);
2231 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2236 rc = ll_ioc_copy_start(inode->i_sb, copy);
2237 if (copy_to_user(uarg, copy, sizeof(*copy)))
2243 case LL_IOC_HSM_COPY_END: {
2244 struct hsm_copy *copy;
2247 OBD_ALLOC_PTR(copy);
2250 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2255 rc = ll_ioc_copy_end(inode->i_sb, copy);
2256 if (copy_to_user(uarg, copy, sizeof(*copy)))
2262 case LL_IOC_MIGRATE: {
2263 struct lmv_user_md *lum;
2270 rc = obd_ioctl_getdata(&data, &len, uarg);
2274 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2275 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2276 GOTO(migrate_free, rc = -EINVAL);
2278 filename = data->ioc_inlbuf1;
2279 namelen = data->ioc_inllen1;
2280 flags = data->ioc_type;
2282 if (namelen < 1 || namelen != strlen(filename) + 1) {
2283 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2284 GOTO(migrate_free, rc = -EINVAL);
2287 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2288 if (lum->lum_magic != LMV_USER_MAGIC &&
2289 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2291 CERROR("%s: wrong lum magic %x: rc = %d\n",
2292 filename, lum->lum_magic, rc);
2293 GOTO(migrate_free, rc);
2296 rc = ll_migrate(inode, file, lum, filename, flags);
2298 OBD_FREE_LARGE(data, len);
2302 case LL_IOC_LADVISE2: {
2303 struct llapi_lu_ladvise2 *ladvise;
2305 OBD_ALLOC_PTR(ladvise);
2306 if (ladvise == NULL)
2309 if (copy_from_user(ladvise, uarg, sizeof(*ladvise)))
2310 GOTO(out_ladvise, rc = -EFAULT);
2312 switch (ladvise->lla_advice) {
2313 case LU_LADVISE_AHEAD:
2314 if (ladvise->lla_start >= ladvise->lla_end) {
2316 "%s: Invalid range (%llu %llu) for %s\n",
2317 sbi->ll_fsname, ladvise->lla_start,
2319 ladvise_names[ladvise->lla_advice]);
2320 GOTO(out_ladvise, rc = -EINVAL);
2324 * Currently we only support name indexing format
2327 if (ladvise->lla_ahead_mode != LU_AH_NAME_INDEX) {
2329 "%s: Invalid access mode (%d) for %s\n",
2330 sbi->ll_fsname, ladvise->lla_ahead_mode,
2331 ladvise_names[ladvise->lla_advice]);
2332 GOTO(out_ladvise, rc = -EINVAL);
2335 /* Currently we only support stat-ahead operations. */
2336 if (!(ladvise->lla_access_flags & ACCESS_FL_STAT)) {
2338 "%s: Invalid access flags (%x) for %s\n",
2340 ladvise->lla_access_flags,
2341 ladvise_names[ladvise->lla_advice]);
2342 GOTO(out_ladvise, rc = -EINVAL);
2345 rc = ll_ioctl_ahead(file, ladvise);
2351 OBD_FREE_PTR(ladvise);
2354 case LL_IOC_PCC_DETACH_BY_FID: {
2355 struct lu_pcc_detach_fid *detach;
2357 struct inode *inode2;
2360 OBD_ALLOC_PTR(detach);
2364 if (copy_from_user(detach, uarg, sizeof(*detach)))
2365 GOTO(out_detach, rc = -EFAULT);
2367 fid = &detach->pccd_fid;
2368 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2369 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2371 /* Target inode is not in inode cache, and PCC file
2372 * has aleady released, return immdiately.
2374 GOTO(out_detach, rc = 0);
2376 if (!S_ISREG(inode2->i_mode))
2377 GOTO(out_iput, rc = -EINVAL);
2379 if (!inode_owner_or_capable(&nop_mnt_idmap, inode2))
2380 GOTO(out_iput, rc = -EPERM);
2382 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2386 OBD_FREE_PTR(detach);
2390 rc = ll_iocontrol(inode, file, cmd, uarg);
2393 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL, uarg));
2397 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2399 struct inode *inode = file->f_mapping->host;
2400 struct ll_file_data *fd = file->private_data;
2401 struct ll_sb_info *sbi = ll_i2sbi(inode);
2402 int api32 = ll_need_32bit_api(sbi);
2403 loff_t ret = -EINVAL;
2406 ll_inode_lock(inode);
2411 offset += file->f_pos;
2417 offset += LL_DIR_END_OFF_32BIT;
2419 offset += LL_DIR_END_OFF;
2426 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2427 (!api32 && offset <= LL_DIR_END_OFF))) {
2428 if (offset != file->f_pos) {
2431 hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2432 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2433 (!api32 && offset == LL_DIR_END_OFF))
2434 fd->lfd_pos = MDS_DIR_END_OFF;
2435 else if (api32 && hash64)
2436 fd->lfd_pos = offset << 32;
2438 fd->lfd_pos = offset;
2439 file->f_pos = offset;
2440 file->f_version = 0;
2447 ll_inode_unlock(inode);
2451 static int ll_dir_open(struct inode *inode, struct file *file)
2454 RETURN(ll_file_open(inode, file));
2457 static int ll_dir_release(struct inode *inode, struct file *file)
2460 RETURN(ll_file_release(inode, file));
2463 /* notify error if partially read striped directory */
2464 static int ll_dir_flush(struct file *file, fl_owner_t id)
2466 struct ll_file_data *lfd = file->private_data;
2467 int rc = lfd->fd_partial_readdir_rc;
2469 lfd->fd_partial_readdir_rc = 0;
2474 const struct file_operations ll_dir_operations = {
2475 .llseek = ll_dir_seek,
2476 .open = ll_dir_open,
2477 .release = ll_dir_release,
2478 .read = generic_read_dir,
2479 #ifdef HAVE_DIR_CONTEXT
2480 .iterate_shared = ll_iterate,
2482 .readdir = ll_readdir,
2484 .unlocked_ioctl = ll_dir_ioctl,
2486 .flush = ll_dir_flush,