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, original
132 * identification of and access to overflow pages
136 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
137 * a header lu_dirpage which describes the start/end hash, and whether this
138 * page is empty (contains no dir entry) or hash collide with next page.
139 * After client receives reply, several pages will be integrated into dir page
140 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
141 * lu_dirpage for this integrated page will be adjusted. See
142 * mdc_adjust_dirpages().
145 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
146 __u64 offset, int *partial_readdir_rc)
148 struct md_readdir_info mrinfo = {
149 .mr_blocking_ast = ll_md_blocking_ast };
153 rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
157 if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
158 *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
163 void ll_release_page(struct inode *inode, struct page *page,
168 /* Always remove the page for striped dir, because the page is
169 * built from temporarily in LMV layer */
170 if (inode && S_ISDIR(inode->i_mode) &&
171 lmv_dir_striped(ll_i2info(inode)->lli_lsm_md)) {
178 if (likely(page->mapping != NULL))
179 cfs_delete_from_page_cache(page);
185 #ifdef HAVE_DIR_CONTEXT
186 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
187 struct dir_context *ctx, int *partial_readdir_rc)
190 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
191 void *cookie, filldir_t filldir, int *partial_readdir_rc)
194 struct ll_sb_info *sbi = ll_i2sbi(inode);
196 bool is_api32 = ll_need_32bit_api(sbi);
197 bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
200 struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
204 if (IS_ENCRYPTED(inode)) {
205 rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
210 page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
212 while (rc == 0 && !done) {
213 struct lu_dirpage *dp;
214 struct lu_dirent *ent;
223 hash = MDS_DIR_END_OFF;
224 dp = page_address(page);
225 for (ent = lu_dirent_start(dp); ent != NULL && !done;
226 ent = lu_dirent_next(ent)) {
233 hash = le64_to_cpu(ent->lde_hash);
234 if (hash < pos) /* Skip until we find target hash */
237 namelen = le16_to_cpu(ent->lde_namelen);
238 if (namelen == 0) /* Skip dummy record */
241 if (is_api32 && is_hash64)
245 fid_le_to_cpu(&fid, &ent->lde_fid);
246 ino = cl_fid_build_ino(&fid, is_api32);
247 type = S_DT(lu_dirent_type_get(ent));
248 /* For ll_nfs_get_name_filldir(), it will try to access
249 * 'ent' through 'lde_name', so the parameter 'name'
250 * for 'filldir()' must be part of the 'ent'. */
251 #ifdef HAVE_DIR_CONTEXT
253 if (!IS_ENCRYPTED(inode)) {
254 done = !dir_emit(ctx, ent->lde_name, namelen,
257 /* Directory is encrypted */
258 int save_len = lltr.len;
259 struct llcrypt_str de_name =
260 LLTR_INIT(ent->lde_name, namelen);
262 rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
270 done = !dir_emit(ctx, de_name.name, de_name.len,
274 /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
275 * IS_ENCRYPTED is brought by kernel 4.14.
276 * So there is no need to handle encryption case here.
278 done = filldir(cookie, ent->lde_name, namelen, lhash,
285 ll_release_page(inode, page, false);
289 next = le64_to_cpu(dp->ldp_hash_end);
291 if (pos == MDS_DIR_END_OFF) {
293 * End of directory reached.
296 ll_release_page(inode, page, false);
299 * Normal case: continue to the next
302 ll_release_page(inode, page,
303 le32_to_cpu(dp->ldp_flags) &
306 page = ll_get_dir_page(inode, op_data, pos,
310 #ifdef HAVE_DIR_CONTEXT
315 llcrypt_fname_free_buffer(&lltr);
319 #ifdef HAVE_DIR_CONTEXT
320 static int ll_iterate(struct file *filp, struct dir_context *ctx)
322 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
325 struct inode *inode = file_inode(filp);
326 struct ll_file_data *lfd = filp->private_data;
327 struct ll_sb_info *sbi = ll_i2sbi(inode);
328 bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
329 int api32 = ll_need_32bit_api(sbi);
330 struct md_op_data *op_data;
331 struct lu_fid pfid = { 0 };
332 ktime_t kstart = ktime_get();
333 /* result of possible partial readdir */
334 int partial_readdir_rc = 0;
340 LASSERT(lfd != NULL);
344 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
345 PFID(ll_inode2fid(inode)),
346 inode, (unsigned long)pos, i_size_read(inode), api32);
348 if (IS_ENCRYPTED(inode)) {
349 rc = llcrypt_prepare_readdir(inode);
350 if (rc && rc != -ENOKEY)
354 if (pos == MDS_DIR_END_OFF)
360 if (unlikely(ll_dir_striped(inode))) {
362 * This is only needed for striped dir to fill ..,
363 * see lmv_read_page()
365 if (file_dentry(filp)->d_parent != NULL &&
366 file_dentry(filp)->d_parent->d_inode != NULL) {
367 __u64 ibits = MDS_INODELOCK_LOOKUP;
368 struct inode *parent =
369 file_dentry(filp)->d_parent->d_inode;
371 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
372 pfid = *ll_inode2fid(parent);
375 /* If it can not find in cache, do lookup .. on the master
377 if (fid_is_zero(&pfid)) {
378 rc = ll_dir_get_parent_fid(inode, &pfid);
384 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
385 LUSTRE_OPC_ANY, inode);
387 GOTO(out, rc = PTR_ERR(op_data));
389 /* foreign dirs are browsed out of Lustre */
390 if (unlikely(op_data->op_mea1 != NULL &&
391 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
392 ll_finish_md_op_data(op_data);
396 op_data->op_fid3 = pfid;
398 #ifdef HAVE_DIR_CONTEXT
400 rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
403 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
404 &partial_readdir_rc);
407 if (!lfd->fd_partial_readdir_rc)
408 lfd->fd_partial_readdir_rc = partial_readdir_rc;
410 if (pos == MDS_DIR_END_OFF) {
412 pos = LL_DIR_END_OFF_32BIT;
414 pos = LL_DIR_END_OFF;
419 #ifdef HAVE_DIR_CONTEXT
424 ll_finish_md_op_data(op_data);
428 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
429 ktime_us_delta(ktime_get(), kstart));
435 * Create striped directory with specified stripe(@lump)
437 * \param[in] dparent the parent of the directory.
438 * \param[in] lump the specified stripes.
439 * \param[in] dirname the name of the directory.
440 * \param[in] mode the specified mode of the directory.
442 * \retval =0 if striped directory is being created successfully.
443 * <0 if the creation is failed.
445 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
446 size_t len, const char *dirname, umode_t mode,
449 struct inode *parent = dparent->d_inode;
450 struct ptlrpc_request *request = NULL;
451 struct md_op_data *op_data;
452 struct ll_sb_info *sbi = ll_i2sbi(parent);
453 struct inode *inode = NULL;
454 struct dentry dentry = {
458 .len = strlen(dirname),
459 .hash = ll_full_name_hash(dparent, dirname,
462 .d_sb = dparent->d_sb,
464 bool encrypt = false;
469 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
472 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
474 "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
475 PFID(ll_inode2fid(parent)), parent, dirname,
476 (int)lump->lum_stripe_offset, lump->lum_stripe_count,
477 lump->lum_hash_type);
479 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
482 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
483 PFID(ll_inode2fid(parent)), parent, dirname,
484 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
487 if (lump->lum_stripe_count > 1 &&
488 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
491 if (IS_DEADDIR(parent) &&
492 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
495 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
496 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
498 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
499 lump->lum_hash_type = cfs_fail_val;
500 } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
501 enum lmv_hash_type type = lump->lum_hash_type &
504 if (type >= LMV_HASH_TYPE_CRUSH ||
505 type == LMV_HASH_TYPE_UNKNOWN)
506 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
507 LMV_HASH_TYPE_FNV_1A_64;
510 hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
511 if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
514 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
515 lustre_swab_lmv_user_md(lump);
517 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
518 mode &= ~current_umask();
519 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
520 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
521 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
524 RETURN(PTR_ERR(op_data));
526 op_data->op_dir_depth = ll_i2info(parent)->lli_inherit_depth ?:
527 ll_i2info(parent)->lli_dir_depth;
529 if (ll_sbi_has_encrypt(sbi) &&
530 (IS_ENCRYPTED(parent) ||
531 unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
532 err = llcrypt_prepare_readdir(parent);
534 GOTO(out_op_data, err);
535 if (!llcrypt_has_encryption_key(parent))
536 GOTO(out_op_data, err = -ENOKEY);
540 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
541 /* selinux_dentry_init_security() uses dentry->d_parent and name
542 * to determine the security context for the file. So our fake
543 * dentry should be real enough for this purpose. */
544 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
545 &op_data->op_file_secctx_name,
546 &op_data->op_file_secctx_name_size,
547 &op_data->op_file_secctx,
548 &op_data->op_file_secctx_size);
550 GOTO(out_op_data, err);
554 err = llcrypt_inherit_context(parent, NULL, op_data, false);
556 GOTO(out_op_data, err);
559 op_data->op_cli_flags |= CLI_SET_MEA;
561 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
563 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
564 from_kuid(&init_user_ns, current_fsuid()),
565 from_kgid(&init_user_ns, current_fsgid()),
566 current_cap(), 0, &request);
568 GOTO(out_request, err);
570 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
572 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
574 GOTO(out_inode, err);
576 dentry.d_inode = inode;
578 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags))
579 err = ll_inode_notifysecctx(inode, op_data->op_file_secctx,
580 op_data->op_file_secctx_size);
582 err = ll_inode_init_security(&dentry, inode, parent);
585 GOTO(out_inode, err);
588 err = ll_set_encflags(inode, op_data->op_file_encctx,
589 op_data->op_file_encctx_size, false);
591 GOTO(out_inode, err);
597 ptlrpc_req_finished(request);
599 ll_finish_md_op_data(op_data);
604 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
607 struct ll_sb_info *sbi = ll_i2sbi(inode);
608 struct md_op_data *op_data;
609 struct ptlrpc_request *req = NULL;
615 switch (lump->lmm_magic) {
616 case LOV_USER_MAGIC_V1:
617 lum_size = sizeof(struct lov_user_md_v1);
619 case LOV_USER_MAGIC_V3:
620 lum_size = sizeof(struct lov_user_md_v3);
622 case LOV_USER_MAGIC_COMP_V1:
623 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
625 case LMV_USER_MAGIC: {
626 struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
628 /* MDS < 2.14 doesn't support 'crush' hash type, and
629 * cannot handle unknown hash if client doesn't set a
630 * valid one. switch to fnv_1a_64.
632 if (!(exp_connect_flags2(sbi->ll_md_exp) &
633 OBD_CONNECT2_CRUSH)) {
634 enum lmv_hash_type type = lmv->lum_hash_type &
637 if (type >= LMV_HASH_TYPE_CRUSH ||
638 type == LMV_HASH_TYPE_UNKNOWN)
640 (lmv->lum_hash_type ^ type) |
641 LMV_HASH_TYPE_FNV_1A_64;
643 if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
644 lustre_swab_lmv_user_md(lmv);
645 lum_size = sizeof(*lmv);
648 case LOV_USER_MAGIC_SPECIFIC: {
649 struct lov_user_md_v3 *v3 =
650 (struct lov_user_md_v3 *)lump;
651 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
653 lum_size = lov_user_md_size(v3->lmm_stripe_count,
654 LOV_USER_MAGIC_SPECIFIC);
658 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
659 " %#08x != %#08x nor %#08x\n",
660 lump->lmm_magic, LOV_USER_MAGIC_V1,
666 * This is coming from userspace, so should be in
667 * local endian. But the MDS would like it in little
668 * endian, so we swab it before we send it.
670 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
671 le32_to_cpu(LOV_MAGIC_MAGIC))
672 lustre_swab_lov_user_md(lump, 0);
674 lum_size = sizeof(struct lov_user_md_v1);
677 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
678 LUSTRE_OPC_ANY, NULL);
680 RETURN(PTR_ERR(op_data));
682 /* swabbing is done in lov_setstripe() on server side */
683 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
684 ll_finish_md_op_data(op_data);
685 ptlrpc_req_finished(req);
692 int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
693 struct ptlrpc_request **request, u64 valid,
694 enum get_default_layout_type type)
696 struct ll_sb_info *sbi = ll_i2sbi(inode);
697 struct mdt_body *body;
698 struct lov_mds_md *lmm = NULL;
699 struct ptlrpc_request *req = NULL;
700 int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
701 struct md_op_data *op_data;
707 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
708 LUSTRE_OPC_ANY, NULL);
710 RETURN(PTR_ERR(op_data));
712 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
714 if (type == GET_DEFAULT_LAYOUT_ROOT) {
715 lu_root_fid(&op_data->op_fid1);
716 fid = op_data->op_fid1;
718 fid = *ll_inode2fid(inode);
721 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
722 ll_finish_md_op_data(op_data);
724 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
729 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
730 LASSERT(body != NULL);
732 lmm_size = body->mbo_eadatasize;
734 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
736 GOTO(out, rc = -ENODATA);
739 lmm = req_capsule_server_sized_get(&req->rq_pill,
740 &RMF_MDT_MD, lmm_size);
741 LASSERT(lmm != NULL);
744 * This is coming from the MDS, so is probably in
745 * little endian. We convert it to host endian before
746 * passing it to userspace.
748 /* We don't swab objects for directories */
749 switch (le32_to_cpu(lmm->lmm_magic)) {
752 case LOV_MAGIC_COMP_V1:
753 case LOV_USER_MAGIC_SPECIFIC:
754 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
755 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
758 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
759 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
762 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
763 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
765 case LMV_MAGIC_FOREIGN: {
766 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
768 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
769 __swab32s(&lfm->lfm_magic);
770 __swab32s(&lfm->lfm_length);
771 __swab32s(&lfm->lfm_type);
772 __swab32s(&lfm->lfm_flags);
777 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
782 *plmm_size = lmm_size;
788 * This function will be used to get default LOV/LMV/Default LMV
789 * @valid will be used to indicate which stripe it will retrieve.
790 * If the directory does not have its own default layout, then the
791 * function will request the default layout from root FID.
792 * OBD_MD_MEA LMV stripe EA
793 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
794 * otherwise Default LOV EA.
795 * Each time, it can only retrieve 1 stripe EA
797 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
798 struct ptlrpc_request **request,
799 struct ptlrpc_request **root_request,
802 struct ptlrpc_request *req = NULL;
803 struct ptlrpc_request *root_req = NULL;
804 struct lov_mds_md *lmm = NULL;
809 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
811 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
812 !(valid & OBD_MD_MEA) && root_request != NULL) {
813 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
814 &lmm_size, &root_req, valid,
815 GET_DEFAULT_LAYOUT_ROOT);
821 *plmm_size = lmm_size;
823 if (root_request != NULL)
824 *root_request = root_req;
830 * This function will be used to get default LOV/LMV/Default LMV
831 * @valid will be used to indicate which stripe it will retrieve
832 * OBD_MD_MEA LMV stripe EA
833 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
834 * otherwise Default LOV EA.
835 * Each time, it can only retrieve 1 stripe EA
837 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
838 struct ptlrpc_request **request, u64 valid)
840 struct ptlrpc_request *req = NULL;
841 struct lov_mds_md *lmm = NULL;
846 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
850 *plmm_size = lmm_size;
856 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
858 struct md_op_data *op_data;
863 OBD_ALLOC_PTR(op_data);
867 op_data->op_flags |= MF_GET_MDT_IDX;
868 op_data->op_fid1 = *fid;
869 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
870 mdt_index = op_data->op_mds;
871 OBD_FREE_PTR(op_data);
879 * Get MDT index for the inode.
881 int ll_get_mdt_idx(struct inode *inode)
883 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
887 * Generic handler to do any pre-copy work.
889 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
890 * first information for it that real work has started.
892 * Moreover, for a ARCHIVE request, it will sample the file data version and
893 * store it in \a copy.
895 * \return 0 on success.
897 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
899 struct ll_sb_info *sbi = ll_s2sbi(sb);
900 struct hsm_progress_kernel hpk;
905 /* Forge a hsm_progress based on data from copy. */
906 hpk.hpk_fid = copy->hc_hai.hai_fid;
907 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
908 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
909 hpk.hpk_extent.length = 0;
912 hpk.hpk_data_version = 0;
915 /* For archive request, we need to read the current file version. */
916 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
918 __u64 data_version = 0;
920 /* Get inode for this fid */
921 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
923 hpk.hpk_flags |= HP_FLAG_RETRY;
924 /* hpk_errval is >= 0 */
925 hpk.hpk_errval = -PTR_ERR(inode);
926 GOTO(progress, rc = PTR_ERR(inode));
929 /* Read current file data version */
930 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
933 CDEBUG(D_HSM, "Could not read file data version of "
934 DFID" (rc = %d). Archive request ("
935 "%#llx) could not be done.\n",
936 PFID(©->hc_hai.hai_fid), rc,
937 copy->hc_hai.hai_cookie);
938 hpk.hpk_flags |= HP_FLAG_RETRY;
939 /* hpk_errval must be >= 0 */
940 hpk.hpk_errval = -rc;
944 /* Store in the hsm_copy for later copytool use.
945 * Always modified even if no lsm. */
946 copy->hc_data_version = data_version;
950 /* On error, the request should be considered as completed */
951 if (hpk.hpk_errval > 0)
952 hpk.hpk_flags |= HP_FLAG_COMPLETED;
954 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
957 /* Return first error */
958 RETURN(rc != 0 ? rc : rc2);
962 * Generic handler to do any post-copy work.
964 * It will send the last hsm_progress update to coordinator to inform it
965 * that copy is finished and whether it was successful or not.
968 * - for ARCHIVE request, it will sample the file data version and compare it
969 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
970 * will be considered as failed.
971 * - for RESTORE request, it will sample the file data version and send it to
972 * coordinator which is useful if the file was imported as 'released'.
974 * \return 0 on success.
976 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
978 struct ll_sb_info *sbi = ll_s2sbi(sb);
979 struct hsm_progress_kernel hpk;
984 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
985 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
986 * initialized if copy_end was called with copy == NULL.
989 /* Forge a hsm_progress based on data from copy. */
990 hpk.hpk_fid = copy->hc_hai.hai_fid;
991 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
992 hpk.hpk_extent = copy->hc_hai.hai_extent;
993 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
994 hpk.hpk_errval = copy->hc_errval;
995 hpk.hpk_data_version = 0;
997 /* For archive request, we need to check the file data was not changed.
999 * For restore request, we need to send the file data version, this is
1000 * useful when the file was created using hsm_import.
1002 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1003 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1004 (copy->hc_errval == 0)) {
1005 struct inode *inode;
1006 __u64 data_version = 0;
1008 /* Get lsm for this fid */
1009 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1010 if (IS_ERR(inode)) {
1011 hpk.hpk_flags |= HP_FLAG_RETRY;
1012 /* hpk_errval must be >= 0 */
1013 hpk.hpk_errval = -PTR_ERR(inode);
1014 GOTO(progress, rc = PTR_ERR(inode));
1017 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1020 CDEBUG(D_HSM, "Could not read file data version. "
1021 "Request could not be confirmed.\n");
1022 if (hpk.hpk_errval == 0)
1023 hpk.hpk_errval = -rc;
1027 /* Store in the hsm_copy for later copytool use.
1028 * Always modified even if no lsm. */
1029 hpk.hpk_data_version = data_version;
1031 /* File could have been stripped during archiving, so we need
1032 * to check anyway. */
1033 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1034 (copy->hc_data_version != data_version)) {
1035 CDEBUG(D_HSM, "File data version mismatched. "
1036 "File content was changed during archiving. "
1037 DFID", start:%#llx current:%#llx\n",
1038 PFID(©->hc_hai.hai_fid),
1039 copy->hc_data_version, data_version);
1040 /* File was changed, send error to cdt. Do not ask for
1041 * retry because if a file is modified frequently,
1042 * the cdt will loop on retried archive requests.
1043 * The policy engine will ask for a new archive later
1044 * when the file will not be modified for some tunable
1046 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1048 /* hpk_errval must be >= 0 */
1049 hpk.hpk_errval = -rc;
1056 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1059 /* Return first error */
1060 RETURN(rc != 0 ? rc : rc2);
1064 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1065 const struct lustre_kernelcomm __user *data)
1067 struct lustre_kernelcomm *lk;
1068 struct lustre_kernelcomm *tmp;
1069 size_t size = sizeof(*lk);
1074 /* copy data from userspace to get numbers of archive_id */
1075 OBD_ALLOC(lk, size);
1079 if (copy_from_user(lk, data, size))
1080 GOTO(out_lk, rc = -EFAULT);
1082 if (lk->lk_flags & LK_FLG_STOP)
1085 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1086 __u32 archive_mask = lk->lk_data_count;
1089 /* old hsm agent to old MDS */
1090 if (!exp_connect_archive_id_array(exp))
1093 /* old hsm agent to new MDS */
1094 lk->lk_flags |= LK_FLG_DATANR;
1096 if (archive_mask == 0)
1099 count = hweight32(archive_mask);
1100 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1101 OBD_ALLOC(tmp, new_size);
1103 GOTO(out_lk, rc = -ENOMEM);
1105 memcpy(tmp, lk, size);
1106 tmp->lk_data_count = count;
1112 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1113 if (BIT(i) & archive_mask) {
1114 lk->lk_data[count] = i + 1;
1121 /* new hsm agent to new mds */
1122 if (lk->lk_data_count > 0) {
1123 new_size = offsetof(struct lustre_kernelcomm,
1124 lk_data[lk->lk_data_count]);
1125 OBD_ALLOC(tmp, new_size);
1127 GOTO(out_lk, rc = -ENOMEM);
1133 if (copy_from_user(lk, data, size))
1134 GOTO(out_lk, rc = -EFAULT);
1137 /* new hsm agent to old MDS */
1138 if (!exp_connect_archive_id_array(exp)) {
1141 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1142 GOTO(out_lk, rc = -EINVAL);
1144 for (i = 0; i < lk->lk_data_count; i++) {
1145 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1147 CERROR("%s: archive id %d requested but only "
1148 "[0 - %zu] supported: rc = %d\n",
1149 exp->exp_obd->obd_name, lk->lk_data[i],
1150 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1154 if (lk->lk_data[i] == 0) {
1159 archives |= (1 << (lk->lk_data[i] - 1));
1161 lk->lk_flags &= ~LK_FLG_DATANR;
1162 lk->lk_data_count = archives;
1165 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1171 static int check_owner(int type, int id)
1175 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1179 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1188 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1190 struct ll_sb_info *sbi = ll_s2sbi(sb);
1191 int cmd = qctl->qc_cmd;
1192 int type = qctl->qc_type;
1193 int id = qctl->qc_id;
1194 int valid = qctl->qc_valid;
1202 case LUSTRE_Q_SETDEFAULT:
1203 case LUSTRE_Q_SETQUOTAPOOL:
1204 case LUSTRE_Q_SETINFOPOOL:
1205 case LUSTRE_Q_SETDEFAULT_POOL:
1206 case LUSTRE_Q_DELETEQID:
1207 case LUSTRE_Q_RESETQID:
1208 if (!capable(CAP_SYS_ADMIN))
1211 if (sb->s_flags & SB_RDONLY)
1215 case LUSTRE_Q_GETDEFAULT:
1216 case LUSTRE_Q_GETQUOTAPOOL:
1217 case LUSTRE_Q_GETDEFAULT_POOL:
1218 if (check_owner(type, id) &&
1219 (!capable(CAP_SYS_ADMIN)))
1223 case LUSTRE_Q_GETINFOPOOL:
1226 CERROR("unsupported quotactl op: %#x\n", cmd);
1230 if (valid != QC_GENERAL) {
1231 if (cmd == Q_GETINFO)
1232 qctl->qc_cmd = Q_GETOINFO;
1233 else if (cmd == Q_GETQUOTA ||
1234 cmd == LUSTRE_Q_GETQUOTAPOOL)
1235 qctl->qc_cmd = Q_GETOQUOTA;
1241 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1242 sizeof(*qctl), qctl, NULL);
1245 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1246 sizeof(*qctl), qctl, NULL);
1249 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1250 sizeof(*qctl), qctl, NULL);
1252 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1254 sizeof(*qctl), qctl, NULL);
1265 struct obd_quotactl *oqctl;
1266 int oqctl_len = sizeof(*oqctl);
1268 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1269 oqctl_len += LOV_MAXPOOLNAME + 1;
1271 OBD_ALLOC(oqctl, oqctl_len);
1275 QCTL_COPY(oqctl, qctl);
1276 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1278 OBD_FREE(oqctl, oqctl_len);
1281 /* If QIF_SPACE is not set, client should collect the
1282 * space usage from OSSs by itself */
1283 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1284 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1285 !oqctl->qc_dqblk.dqb_curspace) {
1286 struct obd_quotactl *oqctl_tmp;
1287 int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1289 OBD_ALLOC(oqctl_tmp, qctl_len);
1290 if (oqctl_tmp == NULL)
1291 GOTO(out, rc = -ENOMEM);
1293 if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1294 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1295 memcpy(oqctl_tmp->qc_poolname,
1297 LOV_MAXPOOLNAME + 1);
1299 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1301 oqctl_tmp->qc_id = oqctl->qc_id;
1302 oqctl_tmp->qc_type = oqctl->qc_type;
1304 /* collect space usage from OSTs */
1305 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1306 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1307 if (!rc || rc == -EREMOTEIO) {
1308 oqctl->qc_dqblk.dqb_curspace =
1309 oqctl_tmp->qc_dqblk.dqb_curspace;
1310 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1313 /* collect space & inode usage from MDTs */
1314 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1315 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1316 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1317 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1318 if (!rc || rc == -EREMOTEIO) {
1319 oqctl->qc_dqblk.dqb_curspace +=
1320 oqctl_tmp->qc_dqblk.dqb_curspace;
1321 oqctl->qc_dqblk.dqb_curinodes =
1322 oqctl_tmp->qc_dqblk.dqb_curinodes;
1323 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1325 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1328 OBD_FREE(oqctl_tmp, qctl_len);
1331 QCTL_COPY(qctl, oqctl);
1332 OBD_FREE(oqctl, oqctl_len);
1338 int ll_rmfid(struct file *file, void __user *arg)
1340 const struct fid_array __user *ufa = arg;
1341 struct inode *inode = file_inode(file);
1342 struct ll_sb_info *sbi = ll_i2sbi(inode);
1343 struct fid_array *lfa = NULL;
1346 int i, rc, *rcs = NULL;
1349 if (!capable(CAP_DAC_READ_SEARCH) &&
1350 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1352 /* Only need to get the buflen */
1353 if (get_user(nr, &ufa->fa_nr))
1355 /* DoS protection */
1356 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1359 size = offsetof(struct fid_array, fa_fids[nr]);
1360 OBD_ALLOC(lfa, size);
1363 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1365 GOTO(free_lfa, rc = -ENOMEM);
1367 if (copy_from_user(lfa, arg, size))
1368 GOTO(free_rcs, rc = -EFAULT);
1370 /* In case of subdirectory mount, we need to make sure all the files
1371 * for which we want to remove FID are visible in the namespace.
1373 if (!fid_is_root(&sbi->ll_root_fid)) {
1374 struct fid_array *lfa_new = NULL;
1375 int path_len = PATH_MAX, linkno;
1376 struct getinfo_fid2path *gf;
1377 int idx, last_idx = nr - 1;
1379 OBD_ALLOC(lfa_new, size);
1381 GOTO(free_rcs, rc = -ENOMEM);
1384 gf = kmalloc(sizeof(*gf) + path_len + 1, GFP_NOFS);
1386 GOTO(free_rcs, rc = -ENOMEM);
1388 for (idx = 0; idx < nr; idx++) {
1391 memset(gf, 0, sizeof(*gf) + path_len + 1);
1392 gf->gf_fid = lfa->fa_fids[idx];
1393 gf->gf_pathlen = path_len;
1394 gf->gf_linkno = linkno;
1395 rc = __ll_fid2path(inode, gf,
1396 sizeof(*gf) + gf->gf_pathlen,
1398 if (rc == -ENAMETOOLONG) {
1399 struct getinfo_fid2path *tmpgf;
1401 path_len += PATH_MAX;
1402 tmpgf = krealloc(gf,
1403 sizeof(*gf) + path_len + 1,
1407 OBD_FREE(lfa_new, size);
1408 GOTO(free_rcs, rc = -ENOMEM);
1415 if (gf->gf_linkno == linkno)
1417 linkno = gf->gf_linkno;
1421 /* All the links for this fid are visible in the
1422 * mounted subdir. So add it to the list of fids
1425 lfa_new->fa_fids[lfa_new->fa_nr++] =
1428 /* At least one link for this fid is not visible
1429 * in the mounted subdir. So add it at the end
1430 * of the list that will be hidden to lower
1431 * layers, and set -ENOENT as ret code.
1433 lfa_new->fa_fids[last_idx] = lfa->fa_fids[idx];
1434 rcs[last_idx--] = rc;
1438 OBD_FREE(lfa, size);
1442 if (lfa->fa_nr == 0)
1443 GOTO(free_rcs, rc = rcs[nr - 1]);
1445 /* Call mdc_iocontrol */
1446 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1449 for (i = 0; i < nr; i++)
1451 lfa->fa_fids[i].f_ver = rcs[i];
1452 if (copy_to_user(arg, lfa, size))
1457 OBD_FREE_PTR_ARRAY(rcs, nr);
1459 OBD_FREE(lfa, size);
1464 /* This function tries to get a single name component,
1465 * to send to the server. No actual path traversal involved,
1466 * so we limit to NAME_MAX */
1467 static char *ll_getname(const char __user *filename)
1472 OBD_ALLOC(tmp, NAME_MAX + 1);
1475 return ERR_PTR(-ENOMEM);
1477 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1480 else if (len > NAME_MAX)
1481 ret = -ENAMETOOLONG;
1484 OBD_FREE(tmp, NAME_MAX + 1);
1490 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1492 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1494 struct dentry *dentry = file_dentry(file);
1495 struct inode *inode = file_inode(file);
1496 struct ll_sb_info *sbi = ll_i2sbi(inode);
1497 struct obd_ioctl_data *data = NULL;
1501 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1502 PFID(ll_inode2fid(inode)), inode, cmd);
1504 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1505 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1508 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1510 case FS_IOC_GETFLAGS:
1511 case FS_IOC_SETFLAGS:
1512 RETURN(ll_iocontrol(inode, file, cmd, arg));
1513 case FSFILT_IOC_GETVERSION:
1514 case FS_IOC_GETVERSION:
1515 RETURN(put_user(inode->i_generation, (int __user *)arg));
1516 /* We need to special case any other ioctls we want to handle,
1517 * to send them to the MDS/OST as appropriate and to properly
1518 * network encode the arg field. */
1519 case FS_IOC_SETVERSION:
1522 case LL_IOC_GET_MDTIDX: {
1525 mdtidx = ll_get_mdt_idx(inode);
1529 if (put_user((int)mdtidx, (int __user *)arg))
1534 case IOC_MDC_LOOKUP: {
1535 int namelen, len = 0;
1538 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1542 filename = data->ioc_inlbuf1;
1543 namelen = strlen(filename);
1545 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1546 GOTO(out_free, rc = -EINVAL);
1549 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1551 CERROR("%s: lookup %.*s failed: rc = %d\n",
1552 sbi->ll_fsname, namelen, filename, rc);
1556 OBD_FREE_LARGE(data, len);
1559 case LL_IOC_LMV_SETSTRIPE: {
1560 struct lmv_user_md *lum;
1565 bool createonly = false;
1569 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1573 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1574 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1575 GOTO(lmv_out_free, rc = -EINVAL);
1577 filename = data->ioc_inlbuf1;
1578 namelen = data->ioc_inllen1;
1581 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1582 GOTO(lmv_out_free, rc = -EINVAL);
1584 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1585 lumlen = data->ioc_inllen2;
1587 if (!lmv_user_magic_supported(lum->lum_magic)) {
1588 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1589 lum->lum_magic, -EINVAL);
1590 GOTO(lmv_out_free, rc = -EINVAL);
1593 if ((lum->lum_magic == LMV_USER_MAGIC ||
1594 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1595 lumlen < sizeof(*lum)) {
1596 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1597 filename, lumlen, lum->lum_magic, -EINVAL);
1598 GOTO(lmv_out_free, rc = -EINVAL);
1601 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1602 lumlen < sizeof(struct lmv_foreign_md)) {
1603 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1604 filename, lum->lum_magic, lumlen, -EFAULT);
1605 GOTO(lmv_out_free, rc = -EINVAL);
1608 mode = data->ioc_type;
1609 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1610 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1613 OBD_FREE_LARGE(data, len);
1617 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1618 struct lmv_user_md lum;
1619 struct lmv_user_md __user *ulump =
1620 (struct lmv_user_md __user *)arg;
1623 if (copy_from_user(&lum, ulump, sizeof(lum)))
1626 if (lum.lum_magic != LMV_USER_MAGIC)
1629 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1633 case LL_IOC_LOV_SETSTRIPE_NEW:
1634 case LL_IOC_LOV_SETSTRIPE: {
1635 struct lov_user_md_v3 *lumv3 = NULL;
1636 struct lov_user_md_v1 lumv1;
1637 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1638 struct lov_user_md_v1 __user *lumv1p =
1639 (struct lov_user_md_v1 __user *)arg;
1640 struct lov_user_md_v3 __user *lumv3p =
1641 (struct lov_user_md_v3 __user *)arg;
1644 int set_default = 0;
1646 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1647 sizeof(struct lov_comp_md_v1));
1648 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1649 /* first try with v1 which is smaller than v3 */
1650 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1653 if (is_root_inode(inode))
1656 switch (lumv1.lmm_magic) {
1657 case LOV_USER_MAGIC_V3:
1658 case LOV_USER_MAGIC_SPECIFIC:
1659 lum_size = ll_lov_user_md_size(&lumv1);
1662 OBD_ALLOC(lumv3, lum_size);
1665 if (copy_from_user(lumv3, lumv3p, lum_size))
1666 GOTO(out, rc = -EFAULT);
1667 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1669 case LOV_USER_MAGIC_V1:
1672 GOTO(out, rc = -ENOTSUPP);
1675 /* in v1 and v3 cases lumv1 points to data */
1676 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1679 OBD_FREE(lumv3, lum_size);
1682 case LL_IOC_LMV_GETSTRIPE: {
1683 struct lmv_user_md __user *ulmv =
1684 (struct lmv_user_md __user *)arg;
1685 struct lmv_user_md lum;
1686 struct ptlrpc_request *request = NULL;
1687 struct ptlrpc_request *root_request = NULL;
1688 union lmv_mds_md *lmm = NULL;
1691 struct lmv_user_md *tmp = NULL;
1695 int max_stripe_count;
1699 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1702 max_stripe_count = lum.lum_stripe_count;
1703 /* lum_magic will indicate which stripe the ioctl will like
1704 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1705 * is for default LMV stripe */
1706 if (lum.lum_magic == LMV_MAGIC_V1)
1707 valid |= OBD_MD_MEA;
1708 else if (lum.lum_magic == LMV_USER_MAGIC)
1709 valid |= OBD_MD_DEFAULT_MEA;
1713 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1714 &request, &root_request, valid);
1716 GOTO(finish_req, rc);
1718 /* Get default LMV EA */
1719 if (lum.lum_magic == LMV_USER_MAGIC) {
1720 if (lmmsize > sizeof(*ulmv))
1721 GOTO(finish_req, rc = -EINVAL);
1723 if (root_request != NULL) {
1724 struct lmv_user_md *lum;
1725 struct ll_inode_info *lli;
1727 lum = (struct lmv_user_md *)lmm;
1728 lli = ll_i2info(inode);
1729 if (lum->lum_max_inherit !=
1730 LMV_INHERIT_UNLIMITED) {
1731 if (lum->lum_max_inherit ==
1733 lum->lum_max_inherit <
1735 lum->lum_max_inherit >
1737 lum->lum_max_inherit <
1739 GOTO(finish_req, rc = -ENODATA);
1741 if (lum->lum_max_inherit ==
1742 lli->lli_dir_depth) {
1743 lum->lum_max_inherit =
1745 lum->lum_max_inherit_rr =
1746 LMV_INHERIT_RR_NONE;
1750 lum->lum_max_inherit -=
1754 if (lum->lum_max_inherit_rr !=
1755 LMV_INHERIT_RR_UNLIMITED) {
1756 if (lum->lum_max_inherit_rr ==
1758 lum->lum_max_inherit_rr <
1759 LMV_INHERIT_RR_END ||
1760 lum->lum_max_inherit_rr >
1761 LMV_INHERIT_RR_MAX ||
1762 lum->lum_max_inherit_rr <=
1763 lli->lli_dir_depth) {
1764 lum->lum_max_inherit_rr =
1765 LMV_INHERIT_RR_NONE;
1769 if (lum->lum_max_inherit_rr >
1771 lum->lum_max_inherit_rr -=
1776 if (copy_to_user(ulmv, lmm, lmmsize))
1777 GOTO(finish_req, rc = -EFAULT);
1779 GOTO(finish_req, rc);
1782 /* if foreign LMV case, fake stripes number */
1783 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1784 struct lmv_foreign_md *lfm;
1786 lfm = (struct lmv_foreign_md *)lmm;
1787 if (lfm->lfm_length < XATTR_SIZE_MAX -
1788 offsetof(typeof(*lfm), lfm_value)) {
1789 __u32 size = lfm->lfm_length +
1790 offsetof(typeof(*lfm), lfm_value);
1792 stripe_count = lmv_foreign_to_md_stripes(size);
1794 CERROR("invalid %d foreign size returned\n",
1799 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1801 if (max_stripe_count < stripe_count) {
1802 lum.lum_stripe_count = stripe_count;
1803 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1804 GOTO(finish_req, rc = -EFAULT);
1805 GOTO(finish_req, rc = -E2BIG);
1808 /* enough room on user side and foreign case */
1809 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1810 struct lmv_foreign_md *lfm;
1813 lfm = (struct lmv_foreign_md *)lmm;
1814 size = lfm->lfm_length +
1815 offsetof(struct lmv_foreign_md, lfm_value);
1816 if (copy_to_user(ulmv, lfm, size))
1817 GOTO(finish_req, rc = -EFAULT);
1818 GOTO(finish_req, rc);
1821 lum_size = lmv_user_md_size(stripe_count,
1822 LMV_USER_MAGIC_SPECIFIC);
1823 OBD_ALLOC(tmp, lum_size);
1825 GOTO(finish_req, rc = -ENOMEM);
1827 mdt_index = ll_get_mdt_idx(inode);
1829 GOTO(out_tmp, rc = -ENOMEM);
1831 tmp->lum_magic = LMV_MAGIC_V1;
1832 tmp->lum_stripe_count = 0;
1833 tmp->lum_stripe_offset = mdt_index;
1834 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1835 for (i = 0; i < stripe_count; i++) {
1838 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1839 if (fid_is_sane(&fid)) {
1840 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1842 GOTO(out_tmp, rc = mdt_index);
1844 tmp->lum_objects[i].lum_mds = mdt_index;
1845 tmp->lum_objects[i].lum_fid = fid;
1848 tmp->lum_stripe_count++;
1851 if (copy_to_user(ulmv, tmp, lum_size))
1852 GOTO(out_tmp, rc = -EFAULT);
1854 OBD_FREE(tmp, lum_size);
1856 ptlrpc_req_finished(request);
1857 ptlrpc_req_finished(root_request);
1861 case LL_IOC_UNLOCK_FOREIGN:
1862 /* if not a foreign symlink do nothing */
1863 if (ll_foreign_is_removable(dentry, true)) {
1865 "prevent rmdir of non-foreign dir ("DFID")\n",
1866 PFID(ll_inode2fid(inode)));
1867 RETURN(-EOPNOTSUPP);
1871 case LL_IOC_REMOVE_ENTRY: {
1872 char *filename = NULL;
1876 /* Here is a little hack to avoid sending REINT_RMENTRY to
1877 * unsupported server, which might crash the server(LU-2730),
1878 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1879 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1880 * server will support REINT_RMENTRY XXX*/
1881 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1884 filename = ll_getname((const char __user *)arg);
1885 if (IS_ERR(filename))
1886 RETURN(PTR_ERR(filename));
1888 namelen = strlen(filename);
1890 GOTO(out_rmdir, rc = -EINVAL);
1892 rc = ll_rmdir_entry(inode, filename, namelen);
1895 ll_putname(filename);
1899 RETURN(ll_rmfid(file, (void __user *)arg));
1900 case LL_IOC_LOV_SWAP_LAYOUTS:
1902 case IOC_OBD_STATFS:
1903 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1904 case LL_IOC_LOV_GETSTRIPE:
1905 case LL_IOC_LOV_GETSTRIPE_NEW:
1906 case LL_IOC_MDC_GETINFO_V1:
1907 case LL_IOC_MDC_GETINFO_V2:
1908 case IOC_MDC_GETFILEINFO_V1:
1909 case IOC_MDC_GETFILEINFO_V2:
1910 case IOC_MDC_GETFILESTRIPE: {
1911 struct ptlrpc_request *request = NULL;
1912 struct ptlrpc_request *root_request = NULL;
1913 struct lov_user_md __user *lump;
1914 struct lov_mds_md *lmm = NULL;
1915 struct mdt_body *body;
1916 char *filename = NULL;
1917 lstat_t __user *statp = NULL;
1918 lstatx_t __user *stxp = NULL;
1919 __u64 __user *flagsp = NULL;
1920 __u32 __user *lmmsizep = NULL;
1921 struct lu_fid __user *fidp = NULL;
1925 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1926 cmd == IOC_MDC_GETFILEINFO_V2 ||
1927 cmd == IOC_MDC_GETFILESTRIPE) {
1928 filename = ll_getname((const char __user *)arg);
1929 if (IS_ERR(filename))
1930 RETURN(PTR_ERR(filename));
1932 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1933 &lmmsize, &request);
1935 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1941 body = req_capsule_server_get(&request->rq_pill,
1943 LASSERT(body != NULL);
1948 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1949 cmd == LL_IOC_MDC_GETINFO_V1 ||
1950 cmd == IOC_MDC_GETFILEINFO_V2 ||
1951 cmd == LL_IOC_MDC_GETINFO_V2)) {
1959 if (cmd == IOC_MDC_GETFILESTRIPE ||
1960 cmd == LL_IOC_LOV_GETSTRIPE ||
1961 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1962 lump = (struct lov_user_md __user *)arg;
1963 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1964 cmd == LL_IOC_MDC_GETINFO_V1){
1965 struct lov_user_mds_data_v1 __user *lmdp;
1967 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1968 statp = &lmdp->lmd_st;
1969 lump = &lmdp->lmd_lmm;
1971 struct lov_user_mds_data __user *lmdp;
1973 lmdp = (struct lov_user_mds_data __user *)arg;
1974 fidp = &lmdp->lmd_fid;
1975 stxp = &lmdp->lmd_stx;
1976 flagsp = &lmdp->lmd_flags;
1977 lmmsizep = &lmdp->lmd_lmmsize;
1978 lump = &lmdp->lmd_lmm;
1982 /* If the file has no striping then zero out *lump so
1983 * that the caller isn't confused by garbage. */
1984 if (clear_user(lump, sizeof(*lump)))
1985 GOTO(out_req, rc = -EFAULT);
1986 } else if (copy_to_user(lump, lmm, lmmsize)) {
1987 if (copy_to_user(lump, lmm, sizeof(*lump)))
1988 GOTO(out_req, rc = -EFAULT);
1991 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1993 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1994 cmd == LL_IOC_MDC_GETINFO_V1) {
1997 st.st_dev = inode->i_sb->s_dev;
1998 st.st_mode = body->mbo_mode;
1999 st.st_nlink = body->mbo_nlink;
2000 st.st_uid = body->mbo_uid;
2001 st.st_gid = body->mbo_gid;
2002 st.st_rdev = body->mbo_rdev;
2003 if (llcrypt_require_key(inode) == -ENOKEY)
2004 st.st_size = round_up(st.st_size,
2005 LUSTRE_ENCRYPTION_UNIT_SIZE);
2007 st.st_size = body->mbo_size;
2008 st.st_blksize = PAGE_SIZE;
2009 st.st_blocks = body->mbo_blocks;
2010 st.st_atime = body->mbo_atime;
2011 st.st_mtime = body->mbo_mtime;
2012 st.st_ctime = body->mbo_ctime;
2013 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
2016 if (copy_to_user(statp, &st, sizeof(st)))
2017 GOTO(out_req, rc = -EFAULT);
2018 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
2019 cmd == LL_IOC_MDC_GETINFO_V2) {
2020 lstatx_t stx = { 0 };
2021 __u64 valid = body->mbo_valid;
2023 stx.stx_blksize = PAGE_SIZE;
2024 stx.stx_nlink = body->mbo_nlink;
2025 stx.stx_uid = body->mbo_uid;
2026 stx.stx_gid = body->mbo_gid;
2027 stx.stx_mode = body->mbo_mode;
2028 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
2030 if (llcrypt_require_key(inode) == -ENOKEY)
2031 stx.stx_size = round_up(stx.stx_size,
2032 LUSTRE_ENCRYPTION_UNIT_SIZE);
2034 stx.stx_size = body->mbo_size;
2035 stx.stx_blocks = body->mbo_blocks;
2036 stx.stx_atime.tv_sec = body->mbo_atime;
2037 stx.stx_ctime.tv_sec = body->mbo_ctime;
2038 stx.stx_mtime.tv_sec = body->mbo_mtime;
2039 stx.stx_btime.tv_sec = body->mbo_btime;
2040 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
2041 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
2042 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
2043 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
2044 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
2047 * For a striped directory, the size and blocks returned
2048 * from MDT is not correct.
2049 * The size and blocks are aggregated by client across
2051 * Thus for a striped directory, do not return the valid
2052 * FLSIZE and FLBLOCKS flags to the caller.
2053 * However, this whould be better decided by the MDS
2054 * instead of the client.
2056 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
2057 ll_i2info(inode)->lli_lsm_md != NULL)
2058 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
2060 if (flagsp && copy_to_user(flagsp, &valid,
2062 GOTO(out_req, rc = -EFAULT);
2064 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
2066 GOTO(out_req, rc = -EFAULT);
2068 if (!(valid & OBD_MD_FLSIZE))
2069 stx.stx_mask &= ~STATX_SIZE;
2070 if (!(valid & OBD_MD_FLBLOCKS))
2071 stx.stx_mask &= ~STATX_BLOCKS;
2073 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
2074 GOTO(out_req, rc = -EFAULT);
2076 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2078 GOTO(out_req, rc = -EFAULT);
2083 ptlrpc_req_finished(request);
2084 ptlrpc_req_finished(root_request);
2086 ll_putname(filename);
2089 case OBD_IOC_QUOTACTL: {
2090 struct if_quotactl *qctl;
2091 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2093 OBD_ALLOC(qctl, qctl_len);
2097 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
2098 GOTO(out_quotactl, rc = -EFAULT);
2100 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2101 char __user *from = (char __user *)arg +
2102 offsetof(typeof(*qctl), qc_poolname);
2103 if (copy_from_user(qctl->qc_poolname, from,
2104 LOV_MAXPOOLNAME + 1))
2105 GOTO(out_quotactl, rc = -EFAULT);
2108 rc = quotactl_ioctl(inode->i_sb, qctl);
2109 if ((rc == 0 || rc == -ENODATA) &&
2110 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
2114 OBD_FREE(qctl, qctl_len);
2117 case OBD_IOC_GETNAME_OLD:
2118 case OBD_IOC_GETDTNAME:
2119 case OBD_IOC_GETMDNAME:
2120 RETURN(ll_get_obd_name(inode, cmd, arg));
2121 case LL_IOC_FLUSHCTX:
2122 RETURN(ll_flush_ctx(inode));
2123 case LL_IOC_GETOBDCOUNT: {
2125 struct obd_export *exp;
2127 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
2130 /* get ost count when count is zero, get mdt count otherwise */
2131 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2132 vallen = sizeof(count);
2133 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2134 KEY_TGT_COUNT, &vallen, &count);
2136 CERROR("get target count failed: %d\n", rc);
2140 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
2145 case LL_IOC_PATH2FID:
2146 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2147 sizeof(struct lu_fid)))
2150 case LL_IOC_GET_CONNECT_FLAGS: {
2151 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
2152 (void __user *)arg));
2154 case OBD_IOC_FID2PATH:
2155 RETURN(ll_fid2path(inode, (void __user *)arg));
2156 case LL_IOC_GETPARENT:
2157 RETURN(ll_getparent(file, (void __user *)arg));
2158 case LL_IOC_FID2MDTIDX: {
2159 struct obd_export *exp = ll_i2mdexp(inode);
2163 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
2167 /* Call mdc_iocontrol */
2168 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2169 (__u32 __user *)&index);
2175 case LL_IOC_HSM_REQUEST: {
2176 struct hsm_user_request *hur;
2183 /* We don't know the true size yet; copy the fixed-size part */
2184 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
2189 /* Compute the whole struct size */
2190 totalsize = hur_len(hur);
2195 /* Final size will be more than double totalsize */
2196 if (totalsize >= MDS_MAXREQSIZE / 3)
2199 OBD_ALLOC_LARGE(hur, totalsize);
2203 /* Copy the whole struct */
2204 if (copy_from_user(hur, (void __user *)arg, totalsize))
2205 GOTO(out_hur, rc = -EFAULT);
2207 if (hur->hur_request.hr_action == HUA_RELEASE) {
2208 const struct lu_fid *fid;
2212 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2213 fid = &hur->hur_user_item[i].hui_fid;
2214 f = search_inode_for_lustre(inode->i_sb, fid);
2220 rc = ll_hsm_release(f);
2226 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2231 OBD_FREE_LARGE(hur, totalsize);
2235 case LL_IOC_HSM_PROGRESS: {
2236 struct hsm_progress_kernel hpk;
2237 struct hsm_progress hp;
2239 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2242 hpk.hpk_fid = hp.hp_fid;
2243 hpk.hpk_cookie = hp.hp_cookie;
2244 hpk.hpk_extent = hp.hp_extent;
2245 hpk.hpk_flags = hp.hp_flags;
2246 hpk.hpk_errval = hp.hp_errval;
2247 hpk.hpk_data_version = 0;
2249 /* File may not exist in Lustre; all progress
2250 * reported to Lustre root */
2251 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2255 case LL_IOC_HSM_CT_START:
2256 if (!capable(CAP_SYS_ADMIN))
2259 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2260 (struct lustre_kernelcomm __user *)arg);
2263 case LL_IOC_HSM_COPY_START: {
2264 struct hsm_copy *copy;
2267 OBD_ALLOC_PTR(copy);
2270 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2275 rc = ll_ioc_copy_start(inode->i_sb, copy);
2276 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2282 case LL_IOC_HSM_COPY_END: {
2283 struct hsm_copy *copy;
2286 OBD_ALLOC_PTR(copy);
2289 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2294 rc = ll_ioc_copy_end(inode->i_sb, copy);
2295 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2301 case LL_IOC_MIGRATE: {
2302 struct lmv_user_md *lum;
2309 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
2313 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2314 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2315 GOTO(migrate_free, rc = -EINVAL);
2317 filename = data->ioc_inlbuf1;
2318 namelen = data->ioc_inllen1;
2319 flags = data->ioc_type;
2321 if (namelen < 1 || namelen != strlen(filename) + 1) {
2322 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2323 GOTO(migrate_free, rc = -EINVAL);
2326 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2327 if (lum->lum_magic != LMV_USER_MAGIC &&
2328 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2330 CERROR("%s: wrong lum magic %x: rc = %d\n",
2331 filename, lum->lum_magic, rc);
2332 GOTO(migrate_free, rc);
2335 rc = ll_migrate(inode, file, lum, filename, flags);
2337 OBD_FREE_LARGE(data, len);
2341 case FS_IOC_FSGETXATTR:
2342 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2343 case FS_IOC_FSSETXATTR:
2344 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2345 case LL_IOC_PROJECT:
2346 RETURN(ll_ioctl_project(file, cmd, arg));
2347 case LL_IOC_PCC_DETACH_BY_FID: {
2348 struct lu_pcc_detach_fid *detach;
2350 struct inode *inode2;
2353 OBD_ALLOC_PTR(detach);
2357 if (copy_from_user(detach,
2358 (const struct lu_pcc_detach_fid __user *)arg,
2360 GOTO(out_detach, rc = -EFAULT);
2362 fid = &detach->pccd_fid;
2363 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2364 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2366 /* Target inode is not in inode cache, and PCC file
2367 * has aleady released, return immdiately.
2369 GOTO(out_detach, rc = 0);
2371 if (!S_ISREG(inode2->i_mode))
2372 GOTO(out_iput, rc = -EINVAL);
2374 if (!inode_owner_or_capable(&init_user_ns, inode2))
2375 GOTO(out_iput, rc = -EPERM);
2377 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2381 OBD_FREE_PTR(detach);
2384 #ifdef HAVE_LUSTRE_CRYPTO
2385 case LL_IOC_SET_ENCRYPTION_POLICY:
2386 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2388 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2389 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2390 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2392 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2393 case LL_IOC_ADD_ENCRYPTION_KEY:
2394 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2396 rc = llcrypt_ioctl_add_key(file, (void __user *)arg);
2397 #ifdef CONFIG_LL_ENCRYPTION
2399 sptlrpc_enc_pool_add_user();
2402 case LL_IOC_REMOVE_ENCRYPTION_KEY:
2403 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2405 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2406 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2407 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2409 return llcrypt_ioctl_remove_key_all_users(file,
2410 (void __user *)arg);
2411 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2412 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2414 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2417 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2418 (void __user *)arg));
2422 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2424 struct inode *inode = file->f_mapping->host;
2425 struct ll_file_data *fd = file->private_data;
2426 struct ll_sb_info *sbi = ll_i2sbi(inode);
2427 int api32 = ll_need_32bit_api(sbi);
2428 loff_t ret = -EINVAL;
2436 offset += file->f_pos;
2442 offset += LL_DIR_END_OFF_32BIT;
2444 offset += LL_DIR_END_OFF;
2451 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2452 (!api32 && offset <= LL_DIR_END_OFF))) {
2453 if (offset != file->f_pos) {
2456 hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2457 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2458 (!api32 && offset == LL_DIR_END_OFF))
2459 fd->lfd_pos = MDS_DIR_END_OFF;
2460 else if (api32 && hash64)
2461 fd->lfd_pos = offset << 32;
2463 fd->lfd_pos = offset;
2464 file->f_pos = offset;
2465 file->f_version = 0;
2472 inode_unlock(inode);
2476 static int ll_dir_open(struct inode *inode, struct file *file)
2479 RETURN(ll_file_open(inode, file));
2482 static int ll_dir_release(struct inode *inode, struct file *file)
2485 RETURN(ll_file_release(inode, file));
2488 /* notify error if partially read striped directory */
2489 static int ll_dir_flush(struct file *file, fl_owner_t id)
2491 struct ll_file_data *lfd = file->private_data;
2492 int rc = lfd->fd_partial_readdir_rc;
2494 lfd->fd_partial_readdir_rc = 0;
2499 const struct file_operations ll_dir_operations = {
2500 .llseek = ll_dir_seek,
2501 .open = ll_dir_open,
2502 .release = ll_dir_release,
2503 .read = generic_read_dir,
2504 #ifdef HAVE_DIR_CONTEXT
2505 .iterate_shared = ll_iterate,
2507 .readdir = ll_readdir,
2509 .unlocked_ioctl = ll_dir_ioctl,
2511 .flush = ll_dir_flush,