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_fid.h>
55 #include <lustre_kernelcomm.h>
56 #include <lustre_swab.h>
57 #include <libcfs/libcfs_crypto.h>
59 #include "llite_internal.h"
62 * (new) readdir implementation overview.
64 * Original lustre readdir implementation cached exact copy of raw directory
65 * pages on the client. These pages were indexed in client page cache by
66 * logical offset in the directory file. This design, while very simple and
67 * intuitive had some inherent problems:
69 * . it implies that byte offset to the directory entry serves as a
70 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71 * ext3/htree directory entries may move due to splits, and more
74 * . it is incompatible with the design of split directories for cmd3,
75 * that assumes that names are distributed across nodes based on their
76 * hash, and so readdir should be done in hash order.
78 * New readdir implementation does readdir in hash order, and uses hash of a
79 * file name as a telldir/seekdir cookie. This led to number of complications:
81 * . hash is not unique, so it cannot be used to index cached directory
82 * pages on the client (note, that it requires a whole pageful of hash
83 * collided entries to cause two pages to have identical hashes);
85 * . hash is not unique, so it cannot, strictly speaking, be used as an
86 * entry cookie. ext3/htree has the same problem and lustre implementation
87 * mimics their solution: seekdir(hash) positions directory at the first
88 * entry with the given hash.
94 * Client caches directory pages using hash of the first entry as an index. As
95 * noted above hash is not unique, so this solution doesn't work as is:
96 * special processing is needed for "page hash chains" (i.e., sequences of
97 * pages filled with entries all having the same hash value).
99 * First, such chains have to be detected. To this end, server returns to the
100 * client the hash of the first entry on the page next to one returned. When
101 * client detects that this hash is the same as hash of the first entry on the
102 * returned page, page hash collision has to be handled. Pages in the
103 * hash chain, except first one, are termed "overflow pages".
105 * Proposed (unimplimented) solution to index uniqueness problem is to
106 * not cache overflow pages. Instead, when page hash collision is
107 * detected, all overflow pages from emerging chain should be
108 * immediately requested from the server and placed in a special data
109 * structure. This data structure can be used by ll_readdir() to
110 * process entries from overflow pages. When readdir invocation
111 * finishes, overflow pages are discarded. If page hash collision chain
112 * weren't completely processed, next call to readdir will again detect
113 * page hash collision, again read overflow pages in, process next
114 * portion of entries and again discard the pages. This is not as
115 * wasteful as it looks, because, given reasonable hash, page hash
116 * collisions are extremely rare.
118 * 1. directory positioning
120 * When seekdir(hash) is called, original
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 && S_ISDIR(inode->i_mode) &&
170 lmv_dir_striped(ll_i2info(inode)->lli_lsm_md)) {
177 if (likely(page->mapping != NULL))
178 delete_from_page_cache(page);
184 #ifdef HAVE_DIR_CONTEXT
185 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
186 struct dir_context *ctx, int *partial_readdir_rc)
189 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
190 void *cookie, filldir_t filldir, int *partial_readdir_rc)
193 struct ll_sb_info *sbi = ll_i2sbi(inode);
195 bool is_api32 = ll_need_32bit_api(sbi);
196 bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
199 struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
203 if (IS_ENCRYPTED(inode)) {
204 rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
209 page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
211 while (rc == 0 && !done) {
212 struct lu_dirpage *dp;
213 struct lu_dirent *ent;
222 hash = MDS_DIR_END_OFF;
223 dp = page_address(page);
224 for (ent = lu_dirent_start(dp); ent != NULL && !done;
225 ent = lu_dirent_next(ent)) {
232 hash = le64_to_cpu(ent->lde_hash);
233 if (hash < pos) /* Skip until we find target hash */
236 namelen = le16_to_cpu(ent->lde_namelen);
237 if (namelen == 0) /* Skip dummy record */
240 if (is_api32 && is_hash64)
244 fid_le_to_cpu(&fid, &ent->lde_fid);
245 ino = cl_fid_build_ino(&fid, is_api32);
246 type = S_DT(lu_dirent_type_get(ent));
247 /* For ll_nfs_get_name_filldir(), it will try to access
248 * 'ent' through 'lde_name', so the parameter 'name'
249 * for 'filldir()' must be part of the 'ent'. */
250 #ifdef HAVE_DIR_CONTEXT
252 if (!IS_ENCRYPTED(inode)) {
253 done = !dir_emit(ctx, ent->lde_name, namelen,
256 /* Directory is encrypted */
257 int save_len = lltr.len;
258 struct llcrypt_str de_name =
259 LLTR_INIT(ent->lde_name, namelen);
261 rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
269 done = !dir_emit(ctx, de_name.name, de_name.len,
273 /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
274 * IS_ENCRYPTED is brought by kernel 4.14.
275 * So there is no need to handle encryption case here.
277 done = filldir(cookie, ent->lde_name, namelen, lhash,
284 ll_release_page(inode, page, false);
288 next = le64_to_cpu(dp->ldp_hash_end);
290 if (pos == MDS_DIR_END_OFF) {
292 * End of directory reached.
295 ll_release_page(inode, page, false);
298 * Normal case: continue to the next
301 ll_release_page(inode, page,
302 le32_to_cpu(dp->ldp_flags) &
305 page = ll_get_dir_page(inode, op_data, pos,
309 #ifdef HAVE_DIR_CONTEXT
314 llcrypt_fname_free_buffer(&lltr);
318 #ifdef HAVE_DIR_CONTEXT
319 static int ll_iterate(struct file *filp, struct dir_context *ctx)
321 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
324 struct inode *inode = file_inode(filp);
325 struct ll_file_data *lfd = filp->private_data;
326 struct ll_sb_info *sbi = ll_i2sbi(inode);
327 bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
328 int api32 = ll_need_32bit_api(sbi);
329 struct md_op_data *op_data;
330 struct lu_fid pfid = { 0 };
331 ktime_t kstart = ktime_get();
332 /* result of possible partial readdir */
333 int partial_readdir_rc = 0;
339 LASSERT(lfd != NULL);
343 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
344 PFID(ll_inode2fid(inode)),
345 inode, (unsigned long)pos, i_size_read(inode), api32);
347 if (IS_ENCRYPTED(inode)) {
348 rc = llcrypt_get_encryption_info(inode);
349 if (rc && rc != -ENOKEY)
353 if (pos == MDS_DIR_END_OFF)
359 if (unlikely(ll_dir_striped(inode))) {
361 * This is only needed for striped dir to fill ..,
362 * see lmv_read_page()
364 if (file_dentry(filp)->d_parent != NULL &&
365 file_dentry(filp)->d_parent->d_inode != NULL) {
366 __u64 ibits = MDS_INODELOCK_LOOKUP;
367 struct inode *parent =
368 file_dentry(filp)->d_parent->d_inode;
370 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
371 pfid = *ll_inode2fid(parent);
374 /* If it can not find in cache, do lookup .. on the master
376 if (fid_is_zero(&pfid)) {
377 rc = ll_dir_get_parent_fid(inode, &pfid);
383 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
384 LUSTRE_OPC_ANY, inode);
386 GOTO(out, rc = PTR_ERR(op_data));
388 /* foreign dirs are browsed out of Lustre */
389 if (unlikely(op_data->op_mea1 != NULL &&
390 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
391 ll_finish_md_op_data(op_data);
395 op_data->op_fid3 = pfid;
397 #ifdef HAVE_DIR_CONTEXT
399 rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
402 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
403 &partial_readdir_rc);
406 if (!lfd->fd_partial_readdir_rc)
407 lfd->fd_partial_readdir_rc = partial_readdir_rc;
409 if (pos == MDS_DIR_END_OFF) {
411 pos = LL_DIR_END_OFF_32BIT;
413 pos = LL_DIR_END_OFF;
418 #ifdef HAVE_DIR_CONTEXT
423 ll_finish_md_op_data(op_data);
427 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
428 ktime_us_delta(ktime_get(), kstart));
434 * Create striped directory with specified stripe(@lump)
436 * \param[in] dparent the parent of the directory.
437 * \param[in] lump the specified stripes.
438 * \param[in] dirname the name of the directory.
439 * \param[in] mode the specified mode of the directory.
441 * \retval =0 if striped directory is being created successfully.
442 * <0 if the creation is failed.
444 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
445 size_t len, const char *dirname, umode_t mode,
448 struct inode *parent = dparent->d_inode;
449 struct ptlrpc_request *request = NULL;
450 struct md_op_data *op_data;
451 struct ll_sb_info *sbi = ll_i2sbi(parent);
452 struct inode *inode = NULL;
453 struct dentry dentry = {
457 .len = strlen(dirname),
458 .hash = ll_full_name_hash(dparent, dirname,
462 bool encrypt = false;
467 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
470 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
472 "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
473 PFID(ll_inode2fid(parent)), parent, dirname,
474 (int)lump->lum_stripe_offset, lump->lum_stripe_count,
475 lump->lum_hash_type);
477 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
480 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
481 PFID(ll_inode2fid(parent)), parent, dirname,
482 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
485 if (lump->lum_stripe_count > 1 &&
486 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
489 if (IS_DEADDIR(parent) &&
490 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
493 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
494 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
496 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
497 lump->lum_hash_type = cfs_fail_val;
498 } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
499 enum lmv_hash_type type = lump->lum_hash_type &
502 if (type >= LMV_HASH_TYPE_CRUSH ||
503 type == LMV_HASH_TYPE_UNKNOWN)
504 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
505 LMV_HASH_TYPE_FNV_1A_64;
508 hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
509 if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
512 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
513 lustre_swab_lmv_user_md(lump);
515 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
516 mode &= ~current_umask();
517 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
518 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
519 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
522 RETURN(PTR_ERR(op_data));
524 op_data->op_dir_depth = ll_i2info(parent)->lli_dir_depth;
526 if (ll_sbi_has_encrypt(sbi) &&
527 (IS_ENCRYPTED(parent) ||
528 unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
529 err = llcrypt_get_encryption_info(parent);
531 GOTO(out_op_data, err);
532 if (!llcrypt_has_encryption_key(parent))
533 GOTO(out_op_data, err = -ENOKEY);
537 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
538 /* selinux_dentry_init_security() uses dentry->d_parent and name
539 * to determine the security context for the file. So our fake
540 * dentry should be real enough for this purpose. */
541 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
542 &op_data->op_file_secctx_name,
543 &op_data->op_file_secctx,
544 &op_data->op_file_secctx_size);
546 GOTO(out_op_data, err);
550 err = llcrypt_inherit_context(parent, NULL, op_data, false);
552 GOTO(out_op_data, err);
555 op_data->op_cli_flags |= CLI_SET_MEA;
557 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
559 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
560 from_kuid(&init_user_ns, current_fsuid()),
561 from_kgid(&init_user_ns, current_fsgid()),
562 current_cap(), 0, &request);
564 GOTO(out_request, err);
566 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
568 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
570 GOTO(out_inode, err);
572 dentry.d_inode = inode;
574 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
575 /* no need to protect selinux_inode_setsecurity() by
576 * inode_lock. Taking it would lead to a client deadlock
579 err = security_inode_notifysecctx(inode,
580 op_data->op_file_secctx,
581 op_data->op_file_secctx_size);
583 err = ll_inode_init_security(&dentry, inode, parent);
586 GOTO(out_inode, err);
589 err = ll_set_encflags(inode, op_data->op_file_encctx,
590 op_data->op_file_encctx_size, false);
592 GOTO(out_inode, err);
598 ptlrpc_req_finished(request);
600 ll_finish_md_op_data(op_data);
605 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
608 struct ll_sb_info *sbi = ll_i2sbi(inode);
609 struct md_op_data *op_data;
610 struct ptlrpc_request *req = NULL;
616 switch (lump->lmm_magic) {
617 case LOV_USER_MAGIC_V1:
618 lum_size = sizeof(struct lov_user_md_v1);
620 case LOV_USER_MAGIC_V3:
621 lum_size = sizeof(struct lov_user_md_v3);
623 case LOV_USER_MAGIC_COMP_V1:
624 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
626 case LMV_USER_MAGIC: {
627 struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
629 /* MDS < 2.14 doesn't support 'crush' hash type, and
630 * cannot handle unknown hash if client doesn't set a
631 * valid one. switch to fnv_1a_64.
633 if (!(exp_connect_flags2(sbi->ll_md_exp) &
634 OBD_CONNECT2_CRUSH)) {
635 enum lmv_hash_type type = lmv->lum_hash_type &
638 if (type >= LMV_HASH_TYPE_CRUSH ||
639 type == LMV_HASH_TYPE_UNKNOWN)
641 (lmv->lum_hash_type ^ type) |
642 LMV_HASH_TYPE_FNV_1A_64;
644 if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
645 lustre_swab_lmv_user_md(lmv);
646 lum_size = sizeof(*lmv);
649 case LOV_USER_MAGIC_SPECIFIC: {
650 struct lov_user_md_v3 *v3 =
651 (struct lov_user_md_v3 *)lump;
652 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
654 lum_size = lov_user_md_size(v3->lmm_stripe_count,
655 LOV_USER_MAGIC_SPECIFIC);
659 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
660 " %#08x != %#08x nor %#08x\n",
661 lump->lmm_magic, LOV_USER_MAGIC_V1,
667 * This is coming from userspace, so should be in
668 * local endian. But the MDS would like it in little
669 * endian, so we swab it before we send it.
671 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
672 le32_to_cpu(LOV_MAGIC_MAGIC))
673 lustre_swab_lov_user_md(lump, 0);
675 lum_size = sizeof(struct lov_user_md_v1);
678 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
679 LUSTRE_OPC_ANY, NULL);
681 RETURN(PTR_ERR(op_data));
683 /* swabbing is done in lov_setstripe() on server side */
684 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
685 ll_finish_md_op_data(op_data);
686 ptlrpc_req_finished(req);
693 int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
694 struct ptlrpc_request **request, u64 valid,
695 enum get_default_layout_type type)
697 struct ll_sb_info *sbi = ll_i2sbi(inode);
698 struct mdt_body *body;
699 struct lov_mds_md *lmm = NULL;
700 struct ptlrpc_request *req = NULL;
701 int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
702 struct md_op_data *op_data;
708 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
709 LUSTRE_OPC_ANY, NULL);
711 RETURN(PTR_ERR(op_data));
713 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
715 if (type == GET_DEFAULT_LAYOUT_ROOT) {
716 lu_root_fid(&op_data->op_fid1);
717 fid = op_data->op_fid1;
719 fid = *ll_inode2fid(inode);
722 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
723 ll_finish_md_op_data(op_data);
725 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
730 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
731 LASSERT(body != NULL);
733 lmm_size = body->mbo_eadatasize;
735 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
737 GOTO(out, rc = -ENODATA);
740 lmm = req_capsule_server_sized_get(&req->rq_pill,
741 &RMF_MDT_MD, lmm_size);
742 LASSERT(lmm != NULL);
745 * This is coming from the MDS, so is probably in
746 * little endian. We convert it to host endian before
747 * passing it to userspace.
749 /* We don't swab objects for directories */
750 switch (le32_to_cpu(lmm->lmm_magic)) {
753 case LOV_MAGIC_COMP_V1:
754 case LOV_USER_MAGIC_SPECIFIC:
755 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
756 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
759 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
760 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
763 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
764 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
766 case LMV_MAGIC_FOREIGN: {
767 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
769 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
770 __swab32s(&lfm->lfm_magic);
771 __swab32s(&lfm->lfm_length);
772 __swab32s(&lfm->lfm_type);
773 __swab32s(&lfm->lfm_flags);
778 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
783 *plmm_size = lmm_size;
789 * This function will be used to get default LOV/LMV/Default LMV
790 * @valid will be used to indicate which stripe it will retrieve.
791 * If the directory does not have its own default layout, then the
792 * function will request the default layout from root FID.
793 * OBD_MD_MEA LMV stripe EA
794 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
795 * otherwise Default LOV EA.
796 * Each time, it can only retrieve 1 stripe EA
798 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
799 struct ptlrpc_request **request,
800 struct ptlrpc_request **root_request,
803 struct ptlrpc_request *req = NULL;
804 struct ptlrpc_request *root_req = NULL;
805 struct lov_mds_md *lmm = NULL;
810 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
812 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
813 !(valid & OBD_MD_MEA) && root_request != NULL) {
814 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
815 &lmm_size, &root_req, valid,
816 GET_DEFAULT_LAYOUT_ROOT);
822 *plmm_size = lmm_size;
824 if (root_request != NULL)
825 *root_request = root_req;
831 * This function will be used to get default LOV/LMV/Default LMV
832 * @valid will be used to indicate which stripe it will retrieve
833 * OBD_MD_MEA LMV stripe EA
834 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
835 * otherwise Default LOV EA.
836 * Each time, it can only retrieve 1 stripe EA
838 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
839 struct ptlrpc_request **request, u64 valid)
841 struct ptlrpc_request *req = NULL;
842 struct lov_mds_md *lmm = NULL;
847 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
851 *plmm_size = lmm_size;
857 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
859 struct md_op_data *op_data;
864 OBD_ALLOC_PTR(op_data);
868 op_data->op_flags |= MF_GET_MDT_IDX;
869 op_data->op_fid1 = *fid;
870 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
871 mdt_index = op_data->op_mds;
872 OBD_FREE_PTR(op_data);
880 * Get MDT index for the inode.
882 int ll_get_mdt_idx(struct inode *inode)
884 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
888 * Generic handler to do any pre-copy work.
890 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
891 * first information for it that real work has started.
893 * Moreover, for a ARCHIVE request, it will sample the file data version and
894 * store it in \a copy.
896 * \return 0 on success.
898 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
900 struct ll_sb_info *sbi = ll_s2sbi(sb);
901 struct hsm_progress_kernel hpk;
906 /* Forge a hsm_progress based on data from copy. */
907 hpk.hpk_fid = copy->hc_hai.hai_fid;
908 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
909 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
910 hpk.hpk_extent.length = 0;
913 hpk.hpk_data_version = 0;
916 /* For archive request, we need to read the current file version. */
917 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
919 __u64 data_version = 0;
921 /* Get inode for this fid */
922 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
924 hpk.hpk_flags |= HP_FLAG_RETRY;
925 /* hpk_errval is >= 0 */
926 hpk.hpk_errval = -PTR_ERR(inode);
927 GOTO(progress, rc = PTR_ERR(inode));
930 /* Read current file data version */
931 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
934 CDEBUG(D_HSM, "Could not read file data version of "
935 DFID" (rc = %d). Archive request ("
936 "%#llx) could not be done.\n",
937 PFID(©->hc_hai.hai_fid), rc,
938 copy->hc_hai.hai_cookie);
939 hpk.hpk_flags |= HP_FLAG_RETRY;
940 /* hpk_errval must be >= 0 */
941 hpk.hpk_errval = -rc;
945 /* Store in the hsm_copy for later copytool use.
946 * Always modified even if no lsm. */
947 copy->hc_data_version = data_version;
951 /* On error, the request should be considered as completed */
952 if (hpk.hpk_errval > 0)
953 hpk.hpk_flags |= HP_FLAG_COMPLETED;
955 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
958 /* Return first error */
959 RETURN(rc != 0 ? rc : rc2);
963 * Generic handler to do any post-copy work.
965 * It will send the last hsm_progress update to coordinator to inform it
966 * that copy is finished and whether it was successful or not.
969 * - for ARCHIVE request, it will sample the file data version and compare it
970 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
971 * will be considered as failed.
972 * - for RESTORE request, it will sample the file data version and send it to
973 * coordinator which is useful if the file was imported as 'released'.
975 * \return 0 on success.
977 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
979 struct ll_sb_info *sbi = ll_s2sbi(sb);
980 struct hsm_progress_kernel hpk;
985 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
986 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
987 * initialized if copy_end was called with copy == NULL.
990 /* Forge a hsm_progress based on data from copy. */
991 hpk.hpk_fid = copy->hc_hai.hai_fid;
992 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
993 hpk.hpk_extent = copy->hc_hai.hai_extent;
994 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
995 hpk.hpk_errval = copy->hc_errval;
996 hpk.hpk_data_version = 0;
998 /* For archive request, we need to check the file data was not changed.
1000 * For restore request, we need to send the file data version, this is
1001 * useful when the file was created using hsm_import.
1003 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1004 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1005 (copy->hc_errval == 0)) {
1006 struct inode *inode;
1007 __u64 data_version = 0;
1009 /* Get lsm for this fid */
1010 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1011 if (IS_ERR(inode)) {
1012 hpk.hpk_flags |= HP_FLAG_RETRY;
1013 /* hpk_errval must be >= 0 */
1014 hpk.hpk_errval = -PTR_ERR(inode);
1015 GOTO(progress, rc = PTR_ERR(inode));
1018 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1021 CDEBUG(D_HSM, "Could not read file data version. "
1022 "Request could not be confirmed.\n");
1023 if (hpk.hpk_errval == 0)
1024 hpk.hpk_errval = -rc;
1028 /* Store in the hsm_copy for later copytool use.
1029 * Always modified even if no lsm. */
1030 hpk.hpk_data_version = data_version;
1032 /* File could have been stripped during archiving, so we need
1033 * to check anyway. */
1034 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1035 (copy->hc_data_version != data_version)) {
1036 CDEBUG(D_HSM, "File data version mismatched. "
1037 "File content was changed during archiving. "
1038 DFID", start:%#llx current:%#llx\n",
1039 PFID(©->hc_hai.hai_fid),
1040 copy->hc_data_version, data_version);
1041 /* File was changed, send error to cdt. Do not ask for
1042 * retry because if a file is modified frequently,
1043 * the cdt will loop on retried archive requests.
1044 * The policy engine will ask for a new archive later
1045 * when the file will not be modified for some tunable
1047 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1049 /* hpk_errval must be >= 0 */
1050 hpk.hpk_errval = -rc;
1057 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1060 /* Return first error */
1061 RETURN(rc != 0 ? rc : rc2);
1065 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1066 const struct lustre_kernelcomm __user *data)
1068 struct lustre_kernelcomm *lk;
1069 struct lustre_kernelcomm *tmp;
1070 size_t size = sizeof(*lk);
1075 /* copy data from userspace to get numbers of archive_id */
1076 OBD_ALLOC(lk, size);
1080 if (copy_from_user(lk, data, size))
1081 GOTO(out_lk, rc = -EFAULT);
1083 if (lk->lk_flags & LK_FLG_STOP)
1086 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1087 __u32 archive_mask = lk->lk_data_count;
1090 /* old hsm agent to old MDS */
1091 if (!exp_connect_archive_id_array(exp))
1094 /* old hsm agent to new MDS */
1095 lk->lk_flags |= LK_FLG_DATANR;
1097 if (archive_mask == 0)
1100 count = hweight32(archive_mask);
1101 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1102 OBD_ALLOC(tmp, new_size);
1104 GOTO(out_lk, rc = -ENOMEM);
1106 memcpy(tmp, lk, size);
1107 tmp->lk_data_count = count;
1113 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1114 if (BIT(i) & archive_mask) {
1115 lk->lk_data[count] = i + 1;
1122 /* new hsm agent to new mds */
1123 if (lk->lk_data_count > 0) {
1124 new_size = offsetof(struct lustre_kernelcomm,
1125 lk_data[lk->lk_data_count]);
1126 OBD_ALLOC(tmp, new_size);
1128 GOTO(out_lk, rc = -ENOMEM);
1134 if (copy_from_user(lk, data, size))
1135 GOTO(out_lk, rc = -EFAULT);
1138 /* new hsm agent to old MDS */
1139 if (!exp_connect_archive_id_array(exp)) {
1142 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1143 GOTO(out_lk, rc = -EINVAL);
1145 for (i = 0; i < lk->lk_data_count; i++) {
1146 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1148 CERROR("%s: archive id %d requested but only "
1149 "[0 - %zu] supported: rc = %d\n",
1150 exp->exp_obd->obd_name, lk->lk_data[i],
1151 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1155 if (lk->lk_data[i] == 0) {
1160 archives |= (1 << (lk->lk_data[i] - 1));
1162 lk->lk_flags &= ~LK_FLG_DATANR;
1163 lk->lk_data_count = archives;
1166 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1172 static int check_owner(int type, int id)
1176 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1180 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1189 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1191 struct ll_sb_info *sbi = ll_s2sbi(sb);
1192 int cmd = qctl->qc_cmd;
1193 int type = qctl->qc_type;
1194 int id = qctl->qc_id;
1195 int valid = qctl->qc_valid;
1203 case LUSTRE_Q_SETDEFAULT:
1204 case LUSTRE_Q_SETQUOTAPOOL:
1205 case LUSTRE_Q_SETINFOPOOL:
1206 case LUSTRE_Q_SETDEFAULT_POOL:
1207 case LUSTRE_Q_DELETEQID:
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 fid_array *lfa = NULL;
1345 int i, rc, *rcs = NULL;
1348 if (!capable(CAP_DAC_READ_SEARCH) &&
1349 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1351 /* Only need to get the buflen */
1352 if (get_user(nr, &ufa->fa_nr))
1354 /* DoS protection */
1355 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1358 size = offsetof(struct fid_array, fa_fids[nr]);
1359 OBD_ALLOC(lfa, size);
1362 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1364 GOTO(free_lfa, rc = -ENOMEM);
1366 if (copy_from_user(lfa, arg, size))
1367 GOTO(free_rcs, rc = -EFAULT);
1369 /* Call mdc_iocontrol */
1370 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1372 for (i = 0; i < nr; i++)
1374 lfa->fa_fids[i].f_ver = rcs[i];
1375 if (copy_to_user(arg, lfa, size))
1380 OBD_FREE_PTR_ARRAY(rcs, nr);
1382 OBD_FREE(lfa, size);
1387 /* This function tries to get a single name component,
1388 * to send to the server. No actual path traversal involved,
1389 * so we limit to NAME_MAX */
1390 static char *ll_getname(const char __user *filename)
1395 OBD_ALLOC(tmp, NAME_MAX + 1);
1398 return ERR_PTR(-ENOMEM);
1400 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1403 else if (len > NAME_MAX)
1404 ret = -ENAMETOOLONG;
1407 OBD_FREE(tmp, NAME_MAX + 1);
1413 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1415 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1417 struct dentry *dentry = file_dentry(file);
1418 struct inode *inode = file_inode(file);
1419 struct ll_sb_info *sbi = ll_i2sbi(inode);
1420 struct obd_ioctl_data *data = NULL;
1424 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1425 PFID(ll_inode2fid(inode)), inode, cmd);
1427 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1428 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1431 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1433 case FS_IOC_GETFLAGS:
1434 case FS_IOC_SETFLAGS:
1435 RETURN(ll_iocontrol(inode, file, cmd, arg));
1436 case FSFILT_IOC_GETVERSION:
1437 case FS_IOC_GETVERSION:
1438 RETURN(put_user(inode->i_generation, (int __user *)arg));
1439 /* We need to special case any other ioctls we want to handle,
1440 * to send them to the MDS/OST as appropriate and to properly
1441 * network encode the arg field. */
1442 case FS_IOC_SETVERSION:
1445 case LL_IOC_GET_MDTIDX: {
1448 mdtidx = ll_get_mdt_idx(inode);
1452 if (put_user((int)mdtidx, (int __user *)arg))
1457 case IOC_MDC_LOOKUP: {
1458 int namelen, len = 0;
1461 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1465 filename = data->ioc_inlbuf1;
1466 namelen = strlen(filename);
1468 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1469 GOTO(out_free, rc = -EINVAL);
1472 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1474 CERROR("%s: lookup %.*s failed: rc = %d\n",
1475 sbi->ll_fsname, namelen, filename, rc);
1479 OBD_FREE_LARGE(data, len);
1482 case LL_IOC_LMV_SETSTRIPE: {
1483 struct lmv_user_md *lum;
1488 bool createonly = false;
1492 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1496 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1497 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1498 GOTO(lmv_out_free, rc = -EINVAL);
1500 filename = data->ioc_inlbuf1;
1501 namelen = data->ioc_inllen1;
1504 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1505 GOTO(lmv_out_free, rc = -EINVAL);
1507 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1508 lumlen = data->ioc_inllen2;
1510 if (!lmv_user_magic_supported(lum->lum_magic)) {
1511 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1512 lum->lum_magic, -EINVAL);
1513 GOTO(lmv_out_free, rc = -EINVAL);
1516 if ((lum->lum_magic == LMV_USER_MAGIC ||
1517 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1518 lumlen < sizeof(*lum)) {
1519 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1520 filename, lumlen, lum->lum_magic, -EINVAL);
1521 GOTO(lmv_out_free, rc = -EINVAL);
1524 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1525 lumlen < sizeof(struct lmv_foreign_md)) {
1526 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1527 filename, lum->lum_magic, lumlen, -EFAULT);
1528 GOTO(lmv_out_free, rc = -EINVAL);
1531 mode = data->ioc_type;
1532 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1533 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1536 OBD_FREE_LARGE(data, len);
1540 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1541 struct lmv_user_md lum;
1542 struct lmv_user_md __user *ulump =
1543 (struct lmv_user_md __user *)arg;
1546 if (copy_from_user(&lum, ulump, sizeof(lum)))
1549 if (lum.lum_magic != LMV_USER_MAGIC)
1552 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1556 case LL_IOC_LOV_SETSTRIPE_NEW:
1557 case LL_IOC_LOV_SETSTRIPE: {
1558 struct lov_user_md_v3 *lumv3 = NULL;
1559 struct lov_user_md_v1 lumv1;
1560 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1561 struct lov_user_md_v1 __user *lumv1p =
1562 (struct lov_user_md_v1 __user *)arg;
1563 struct lov_user_md_v3 __user *lumv3p =
1564 (struct lov_user_md_v3 __user *)arg;
1567 int set_default = 0;
1569 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1570 sizeof(struct lov_comp_md_v1));
1571 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1572 /* first try with v1 which is smaller than v3 */
1573 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1576 if (is_root_inode(inode))
1579 switch (lumv1.lmm_magic) {
1580 case LOV_USER_MAGIC_V3:
1581 case LOV_USER_MAGIC_SPECIFIC:
1582 lum_size = ll_lov_user_md_size(&lumv1);
1585 OBD_ALLOC(lumv3, lum_size);
1588 if (copy_from_user(lumv3, lumv3p, lum_size))
1589 GOTO(out, rc = -EFAULT);
1590 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1592 case LOV_USER_MAGIC_V1:
1595 GOTO(out, rc = -ENOTSUPP);
1598 /* in v1 and v3 cases lumv1 points to data */
1599 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1602 OBD_FREE(lumv3, lum_size);
1605 case LL_IOC_LMV_GETSTRIPE: {
1606 struct lmv_user_md __user *ulmv =
1607 (struct lmv_user_md __user *)arg;
1608 struct lmv_user_md lum;
1609 struct ptlrpc_request *request = NULL;
1610 struct ptlrpc_request *root_request = NULL;
1611 union lmv_mds_md *lmm = NULL;
1614 struct lmv_user_md *tmp = NULL;
1618 int max_stripe_count;
1622 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1625 max_stripe_count = lum.lum_stripe_count;
1626 /* lum_magic will indicate which stripe the ioctl will like
1627 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1628 * is for default LMV stripe */
1629 if (lum.lum_magic == LMV_MAGIC_V1)
1630 valid |= OBD_MD_MEA;
1631 else if (lum.lum_magic == LMV_USER_MAGIC)
1632 valid |= OBD_MD_DEFAULT_MEA;
1636 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1637 &request, &root_request, valid);
1639 GOTO(finish_req, rc);
1641 /* Get default LMV EA */
1642 if (lum.lum_magic == LMV_USER_MAGIC) {
1643 if (lmmsize > sizeof(*ulmv))
1644 GOTO(finish_req, rc = -EINVAL);
1646 if (root_request != NULL) {
1647 struct lmv_user_md *lum;
1648 struct ll_inode_info *lli;
1650 lum = (struct lmv_user_md *)lmm;
1651 lli = ll_i2info(inode);
1652 if (lum->lum_max_inherit !=
1653 LMV_INHERIT_UNLIMITED) {
1654 if (lum->lum_max_inherit ==
1656 lum->lum_max_inherit <
1658 lum->lum_max_inherit >
1660 lum->lum_max_inherit <
1662 GOTO(finish_req, rc = -ENODATA);
1664 if (lum->lum_max_inherit ==
1665 lli->lli_dir_depth) {
1666 lum->lum_max_inherit =
1668 lum->lum_max_inherit_rr =
1669 LMV_INHERIT_RR_NONE;
1673 lum->lum_max_inherit -=
1677 if (lum->lum_max_inherit_rr !=
1678 LMV_INHERIT_RR_UNLIMITED) {
1679 if (lum->lum_max_inherit_rr ==
1681 lum->lum_max_inherit_rr <
1682 LMV_INHERIT_RR_END ||
1683 lum->lum_max_inherit_rr >
1684 LMV_INHERIT_RR_MAX ||
1685 lum->lum_max_inherit_rr <=
1686 lli->lli_dir_depth) {
1687 lum->lum_max_inherit_rr =
1688 LMV_INHERIT_RR_NONE;
1692 if (lum->lum_max_inherit_rr >
1694 lum->lum_max_inherit_rr -=
1699 if (copy_to_user(ulmv, lmm, lmmsize))
1700 GOTO(finish_req, rc = -EFAULT);
1702 GOTO(finish_req, rc);
1705 /* if foreign LMV case, fake stripes number */
1706 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1707 struct lmv_foreign_md *lfm;
1709 lfm = (struct lmv_foreign_md *)lmm;
1710 if (lfm->lfm_length < XATTR_SIZE_MAX -
1711 offsetof(typeof(*lfm), lfm_value)) {
1712 __u32 size = lfm->lfm_length +
1713 offsetof(typeof(*lfm), lfm_value);
1715 stripe_count = lmv_foreign_to_md_stripes(size);
1717 CERROR("invalid %d foreign size returned\n",
1722 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1724 if (max_stripe_count < stripe_count) {
1725 lum.lum_stripe_count = stripe_count;
1726 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1727 GOTO(finish_req, rc = -EFAULT);
1728 GOTO(finish_req, rc = -E2BIG);
1731 /* enough room on user side and foreign case */
1732 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1733 struct lmv_foreign_md *lfm;
1736 lfm = (struct lmv_foreign_md *)lmm;
1737 size = lfm->lfm_length +
1738 offsetof(struct lmv_foreign_md, lfm_value);
1739 if (copy_to_user(ulmv, lfm, size))
1740 GOTO(finish_req, rc = -EFAULT);
1741 GOTO(finish_req, rc);
1744 lum_size = lmv_user_md_size(stripe_count,
1745 LMV_USER_MAGIC_SPECIFIC);
1746 OBD_ALLOC(tmp, lum_size);
1748 GOTO(finish_req, rc = -ENOMEM);
1750 mdt_index = ll_get_mdt_idx(inode);
1752 GOTO(out_tmp, rc = -ENOMEM);
1754 tmp->lum_magic = LMV_MAGIC_V1;
1755 tmp->lum_stripe_count = 0;
1756 tmp->lum_stripe_offset = mdt_index;
1757 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1758 for (i = 0; i < stripe_count; i++) {
1761 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1762 if (fid_is_sane(&fid)) {
1763 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1765 GOTO(out_tmp, rc = mdt_index);
1767 tmp->lum_objects[i].lum_mds = mdt_index;
1768 tmp->lum_objects[i].lum_fid = fid;
1771 tmp->lum_stripe_count++;
1774 if (copy_to_user(ulmv, tmp, lum_size))
1775 GOTO(out_tmp, rc = -EFAULT);
1777 OBD_FREE(tmp, lum_size);
1779 ptlrpc_req_finished(request);
1780 ptlrpc_req_finished(root_request);
1784 case LL_IOC_UNLOCK_FOREIGN:
1785 /* if not a foreign symlink do nothing */
1786 if (ll_foreign_is_removable(dentry, true)) {
1788 "prevent rmdir of non-foreign dir ("DFID")\n",
1789 PFID(ll_inode2fid(inode)));
1790 RETURN(-EOPNOTSUPP);
1794 case LL_IOC_REMOVE_ENTRY: {
1795 char *filename = NULL;
1799 /* Here is a little hack to avoid sending REINT_RMENTRY to
1800 * unsupported server, which might crash the server(LU-2730),
1801 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1802 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1803 * server will support REINT_RMENTRY XXX*/
1804 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1807 filename = ll_getname((const char __user *)arg);
1808 if (IS_ERR(filename))
1809 RETURN(PTR_ERR(filename));
1811 namelen = strlen(filename);
1813 GOTO(out_rmdir, rc = -EINVAL);
1815 rc = ll_rmdir_entry(inode, filename, namelen);
1818 ll_putname(filename);
1822 RETURN(ll_rmfid(file, (void __user *)arg));
1823 case LL_IOC_LOV_SWAP_LAYOUTS:
1825 case IOC_OBD_STATFS:
1826 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1827 case LL_IOC_LOV_GETSTRIPE:
1828 case LL_IOC_LOV_GETSTRIPE_NEW:
1829 case LL_IOC_MDC_GETINFO_V1:
1830 case LL_IOC_MDC_GETINFO_V2:
1831 case IOC_MDC_GETFILEINFO_V1:
1832 case IOC_MDC_GETFILEINFO_V2:
1833 case IOC_MDC_GETFILESTRIPE: {
1834 struct ptlrpc_request *request = NULL;
1835 struct ptlrpc_request *root_request = NULL;
1836 struct lov_user_md __user *lump;
1837 struct lov_mds_md *lmm = NULL;
1838 struct mdt_body *body;
1839 char *filename = NULL;
1840 lstat_t __user *statp = NULL;
1841 lstatx_t __user *stxp = NULL;
1842 __u64 __user *flagsp = NULL;
1843 __u32 __user *lmmsizep = NULL;
1844 struct lu_fid __user *fidp = NULL;
1848 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1849 cmd == IOC_MDC_GETFILEINFO_V2 ||
1850 cmd == IOC_MDC_GETFILESTRIPE) {
1851 filename = ll_getname((const char __user *)arg);
1852 if (IS_ERR(filename))
1853 RETURN(PTR_ERR(filename));
1855 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1856 &lmmsize, &request);
1858 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1864 body = req_capsule_server_get(&request->rq_pill,
1866 LASSERT(body != NULL);
1871 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1872 cmd == LL_IOC_MDC_GETINFO_V1 ||
1873 cmd == IOC_MDC_GETFILEINFO_V2 ||
1874 cmd == LL_IOC_MDC_GETINFO_V2)) {
1882 if (cmd == IOC_MDC_GETFILESTRIPE ||
1883 cmd == LL_IOC_LOV_GETSTRIPE ||
1884 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1885 lump = (struct lov_user_md __user *)arg;
1886 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1887 cmd == LL_IOC_MDC_GETINFO_V1){
1888 struct lov_user_mds_data_v1 __user *lmdp;
1890 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1891 statp = &lmdp->lmd_st;
1892 lump = &lmdp->lmd_lmm;
1894 struct lov_user_mds_data __user *lmdp;
1896 lmdp = (struct lov_user_mds_data __user *)arg;
1897 fidp = &lmdp->lmd_fid;
1898 stxp = &lmdp->lmd_stx;
1899 flagsp = &lmdp->lmd_flags;
1900 lmmsizep = &lmdp->lmd_lmmsize;
1901 lump = &lmdp->lmd_lmm;
1905 /* If the file has no striping then zero out *lump so
1906 * that the caller isn't confused by garbage. */
1907 if (clear_user(lump, sizeof(*lump)))
1908 GOTO(out_req, rc = -EFAULT);
1909 } else if (copy_to_user(lump, lmm, lmmsize)) {
1910 if (copy_to_user(lump, lmm, sizeof(*lump)))
1911 GOTO(out_req, rc = -EFAULT);
1914 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1916 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1917 cmd == LL_IOC_MDC_GETINFO_V1) {
1920 st.st_dev = inode->i_sb->s_dev;
1921 st.st_mode = body->mbo_mode;
1922 st.st_nlink = body->mbo_nlink;
1923 st.st_uid = body->mbo_uid;
1924 st.st_gid = body->mbo_gid;
1925 st.st_rdev = body->mbo_rdev;
1926 if (llcrypt_require_key(inode) == -ENOKEY)
1927 st.st_size = round_up(st.st_size,
1928 LUSTRE_ENCRYPTION_UNIT_SIZE);
1930 st.st_size = body->mbo_size;
1931 st.st_blksize = PAGE_SIZE;
1932 st.st_blocks = body->mbo_blocks;
1933 st.st_atime = body->mbo_atime;
1934 st.st_mtime = body->mbo_mtime;
1935 st.st_ctime = body->mbo_ctime;
1936 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1939 if (copy_to_user(statp, &st, sizeof(st)))
1940 GOTO(out_req, rc = -EFAULT);
1941 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1942 cmd == LL_IOC_MDC_GETINFO_V2) {
1943 lstatx_t stx = { 0 };
1944 __u64 valid = body->mbo_valid;
1946 stx.stx_blksize = PAGE_SIZE;
1947 stx.stx_nlink = body->mbo_nlink;
1948 stx.stx_uid = body->mbo_uid;
1949 stx.stx_gid = body->mbo_gid;
1950 stx.stx_mode = body->mbo_mode;
1951 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1953 if (llcrypt_require_key(inode) == -ENOKEY)
1954 stx.stx_size = round_up(stx.stx_size,
1955 LUSTRE_ENCRYPTION_UNIT_SIZE);
1957 stx.stx_size = body->mbo_size;
1958 stx.stx_blocks = body->mbo_blocks;
1959 stx.stx_atime.tv_sec = body->mbo_atime;
1960 stx.stx_ctime.tv_sec = body->mbo_ctime;
1961 stx.stx_mtime.tv_sec = body->mbo_mtime;
1962 stx.stx_btime.tv_sec = body->mbo_btime;
1963 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1964 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1965 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1966 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1967 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1970 * For a striped directory, the size and blocks returned
1971 * from MDT is not correct.
1972 * The size and blocks are aggregated by client across
1974 * Thus for a striped directory, do not return the valid
1975 * FLSIZE and FLBLOCKS flags to the caller.
1976 * However, this whould be better decided by the MDS
1977 * instead of the client.
1979 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
1980 ll_i2info(inode)->lli_lsm_md != NULL)
1981 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1983 if (flagsp && copy_to_user(flagsp, &valid,
1985 GOTO(out_req, rc = -EFAULT);
1987 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1989 GOTO(out_req, rc = -EFAULT);
1991 if (!(valid & OBD_MD_FLSIZE))
1992 stx.stx_mask &= ~STATX_SIZE;
1993 if (!(valid & OBD_MD_FLBLOCKS))
1994 stx.stx_mask &= ~STATX_BLOCKS;
1996 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1997 GOTO(out_req, rc = -EFAULT);
1999 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2001 GOTO(out_req, rc = -EFAULT);
2006 ptlrpc_req_finished(request);
2007 ptlrpc_req_finished(root_request);
2009 ll_putname(filename);
2012 case OBD_IOC_QUOTACTL: {
2013 struct if_quotactl *qctl;
2014 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2016 OBD_ALLOC(qctl, qctl_len);
2020 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
2021 GOTO(out_quotactl, rc = -EFAULT);
2023 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2024 char __user *from = (char __user *)arg +
2025 offsetof(typeof(*qctl), qc_poolname);
2026 if (copy_from_user(qctl->qc_poolname, from,
2027 LOV_MAXPOOLNAME + 1))
2028 GOTO(out_quotactl, rc = -EFAULT);
2031 rc = quotactl_ioctl(inode->i_sb, qctl);
2032 if ((rc == 0 || rc == -ENODATA) &&
2033 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
2037 OBD_FREE(qctl, qctl_len);
2040 case OBD_IOC_GETNAME_OLD:
2041 case OBD_IOC_GETDTNAME:
2042 case OBD_IOC_GETMDNAME:
2043 RETURN(ll_get_obd_name(inode, cmd, arg));
2044 case LL_IOC_FLUSHCTX:
2045 RETURN(ll_flush_ctx(inode));
2046 case LL_IOC_GETOBDCOUNT: {
2048 struct obd_export *exp;
2050 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
2053 /* get ost count when count is zero, get mdt count otherwise */
2054 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2055 vallen = sizeof(count);
2056 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2057 KEY_TGT_COUNT, &vallen, &count);
2059 CERROR("get target count failed: %d\n", rc);
2063 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
2068 case LL_IOC_PATH2FID:
2069 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2070 sizeof(struct lu_fid)))
2073 case LL_IOC_GET_CONNECT_FLAGS: {
2074 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
2075 (void __user *)arg));
2077 case OBD_IOC_FID2PATH:
2078 RETURN(ll_fid2path(inode, (void __user *)arg));
2079 case LL_IOC_GETPARENT:
2080 RETURN(ll_getparent(file, (void __user *)arg));
2081 case LL_IOC_FID2MDTIDX: {
2082 struct obd_export *exp = ll_i2mdexp(inode);
2086 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
2090 /* Call mdc_iocontrol */
2091 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2092 (__u32 __user *)&index);
2098 case LL_IOC_HSM_REQUEST: {
2099 struct hsm_user_request *hur;
2106 /* We don't know the true size yet; copy the fixed-size part */
2107 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
2112 /* Compute the whole struct size */
2113 totalsize = hur_len(hur);
2118 /* Final size will be more than double totalsize */
2119 if (totalsize >= MDS_MAXREQSIZE / 3)
2122 OBD_ALLOC_LARGE(hur, totalsize);
2126 /* Copy the whole struct */
2127 if (copy_from_user(hur, (void __user *)arg, totalsize))
2128 GOTO(out_hur, rc = -EFAULT);
2130 if (hur->hur_request.hr_action == HUA_RELEASE) {
2131 const struct lu_fid *fid;
2135 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2136 fid = &hur->hur_user_item[i].hui_fid;
2137 f = search_inode_for_lustre(inode->i_sb, fid);
2143 rc = ll_hsm_release(f);
2149 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2154 OBD_FREE_LARGE(hur, totalsize);
2158 case LL_IOC_HSM_PROGRESS: {
2159 struct hsm_progress_kernel hpk;
2160 struct hsm_progress hp;
2162 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2165 hpk.hpk_fid = hp.hp_fid;
2166 hpk.hpk_cookie = hp.hp_cookie;
2167 hpk.hpk_extent = hp.hp_extent;
2168 hpk.hpk_flags = hp.hp_flags;
2169 hpk.hpk_errval = hp.hp_errval;
2170 hpk.hpk_data_version = 0;
2172 /* File may not exist in Lustre; all progress
2173 * reported to Lustre root */
2174 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2178 case LL_IOC_HSM_CT_START:
2179 if (!capable(CAP_SYS_ADMIN))
2182 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2183 (struct lustre_kernelcomm __user *)arg);
2186 case LL_IOC_HSM_COPY_START: {
2187 struct hsm_copy *copy;
2190 OBD_ALLOC_PTR(copy);
2193 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2198 rc = ll_ioc_copy_start(inode->i_sb, copy);
2199 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2205 case LL_IOC_HSM_COPY_END: {
2206 struct hsm_copy *copy;
2209 OBD_ALLOC_PTR(copy);
2212 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2217 rc = ll_ioc_copy_end(inode->i_sb, copy);
2218 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2224 case LL_IOC_MIGRATE: {
2225 struct lmv_user_md *lum;
2232 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
2236 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2237 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2238 GOTO(migrate_free, rc = -EINVAL);
2240 filename = data->ioc_inlbuf1;
2241 namelen = data->ioc_inllen1;
2242 flags = data->ioc_type;
2244 if (namelen < 1 || namelen != strlen(filename) + 1) {
2245 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2246 GOTO(migrate_free, rc = -EINVAL);
2249 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2250 if (lum->lum_magic != LMV_USER_MAGIC &&
2251 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2253 CERROR("%s: wrong lum magic %x: rc = %d\n",
2254 filename, lum->lum_magic, rc);
2255 GOTO(migrate_free, rc);
2258 rc = ll_migrate(inode, file, lum, filename, flags);
2260 OBD_FREE_LARGE(data, len);
2264 case FS_IOC_FSGETXATTR:
2265 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2266 case FS_IOC_FSSETXATTR:
2267 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2268 case LL_IOC_PROJECT:
2269 RETURN(ll_ioctl_project(file, cmd, arg));
2270 case LL_IOC_PCC_DETACH_BY_FID: {
2271 struct lu_pcc_detach_fid *detach;
2273 struct inode *inode2;
2276 OBD_ALLOC_PTR(detach);
2280 if (copy_from_user(detach,
2281 (const struct lu_pcc_detach_fid __user *)arg,
2283 GOTO(out_detach, rc = -EFAULT);
2285 fid = &detach->pccd_fid;
2286 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2287 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2289 /* Target inode is not in inode cache, and PCC file
2290 * has aleady released, return immdiately.
2292 GOTO(out_detach, rc = 0);
2294 if (!S_ISREG(inode2->i_mode))
2295 GOTO(out_iput, rc = -EINVAL);
2297 if (!inode_owner_or_capable(&init_user_ns, inode2))
2298 GOTO(out_iput, rc = -EPERM);
2300 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2304 OBD_FREE_PTR(detach);
2307 #ifdef HAVE_LUSTRE_CRYPTO
2308 case LL_IOC_SET_ENCRYPTION_POLICY:
2309 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2311 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2312 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2313 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2315 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2316 case LL_IOC_ADD_ENCRYPTION_KEY:
2317 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2319 return llcrypt_ioctl_add_key(file, (void __user *)arg);
2320 case LL_IOC_REMOVE_ENCRYPTION_KEY:
2321 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2323 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2324 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2325 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2327 return llcrypt_ioctl_remove_key_all_users(file,
2328 (void __user *)arg);
2329 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2330 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2332 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2335 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2336 (void __user *)arg));
2340 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2342 struct inode *inode = file->f_mapping->host;
2343 struct ll_file_data *fd = file->private_data;
2344 struct ll_sb_info *sbi = ll_i2sbi(inode);
2345 int api32 = ll_need_32bit_api(sbi);
2346 loff_t ret = -EINVAL;
2354 offset += file->f_pos;
2360 offset += LL_DIR_END_OFF_32BIT;
2362 offset += LL_DIR_END_OFF;
2369 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2370 (!api32 && offset <= LL_DIR_END_OFF))) {
2371 if (offset != file->f_pos) {
2374 hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2375 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2376 (!api32 && offset == LL_DIR_END_OFF))
2377 fd->lfd_pos = MDS_DIR_END_OFF;
2378 else if (api32 && hash64)
2379 fd->lfd_pos = offset << 32;
2381 fd->lfd_pos = offset;
2382 file->f_pos = offset;
2383 file->f_version = 0;
2390 inode_unlock(inode);
2394 static int ll_dir_open(struct inode *inode, struct file *file)
2397 RETURN(ll_file_open(inode, file));
2400 static int ll_dir_release(struct inode *inode, struct file *file)
2403 RETURN(ll_file_release(inode, file));
2406 /* notify error if partially read striped directory */
2407 static int ll_dir_flush(struct file *file, fl_owner_t id)
2409 struct ll_file_data *lfd = file->private_data;
2410 int rc = lfd->fd_partial_readdir_rc;
2412 lfd->fd_partial_readdir_rc = 0;
2417 const struct file_operations ll_dir_operations = {
2418 .llseek = ll_dir_seek,
2419 .open = ll_dir_open,
2420 .release = ll_dir_release,
2421 .read = generic_read_dir,
2422 #ifdef HAVE_DIR_CONTEXT
2423 .iterate_shared = ll_iterate,
2425 .readdir = ll_readdir,
2427 .unlocked_ioctl = ll_dir_ioctl,
2429 .flush = ll_dir_flush,