4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Directory code for lustre client.
37 #include <linux/pagemap.h>
39 #include <linux/version.h>
40 #include <linux/security.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43 #include <linux/uaccess.h>
44 #include <linux/buffer_head.h> // for wait_on_buffer
45 #include <linux/pagevec.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_lib.h>
53 #include <lustre_dlm.h>
54 #include <lustre_compat.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
58 #include <libcfs/libcfs_crypto.h>
60 #include "llite_internal.h"
63 * (new) readdir implementation overview.
65 * Original lustre readdir implementation cached exact copy of raw directory
66 * pages on the client. These pages were indexed in client page cache by
67 * logical offset in the directory file. This design, while very simple and
68 * intuitive had some inherent problems:
70 * . it implies that byte offset to the directory entry serves as a
71 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72 * ext3/htree directory entries may move due to splits, and more
75 * . it is incompatible with the design of split directories for cmd3,
76 * that assumes that names are distributed across nodes based on their
77 * hash, and so readdir should be done in hash order.
79 * New readdir implementation does readdir in hash order, and uses hash of a
80 * file name as a telldir/seekdir cookie. This led to number of complications:
82 * . hash is not unique, so it cannot be used to index cached directory
83 * pages on the client (note, that it requires a whole pageful of hash
84 * collided entries to cause two pages to have identical hashes);
86 * . hash is not unique, so it cannot, strictly speaking, be used as an
87 * entry cookie. ext3/htree has the same problem and lustre implementation
88 * mimics their solution: seekdir(hash) positions directory at the first
89 * entry with the given hash.
95 * Client caches directory pages using hash of the first entry as an index. As
96 * noted above hash is not unique, so this solution doesn't work as is:
97 * special processing is needed for "page hash chains" (i.e., sequences of
98 * pages filled with entries all having the same hash value).
100 * First, such chains have to be detected. To this end, server returns to the
101 * client the hash of the first entry on the page next to one returned. When
102 * client detects that this hash is the same as hash of the first entry on the
103 * returned page, page hash collision has to be handled. Pages in the
104 * hash chain, except first one, are termed "overflow pages".
106 * Proposed (unimplimented) solution to index uniqueness problem is to
107 * not cache overflow pages. Instead, when page hash collision is
108 * detected, all overflow pages from emerging chain should be
109 * immediately requested from the server and placed in a special data
110 * structure. This data structure can be used by ll_readdir() to
111 * process entries from overflow pages. When readdir invocation
112 * finishes, overflow pages are discarded. If page hash collision chain
113 * weren't completely processed, next call to readdir will again detect
114 * page hash collision, again read overflow pages in, process next
115 * portion of entries and again discard the pages. This is not as
116 * wasteful as it looks, because, given reasonable hash, page hash
117 * collisions are extremely rare.
119 * 1. directory positioning
121 * When seekdir(hash) is called.
123 * seekdir() sets the location in the directory stream from which the next
124 * readdir() call will start. mdc_page_locate() is used to find page with
125 * starting hash and will issue RPC to fetch that page. If there is a hash
126 * collision the concerned page is removed.
131 * identification of and access to overflow pages
135 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
136 * a header lu_dirpage which describes the start/end hash, and whether this
137 * page is empty (contains no dir entry) or hash collide with next page.
138 * After client receives reply, several pages will be integrated into dir page
139 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
140 * lu_dirpage for this integrated page will be adjusted. See
141 * mdc_adjust_dirpages().
144 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
145 __u64 offset, int *partial_readdir_rc)
147 struct md_readdir_info mrinfo = {
148 .mr_blocking_ast = ll_md_blocking_ast };
152 rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
156 if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
157 *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
162 void ll_release_page(struct inode *inode, struct page *page,
167 /* Always remove the page for striped dir, because the page is
168 * built from temporarily in LMV layer */
169 if (inode && S_ISDIR(inode->i_mode) &&
170 lmv_dir_striped(ll_i2info(inode)->lli_lsm_md)) {
177 if (likely(page->mapping != NULL))
178 cfs_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) {
291 /* End of directory reached. */
293 ll_release_page(inode, page, false);
295 /* Normal case: continue to the next page.*/
296 ll_release_page(inode, page,
297 le32_to_cpu(dp->ldp_flags) &
300 page = ll_get_dir_page(inode, op_data, pos,
304 #ifdef HAVE_DIR_CONTEXT
309 llcrypt_fname_free_buffer(&lltr);
313 #ifdef HAVE_DIR_CONTEXT
314 static int ll_iterate(struct file *filp, struct dir_context *ctx)
316 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
319 struct inode *inode = file_inode(filp);
320 struct ll_file_data *lfd = filp->private_data;
321 struct ll_sb_info *sbi = ll_i2sbi(inode);
322 bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
323 int api32 = ll_need_32bit_api(sbi);
324 struct md_op_data *op_data;
325 struct lu_fid pfid = { 0 };
326 ktime_t kstart = ktime_get();
327 /* result of possible partial readdir */
328 int partial_readdir_rc = 0;
334 LASSERT(lfd != NULL);
338 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
339 PFID(ll_inode2fid(inode)),
340 inode, (unsigned long)pos, i_size_read(inode), api32);
342 if (IS_ENCRYPTED(inode)) {
343 rc = llcrypt_prepare_readdir(inode);
344 if (rc && rc != -ENOKEY)
348 if (pos == MDS_DIR_END_OFF)
352 if (unlikely(ll_dir_striped(inode))) {
353 /* Only needed for striped dir to fill ..see lmv_read_page() */
354 if (file_dentry(filp)->d_parent != NULL &&
355 file_dentry(filp)->d_parent->d_inode != NULL) {
356 __u64 ibits = MDS_INODELOCK_LOOKUP;
357 struct inode *parent =
358 file_dentry(filp)->d_parent->d_inode;
360 if (ll_have_md_lock(ll_i2mdexp(parent), parent, &ibits,
362 pfid = *ll_inode2fid(parent);
365 /* If it can not find in cache, do lookup .. on the master
367 if (fid_is_zero(&pfid)) {
368 rc = ll_dir_get_parent_fid(inode, &pfid);
374 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
375 LUSTRE_OPC_ANY, inode);
377 GOTO(out, rc = PTR_ERR(op_data));
379 /* foreign dirs are browsed out of Lustre */
380 if (unlikely(op_data->op_mea1 != NULL &&
381 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
382 ll_finish_md_op_data(op_data);
386 op_data->op_fid3 = pfid;
388 #ifdef HAVE_DIR_CONTEXT
390 rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
393 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
394 &partial_readdir_rc);
397 if (!lfd->fd_partial_readdir_rc)
398 lfd->fd_partial_readdir_rc = partial_readdir_rc;
400 if (pos == MDS_DIR_END_OFF) {
402 pos = LL_DIR_END_OFF_32BIT;
404 pos = LL_DIR_END_OFF;
409 #ifdef HAVE_DIR_CONTEXT
414 ll_finish_md_op_data(op_data);
418 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
419 ktime_us_delta(ktime_get(), kstart));
425 * Create striped directory with specified stripe(@lump)
427 * \param[in] dparent the parent of the directory.
428 * \param[in] lump the specified stripes.
429 * \param[in] dirname the name of the directory.
430 * \param[in] mode the specified mode of the directory.
432 * \retval =0 if striped directory is being created successfully.
433 * <0 if the creation is failed.
435 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
436 size_t len, const char *dirname, umode_t mode,
439 struct inode *parent = dparent->d_inode;
440 struct ptlrpc_request *request = NULL;
441 struct md_op_data *op_data;
442 struct ll_sb_info *sbi = ll_i2sbi(parent);
443 struct inode *inode = NULL;
444 struct dentry dentry = {
448 .len = strlen(dirname),
449 .hash = ll_full_name_hash(dparent, dirname,
452 .d_sb = dparent->d_sb,
454 bool encrypt = false;
459 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
462 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
464 "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
465 PFID(ll_inode2fid(parent)), parent, dirname,
466 (int)lump->lum_stripe_offset, lump->lum_stripe_count,
467 lump->lum_hash_type);
469 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
472 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
473 PFID(ll_inode2fid(parent)), parent, dirname,
474 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
477 if (lump->lum_stripe_count > 1 &&
478 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
481 if (IS_DEADDIR(parent) &&
482 !CFS_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
485 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
486 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
488 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
489 lump->lum_hash_type = cfs_fail_val;
490 } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
491 enum lmv_hash_type type = lump->lum_hash_type &
494 if (type >= LMV_HASH_TYPE_CRUSH ||
495 type == LMV_HASH_TYPE_UNKNOWN)
496 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
497 LMV_HASH_TYPE_FNV_1A_64;
500 hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
501 if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
504 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
505 lustre_swab_lmv_user_md(lump);
507 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
508 mode &= ~current_umask();
509 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
510 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
511 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
514 RETURN(PTR_ERR(op_data));
516 op_data->op_dir_depth = ll_i2info(parent)->lli_inherit_depth ?:
517 ll_i2info(parent)->lli_dir_depth;
519 if (ll_sbi_has_encrypt(sbi) &&
520 (IS_ENCRYPTED(parent) ||
521 unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
522 err = llcrypt_prepare_readdir(parent);
524 GOTO(out_op_data, err);
525 if (!llcrypt_has_encryption_key(parent))
526 GOTO(out_op_data, err = -ENOKEY);
530 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
531 /* selinux_dentry_init_security() uses dentry->d_parent and name
532 * to determine the security context for the file. So our fake
533 * dentry should be real enough for this purpose. */
534 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
535 &op_data->op_file_secctx_name,
536 &op_data->op_file_secctx_name_size,
537 &op_data->op_file_secctx,
538 &op_data->op_file_secctx_size,
539 &op_data->op_file_secctx_slot);
541 GOTO(out_op_data, err);
545 err = llcrypt_inherit_context(parent, NULL, op_data, false);
547 GOTO(out_op_data, err);
550 op_data->op_cli_flags |= CLI_SET_MEA;
552 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
554 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
555 from_kuid(&init_user_ns, current_fsuid()),
556 from_kgid(&init_user_ns, current_fsgid()),
557 current_cap(), 0, &request);
559 GOTO(out_request, err);
561 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
563 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
565 GOTO(out_inode, err);
567 dentry.d_inode = inode;
569 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags))
570 err = ll_inode_notifysecctx(inode, op_data->op_file_secctx,
571 op_data->op_file_secctx_size);
573 err = ll_inode_init_security(&dentry, inode, parent);
576 GOTO(out_inode, err);
579 err = ll_set_encflags(inode, op_data->op_file_encctx,
580 op_data->op_file_encctx_size, false);
582 GOTO(out_inode, err);
588 ptlrpc_req_finished(request);
590 ll_finish_md_op_data(op_data);
595 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
598 struct ll_sb_info *sbi = ll_i2sbi(inode);
599 struct md_op_data *op_data;
600 struct ptlrpc_request *req = NULL;
606 switch (lump->lmm_magic) {
607 case LOV_USER_MAGIC_V1:
608 lum_size = sizeof(struct lov_user_md_v1);
610 case LOV_USER_MAGIC_V3:
611 lum_size = sizeof(struct lov_user_md_v3);
613 case LOV_USER_MAGIC_COMP_V1:
614 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
616 case LMV_USER_MAGIC: {
617 struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
619 /* MDS < 2.14 doesn't support 'crush' hash type, and
620 * cannot handle unknown hash if client doesn't set a
621 * valid one. switch to fnv_1a_64.
623 if (!(exp_connect_flags2(sbi->ll_md_exp) &
624 OBD_CONNECT2_CRUSH)) {
625 enum lmv_hash_type type = lmv->lum_hash_type &
628 if (type >= LMV_HASH_TYPE_CRUSH ||
629 type == LMV_HASH_TYPE_UNKNOWN)
631 (lmv->lum_hash_type ^ type) |
632 LMV_HASH_TYPE_FNV_1A_64;
634 if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
635 lustre_swab_lmv_user_md(lmv);
636 lum_size = sizeof(*lmv);
639 case LOV_USER_MAGIC_SPECIFIC: {
640 struct lov_user_md_v3 *v3 =
641 (struct lov_user_md_v3 *)lump;
642 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
644 lum_size = lov_user_md_size(v3->lmm_stripe_count,
645 LOV_USER_MAGIC_SPECIFIC);
650 "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
651 lump->lmm_magic, LOV_USER_MAGIC_V1,
656 /* This is coming from userspace, so should be in
657 * local endian. But the MDS would like it in little
658 * endian, so we swab it before we send it.
660 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
661 le32_to_cpu(LOV_MAGIC_MAGIC))
662 lustre_swab_lov_user_md(lump, 0);
664 lum_size = sizeof(struct lov_user_md_v1);
667 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
668 LUSTRE_OPC_ANY, NULL);
670 RETURN(PTR_ERR(op_data));
672 /* swabbing is done in lov_setstripe() on server side */
673 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
674 ll_finish_md_op_data(op_data);
675 ptlrpc_req_finished(req);
682 /* get default LMV from client cache */
683 static int ll_dir_get_default_lmv(struct inode *inode, struct lmv_user_md *lum)
685 struct ll_inode_info *lli = ll_i2info(inode);
686 const struct lmv_stripe_md *lsm;
687 bool fs_dmv_got = false;
692 if (lli->lli_default_lsm_md) {
693 down_read(&lli->lli_lsm_sem);
694 lsm = lli->lli_default_lsm_md;
696 lum->lum_magic = lsm->lsm_md_magic;
697 lum->lum_stripe_count = lsm->lsm_md_stripe_count;
698 lum->lum_stripe_offset = lsm->lsm_md_master_mdt_index;
699 lum->lum_hash_type = lsm->lsm_md_hash_type;
700 lum->lum_max_inherit = lsm->lsm_md_max_inherit;
701 lum->lum_max_inherit_rr = lsm->lsm_md_max_inherit_rr;
704 up_read(&lli->lli_lsm_sem);
707 if (rc == -ENODATA && !is_root_inode(inode) && !fs_dmv_got) {
708 lli = ll_i2info(inode->i_sb->s_root->d_inode);
713 if (!rc && fs_dmv_got) {
714 lli = ll_i2info(inode);
715 if (lum->lum_max_inherit != LMV_INHERIT_UNLIMITED) {
716 if (lum->lum_max_inherit == LMV_INHERIT_NONE ||
717 lum->lum_max_inherit < LMV_INHERIT_END ||
718 lum->lum_max_inherit > LMV_INHERIT_MAX ||
719 lum->lum_max_inherit <= lli->lli_dir_depth)
720 GOTO(out, rc = -ENODATA);
722 lum->lum_max_inherit -= lli->lli_dir_depth;
725 if (lum->lum_max_inherit_rr != LMV_INHERIT_RR_UNLIMITED) {
726 if (lum->lum_max_inherit_rr == LMV_INHERIT_NONE ||
727 lum->lum_max_inherit_rr < LMV_INHERIT_RR_END ||
728 lum->lum_max_inherit_rr > LMV_INHERIT_RR_MAX ||
729 lum->lum_max_inherit_rr <= lli->lli_dir_depth)
730 lum->lum_max_inherit_rr = LMV_INHERIT_RR_NONE;
732 if (lum->lum_max_inherit_rr > lli->lli_dir_depth)
733 lum->lum_max_inherit_rr -= lli->lli_dir_depth;
740 int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
741 struct ptlrpc_request **request, u64 valid,
742 enum get_default_layout_type type)
744 struct ll_sb_info *sbi = ll_i2sbi(inode);
745 struct mdt_body *body;
746 struct lov_mds_md *lmm = NULL;
747 struct ptlrpc_request *req = NULL;
748 int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
749 struct md_op_data *op_data;
755 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
756 LUSTRE_OPC_ANY, NULL);
758 RETURN(PTR_ERR(op_data));
760 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
762 if (type == GET_DEFAULT_LAYOUT_ROOT) {
763 lu_root_fid(&op_data->op_fid1);
764 fid = op_data->op_fid1;
766 fid = *ll_inode2fid(inode);
769 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
770 ll_finish_md_op_data(op_data);
772 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
777 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
778 LASSERT(body != NULL);
780 lmm_size = body->mbo_eadatasize;
782 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
784 GOTO(out, rc = -ENODATA);
787 lmm = req_capsule_server_sized_get(&req->rq_pill,
788 &RMF_MDT_MD, lmm_size);
789 LASSERT(lmm != NULL);
791 /* This is coming from the MDS, so is probably in
792 * little endian. We convert it to host endian before
793 * passing it to userspace.
795 /* We don't swab objects for directories */
796 switch (le32_to_cpu(lmm->lmm_magic)) {
799 case LOV_MAGIC_COMP_V1:
800 case LOV_USER_MAGIC_SPECIFIC:
801 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
802 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
805 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
806 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
809 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
810 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
812 case LMV_MAGIC_FOREIGN: {
813 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
815 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
816 __swab32s(&lfm->lfm_magic);
817 __swab32s(&lfm->lfm_length);
818 __swab32s(&lfm->lfm_type);
819 __swab32s(&lfm->lfm_flags);
825 CERROR("%s: unknown magic: %lX: rc = %d\n", sbi->ll_fsname,
826 (unsigned long)lmm->lmm_magic, rc);
830 *plmm_size = lmm_size;
836 * This function will be used to get default LOV/LMV/Default LMV
837 * @valid will be used to indicate which stripe it will retrieve.
838 * If the directory does not have its own default layout, then the
839 * function will request the default layout from root FID.
840 * OBD_MD_MEA LMV stripe EA
841 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
842 * otherwise Default LOV EA.
843 * Each time, it can only retrieve 1 stripe EA
845 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
846 struct ptlrpc_request **request,
847 struct ptlrpc_request **root_request,
850 struct ptlrpc_request *req = NULL;
851 struct ptlrpc_request *root_req = NULL;
852 struct lov_mds_md *lmm = NULL;
857 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
859 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
860 !(valid & OBD_MD_MEA) && root_request != NULL) {
861 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
862 &lmm_size, &root_req, valid,
863 GET_DEFAULT_LAYOUT_ROOT);
869 *plmm_size = lmm_size;
871 if (root_request != NULL)
872 *root_request = root_req;
878 * This function will be used to get default LOV/LMV/Default LMV
879 * @valid will be used to indicate which stripe it will retrieve
880 * OBD_MD_MEA LMV stripe EA
881 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
882 * otherwise Default LOV EA.
883 * Each time, it can only retrieve 1 stripe EA
885 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
886 struct ptlrpc_request **request, u64 valid)
888 struct ptlrpc_request *req = NULL;
889 struct lov_mds_md *lmm = NULL;
894 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
898 *plmm_size = lmm_size;
904 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
906 struct md_op_data *op_data;
911 OBD_ALLOC_PTR(op_data);
915 op_data->op_flags |= MF_GET_MDT_IDX;
916 op_data->op_fid1 = *fid;
917 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
918 mdt_index = op_data->op_mds;
919 OBD_FREE_PTR(op_data);
927 * Get MDT index for the inode.
929 int ll_get_mdt_idx(struct inode *inode)
931 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
935 * Generic handler to do any pre-copy work.
937 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
938 * first information for it that real work has started.
940 * Moreover, for a ARCHIVE request, it will sample the file data version and
941 * store it in \a copy.
943 * \return 0 on success.
945 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
947 struct ll_sb_info *sbi = ll_s2sbi(sb);
948 struct hsm_progress_kernel hpk;
953 /* Forge a hsm_progress based on data from copy. */
954 hpk.hpk_fid = copy->hc_hai.hai_fid;
955 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
956 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
957 hpk.hpk_extent.length = 0;
960 hpk.hpk_data_version = 0;
963 /* For archive request, we need to read the current file version. */
964 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
966 __u64 data_version = 0;
968 /* Get inode for this fid */
969 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
971 hpk.hpk_flags |= HP_FLAG_RETRY;
972 /* hpk_errval is >= 0 */
973 hpk.hpk_errval = -PTR_ERR(inode);
974 GOTO(progress, rc = PTR_ERR(inode));
977 /* Read current file data version */
978 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
981 CDEBUG(D_HSM, "Could not read file data version of "
982 DFID" (rc = %d). Archive request ("
983 "%#llx) could not be done.\n",
984 PFID(©->hc_hai.hai_fid), rc,
985 copy->hc_hai.hai_cookie);
986 hpk.hpk_flags |= HP_FLAG_RETRY;
987 /* hpk_errval must be >= 0 */
988 hpk.hpk_errval = -rc;
992 /* Store in the hsm_copy for later copytool use.
993 * Always modified even if no lsm. */
994 copy->hc_data_version = data_version;
998 /* On error, the request should be considered as completed */
999 if (hpk.hpk_errval > 0)
1000 hpk.hpk_flags |= HP_FLAG_COMPLETED;
1002 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1005 /* Return first error */
1006 RETURN(rc != 0 ? rc : rc2);
1010 * Generic handler to do any post-copy work.
1012 * It will send the last hsm_progress update to coordinator to inform it
1013 * that copy is finished and whether it was successful or not.
1016 * - for ARCHIVE request, it will sample the file data version and compare it
1017 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
1018 * will be considered as failed.
1019 * - for RESTORE request, it will sample the file data version and send it to
1020 * coordinator which is useful if the file was imported as 'released'.
1022 * \return 0 on success.
1024 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
1026 struct ll_sb_info *sbi = ll_s2sbi(sb);
1027 struct hsm_progress_kernel hpk;
1032 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
1033 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
1034 * initialized if copy_end was called with copy == NULL.
1037 /* Forge a hsm_progress based on data from copy. */
1038 hpk.hpk_fid = copy->hc_hai.hai_fid;
1039 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1040 hpk.hpk_extent = copy->hc_hai.hai_extent;
1041 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1042 hpk.hpk_errval = copy->hc_errval;
1043 hpk.hpk_data_version = 0;
1045 /* For archive request, we need to check the file data was not changed.
1047 * For restore request, we need to send the file data version, this is
1048 * useful when the file was created using hsm_import.
1050 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1051 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1052 (copy->hc_errval == 0)) {
1053 struct inode *inode;
1054 __u64 data_version = 0;
1056 /* Get lsm for this fid */
1057 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1058 if (IS_ERR(inode)) {
1059 hpk.hpk_flags |= HP_FLAG_RETRY;
1060 /* hpk_errval must be >= 0 */
1061 hpk.hpk_errval = -PTR_ERR(inode);
1062 GOTO(progress, rc = PTR_ERR(inode));
1065 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1069 "Could not read file data version. Request could not be confirmed.\n");
1070 if (hpk.hpk_errval == 0)
1071 hpk.hpk_errval = -rc;
1075 /* Store in the hsm_copy for later copytool use.
1076 * Always modified even if no lsm. */
1077 hpk.hpk_data_version = data_version;
1079 /* File could have been stripped during archiving, so we need
1080 * to check anyway. */
1081 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1082 (copy->hc_data_version != data_version)) {
1083 CDEBUG(D_HSM, "File data version mismatched. "
1084 "File content was changed during archiving. "
1085 DFID", start:%#llx current:%#llx\n",
1086 PFID(©->hc_hai.hai_fid),
1087 copy->hc_data_version, data_version);
1088 /* File was changed, send error to cdt. Do not ask for
1089 * retry because if a file is modified frequently,
1090 * the cdt will loop on retried archive requests.
1091 * The policy engine will ask for a new archive later
1092 * when the file will not be modified for some tunable
1094 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1096 /* hpk_errval must be >= 0 */
1097 hpk.hpk_errval = -rc;
1104 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1107 /* Return first error */
1108 RETURN(rc != 0 ? rc : rc2);
1112 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1113 const struct lustre_kernelcomm __user *data)
1115 struct lustre_kernelcomm *lk;
1116 struct lustre_kernelcomm *tmp;
1117 size_t size = sizeof(*lk);
1122 /* copy data from userspace to get numbers of archive_id */
1123 OBD_ALLOC(lk, size);
1127 if (copy_from_user(lk, data, size))
1128 GOTO(out_lk, rc = -EFAULT);
1130 if (lk->lk_flags & LK_FLG_STOP)
1133 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1134 __u32 archive_mask = lk->lk_data_count;
1137 /* old hsm agent to old MDS */
1138 if (!exp_connect_archive_id_array(exp))
1141 /* old hsm agent to new MDS */
1142 lk->lk_flags |= LK_FLG_DATANR;
1144 if (archive_mask == 0)
1147 count = hweight32(archive_mask);
1148 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1149 OBD_ALLOC(tmp, new_size);
1151 GOTO(out_lk, rc = -ENOMEM);
1153 memcpy(tmp, lk, size);
1154 tmp->lk_data_count = count;
1160 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1161 if (BIT(i) & archive_mask) {
1162 lk->lk_data[count] = i + 1;
1169 /* new hsm agent to new mds */
1170 if (lk->lk_data_count > 0) {
1171 new_size = offsetof(struct lustre_kernelcomm,
1172 lk_data[lk->lk_data_count]);
1173 OBD_ALLOC(tmp, new_size);
1175 GOTO(out_lk, rc = -ENOMEM);
1181 if (copy_from_user(lk, data, size))
1182 GOTO(out_lk, rc = -EFAULT);
1185 /* new hsm agent to old MDS */
1186 if (!exp_connect_archive_id_array(exp)) {
1189 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1190 GOTO(out_lk, rc = -EINVAL);
1192 for (i = 0; i < lk->lk_data_count; i++) {
1193 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1195 CERROR("%s: archive id %d requested but only [0 - %zu] supported: rc = %d\n",
1196 exp->exp_obd->obd_name, lk->lk_data[i],
1197 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1201 if (lk->lk_data[i] == 0) {
1206 archives |= (1 << (lk->lk_data[i] - 1));
1208 lk->lk_flags &= ~LK_FLG_DATANR;
1209 lk->lk_data_count = archives;
1212 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1218 static int check_owner(int type, int id)
1222 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1226 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1235 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1237 struct ll_sb_info *sbi = ll_s2sbi(sb);
1238 int cmd = qctl->qc_cmd;
1239 int type = qctl->qc_type;
1240 int id = qctl->qc_id;
1241 int valid = qctl->qc_valid;
1249 case LUSTRE_Q_SETDEFAULT:
1250 case LUSTRE_Q_SETQUOTAPOOL:
1251 case LUSTRE_Q_SETINFOPOOL:
1252 case LUSTRE_Q_SETDEFAULT_POOL:
1253 case LUSTRE_Q_DELETEQID:
1254 case LUSTRE_Q_RESETQID:
1255 if (!capable(CAP_SYS_ADMIN))
1258 if (sb->s_flags & SB_RDONLY)
1262 case LUSTRE_Q_GETDEFAULT:
1263 case LUSTRE_Q_GETQUOTAPOOL:
1264 case LUSTRE_Q_GETDEFAULT_POOL:
1265 if (check_owner(type, id) &&
1266 (!capable(CAP_SYS_ADMIN)))
1270 case LUSTRE_Q_GETINFOPOOL:
1273 CERROR("%s: unsupported quotactl op: %#x: rc = %d\n",
1274 sbi->ll_fsname, cmd, -EOPNOTSUPP);
1275 RETURN(-EOPNOTSUPP);
1278 if (valid != QC_GENERAL) {
1279 if (cmd == Q_GETINFO)
1280 qctl->qc_cmd = Q_GETOINFO;
1281 else if (cmd == Q_GETQUOTA ||
1282 cmd == LUSTRE_Q_GETQUOTAPOOL)
1283 qctl->qc_cmd = Q_GETOQUOTA;
1289 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1290 sizeof(*qctl), qctl, NULL);
1293 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1294 sizeof(*qctl), qctl, NULL);
1297 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1298 sizeof(*qctl), qctl, NULL);
1300 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1302 sizeof(*qctl), qctl, NULL);
1313 struct obd_quotactl *oqctl;
1314 int oqctl_len = sizeof(*oqctl);
1316 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1317 oqctl_len += LOV_MAXPOOLNAME + 1;
1319 OBD_ALLOC(oqctl, oqctl_len);
1323 QCTL_COPY(oqctl, qctl);
1324 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1326 OBD_FREE(oqctl, oqctl_len);
1329 /* If QIF_SPACE is not set, client should collect the
1330 * space usage from OSSs by itself
1332 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1333 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1334 !oqctl->qc_dqblk.dqb_curspace) {
1335 struct obd_quotactl *oqctl_tmp;
1336 int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1338 OBD_ALLOC(oqctl_tmp, qctl_len);
1339 if (oqctl_tmp == NULL)
1340 GOTO(out, rc = -ENOMEM);
1342 if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1343 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1344 memcpy(oqctl_tmp->qc_poolname,
1346 LOV_MAXPOOLNAME + 1);
1348 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1350 oqctl_tmp->qc_id = oqctl->qc_id;
1351 oqctl_tmp->qc_type = oqctl->qc_type;
1353 /* collect space usage from OSTs */
1354 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1355 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1356 if (!rc || rc == -EREMOTEIO) {
1357 oqctl->qc_dqblk.dqb_curspace =
1358 oqctl_tmp->qc_dqblk.dqb_curspace;
1359 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1362 /* collect space & inode usage from MDTs */
1363 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1364 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1365 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1366 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1367 if (!rc || rc == -EREMOTEIO) {
1368 oqctl->qc_dqblk.dqb_curspace +=
1369 oqctl_tmp->qc_dqblk.dqb_curspace;
1370 oqctl->qc_dqblk.dqb_curinodes =
1371 oqctl_tmp->qc_dqblk.dqb_curinodes;
1372 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1374 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1377 OBD_FREE(oqctl_tmp, qctl_len);
1380 QCTL_COPY(qctl, oqctl);
1381 OBD_FREE(oqctl, oqctl_len);
1387 static int ll_rmfid(struct file *file, void __user *arg)
1389 const struct fid_array __user *ufa = arg;
1390 struct inode *inode = file_inode(file);
1391 struct ll_sb_info *sbi = ll_i2sbi(inode);
1392 struct fid_array *lfa = NULL;
1395 int i, rc, *rcs = NULL;
1398 if (!capable(CAP_DAC_READ_SEARCH) &&
1399 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1401 /* Only need to get the buflen */
1402 if (get_user(nr, &ufa->fa_nr))
1404 /* DoS protection */
1405 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1408 size = offsetof(struct fid_array, fa_fids[nr]);
1409 OBD_ALLOC(lfa, size);
1412 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1414 GOTO(free_lfa, rc = -ENOMEM);
1416 if (copy_from_user(lfa, arg, size))
1417 GOTO(free_rcs, rc = -EFAULT);
1419 /* In case of subdirectory mount, we need to make sure all the files
1420 * for which we want to remove FID are visible in the namespace.
1422 if (!fid_is_root(&sbi->ll_root_fid)) {
1423 struct fid_array *lfa_new = NULL;
1424 int path_len = PATH_MAX, linkno;
1425 struct getinfo_fid2path *gf;
1426 int idx, last_idx = nr - 1;
1428 OBD_ALLOC(lfa_new, size);
1430 GOTO(free_rcs, rc = -ENOMEM);
1433 gf = kmalloc(sizeof(*gf) + path_len + 1, GFP_NOFS);
1435 GOTO(free_rcs, rc = -ENOMEM);
1437 for (idx = 0; idx < nr; idx++) {
1440 memset(gf, 0, sizeof(*gf) + path_len + 1);
1441 gf->gf_fid = lfa->fa_fids[idx];
1442 gf->gf_pathlen = path_len;
1443 gf->gf_linkno = linkno;
1444 rc = __ll_fid2path(inode, gf,
1445 sizeof(*gf) + gf->gf_pathlen,
1447 if (rc == -ENAMETOOLONG) {
1448 struct getinfo_fid2path *tmpgf;
1450 path_len += PATH_MAX;
1451 tmpgf = krealloc(gf,
1452 sizeof(*gf) + path_len + 1,
1456 OBD_FREE(lfa_new, size);
1457 GOTO(free_rcs, rc = -ENOMEM);
1464 if (gf->gf_linkno == linkno)
1466 linkno = gf->gf_linkno;
1470 /* All the links for this fid are visible in the
1471 * mounted subdir. So add it to the list of fids
1474 lfa_new->fa_fids[lfa_new->fa_nr++] =
1477 /* At least one link for this fid is not visible
1478 * in the mounted subdir. So add it at the end
1479 * of the list that will be hidden to lower
1480 * layers, and set -ENOENT as ret code.
1482 lfa_new->fa_fids[last_idx] = lfa->fa_fids[idx];
1483 rcs[last_idx--] = rc;
1487 OBD_FREE(lfa, size);
1491 if (lfa->fa_nr == 0)
1492 GOTO(free_rcs, rc = rcs[nr - 1]);
1494 /* Call mdc_iocontrol */
1495 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1498 for (i = 0; i < nr; i++)
1500 lfa->fa_fids[i].f_ver = rcs[i];
1501 if (copy_to_user(arg, lfa, size))
1506 OBD_FREE_PTR_ARRAY(rcs, nr);
1508 OBD_FREE(lfa, size);
1513 /* This function tries to get a single name component,
1514 * to send to the server. No actual path traversal involved,
1515 * so we limit to NAME_MAX */
1516 static char *ll_getname(const char __user *filename)
1521 OBD_ALLOC(tmp, NAME_MAX + 1);
1524 return ERR_PTR(-ENOMEM);
1526 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1529 else if (len > NAME_MAX)
1530 ret = -ENAMETOOLONG;
1533 OBD_FREE(tmp, NAME_MAX + 1);
1539 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1541 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1543 struct dentry *dentry = file_dentry(file);
1544 struct inode *inode = file_inode(file);
1545 struct ll_sb_info *sbi = ll_i2sbi(inode);
1546 struct obd_ioctl_data *data = NULL;
1547 void __user *uarg = (void __user *)arg;
1551 CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
1552 PFID(ll_inode2fid(inode)), inode, cmd, arg);
1554 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1555 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1558 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1560 case IOC_MDC_LOOKUP: {
1561 int namelen, len = 0;
1564 rc = obd_ioctl_getdata(&data, &len, uarg);
1568 filename = data->ioc_inlbuf1;
1569 namelen = strlen(filename);
1571 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1572 GOTO(out_free, rc = -EINVAL);
1575 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1577 CERROR("%s: lookup %.*s failed: rc = %d\n",
1578 sbi->ll_fsname, namelen, filename, rc);
1582 OBD_FREE_LARGE(data, len);
1585 case LL_IOC_LMV_SETSTRIPE: {
1586 struct lmv_user_md *lum;
1591 bool createonly = false;
1595 rc = obd_ioctl_getdata(&data, &len, uarg);
1599 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1600 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1601 GOTO(lmv_out_free, rc = -EINVAL);
1603 filename = data->ioc_inlbuf1;
1604 namelen = data->ioc_inllen1;
1607 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1608 GOTO(lmv_out_free, rc = -EINVAL);
1610 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1611 lumlen = data->ioc_inllen2;
1613 if (!lmv_user_magic_supported(lum->lum_magic)) {
1614 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1615 lum->lum_magic, -EINVAL);
1616 GOTO(lmv_out_free, rc = -EINVAL);
1619 if ((lum->lum_magic == LMV_USER_MAGIC ||
1620 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1621 lumlen < sizeof(*lum)) {
1622 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1623 filename, lumlen, lum->lum_magic, -EINVAL);
1624 GOTO(lmv_out_free, rc = -EINVAL);
1627 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1628 lumlen < sizeof(struct lmv_foreign_md)) {
1629 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1630 filename, lum->lum_magic, lumlen, -EFAULT);
1631 GOTO(lmv_out_free, rc = -EINVAL);
1634 mode = data->ioc_type;
1635 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1636 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1639 OBD_FREE_LARGE(data, len);
1643 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1644 struct lmv_user_md lum;
1645 struct lmv_user_md __user *ulump = uarg;
1648 if (copy_from_user(&lum, ulump, sizeof(lum)))
1651 if (lum.lum_magic != LMV_USER_MAGIC)
1654 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1658 case LL_IOC_LOV_SETSTRIPE_NEW:
1659 case LL_IOC_LOV_SETSTRIPE: {
1660 struct lov_user_md_v3 *lumv3 = NULL;
1661 struct lov_user_md_v1 lumv1;
1662 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1663 struct lov_user_md_v1 __user *lumv1p = uarg;
1664 struct lov_user_md_v3 __user *lumv3p = uarg;
1666 int set_default = 0;
1668 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1669 sizeof(struct lov_comp_md_v1));
1670 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1671 /* first try with v1 which is smaller than v3 */
1672 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1675 if (is_root_inode(inode))
1678 switch (lumv1.lmm_magic) {
1679 case LOV_USER_MAGIC_V3:
1680 case LOV_USER_MAGIC_SPECIFIC:
1681 lum_size = ll_lov_user_md_size(&lumv1);
1684 OBD_ALLOC(lumv3, lum_size);
1687 if (copy_from_user(lumv3, lumv3p, lum_size))
1688 GOTO(out, rc = -EFAULT);
1689 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1691 case LOV_USER_MAGIC_V1:
1694 GOTO(out, rc = -EOPNOTSUPP);
1697 /* in v1 and v3 cases lumv1 points to data */
1698 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1701 OBD_FREE(lumv3, lum_size);
1704 case LL_IOC_LMV_GETSTRIPE: {
1705 struct lmv_user_md __user *ulmv = uarg;
1706 struct lmv_user_md lum;
1707 struct ptlrpc_request *request = NULL;
1708 union lmv_mds_md *lmm = NULL;
1711 struct lmv_user_md *tmp = NULL;
1715 int max_stripe_count;
1719 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1722 /* get default LMV */
1723 if (lum.lum_magic == LMV_USER_MAGIC &&
1724 lum.lum_type != LMV_TYPE_RAW) {
1725 rc = ll_dir_get_default_lmv(inode, &lum);
1729 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1735 max_stripe_count = lum.lum_stripe_count;
1736 /* lum_magic will indicate which stripe the ioctl will like
1737 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1738 * is for default LMV stripe */
1739 if (lum.lum_magic == LMV_MAGIC_V1)
1740 valid |= OBD_MD_MEA;
1741 else if (lum.lum_magic == LMV_USER_MAGIC)
1742 valid |= OBD_MD_DEFAULT_MEA;
1746 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1747 &request, NULL, valid);
1749 GOTO(finish_req, rc);
1751 /* get default LMV in raw mode */
1752 if (lum.lum_magic == LMV_USER_MAGIC) {
1753 if (copy_to_user(ulmv, lmm, lmmsize))
1754 GOTO(finish_req, rc = -EFAULT);
1755 GOTO(finish_req, rc);
1758 /* if foreign LMV case, fake stripes number */
1759 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1760 struct lmv_foreign_md *lfm;
1762 lfm = (struct lmv_foreign_md *)lmm;
1763 if (lfm->lfm_length < XATTR_SIZE_MAX -
1764 offsetof(typeof(*lfm), lfm_value)) {
1765 __u32 size = lfm->lfm_length +
1766 offsetof(typeof(*lfm), lfm_value);
1768 stripe_count = lmv_foreign_to_md_stripes(size);
1770 CERROR("%s: invalid %d foreign size returned: rc = %d\n",
1771 sbi->ll_fsname, lfm->lfm_length,
1776 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1778 if (max_stripe_count < stripe_count) {
1779 lum.lum_stripe_count = stripe_count;
1780 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1781 GOTO(finish_req, rc = -EFAULT);
1782 GOTO(finish_req, rc = -E2BIG);
1785 /* enough room on user side and foreign case */
1786 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1787 struct lmv_foreign_md *lfm;
1790 lfm = (struct lmv_foreign_md *)lmm;
1791 size = lfm->lfm_length +
1792 offsetof(struct lmv_foreign_md, lfm_value);
1793 if (copy_to_user(ulmv, lfm, size))
1794 GOTO(finish_req, rc = -EFAULT);
1795 GOTO(finish_req, rc);
1798 lum_size = lmv_user_md_size(stripe_count,
1799 LMV_USER_MAGIC_SPECIFIC);
1800 OBD_ALLOC(tmp, lum_size);
1802 GOTO(finish_req, rc = -ENOMEM);
1804 mdt_index = ll_get_mdt_idx(inode);
1806 GOTO(out_tmp, rc = -ENOMEM);
1808 tmp->lum_magic = LMV_MAGIC_V1;
1809 tmp->lum_stripe_count = 0;
1810 tmp->lum_stripe_offset = mdt_index;
1811 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1812 for (i = 0; i < stripe_count; i++) {
1815 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1816 if (fid_is_sane(&fid)) {
1817 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1819 GOTO(out_tmp, rc = mdt_index);
1821 tmp->lum_objects[i].lum_mds = mdt_index;
1822 tmp->lum_objects[i].lum_fid = fid;
1825 tmp->lum_stripe_count++;
1828 if (copy_to_user(ulmv, tmp, lum_size))
1829 GOTO(out_tmp, rc = -EFAULT);
1831 OBD_FREE(tmp, lum_size);
1833 ptlrpc_req_finished(request);
1836 case LL_IOC_REMOVE_ENTRY: {
1837 char *filename = NULL;
1841 /* Here is a little hack to avoid sending REINT_RMENTRY to
1842 * unsupported server, which might crash the server(LU-2730),
1843 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1844 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1845 * server will support REINT_RMENTRY XXX*/
1846 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1847 RETURN(-EOPNOTSUPP);
1849 filename = ll_getname(uarg);
1850 if (IS_ERR(filename))
1851 RETURN(PTR_ERR(filename));
1853 namelen = strlen(filename);
1855 GOTO(out_rmdir, rc = -EINVAL);
1857 rc = ll_rmdir_entry(inode, filename, namelen);
1860 ll_putname(filename);
1864 RETURN(ll_rmfid(file, uarg));
1865 case LL_IOC_LOV_SWAP_LAYOUTS:
1867 case LL_IOC_LOV_GETSTRIPE:
1868 case LL_IOC_LOV_GETSTRIPE_NEW:
1869 case LL_IOC_MDC_GETINFO_V1:
1870 case LL_IOC_MDC_GETINFO_V2:
1871 case IOC_MDC_GETFILEINFO_V1:
1872 case IOC_MDC_GETFILEINFO_V2:
1873 case IOC_MDC_GETFILESTRIPE: {
1874 struct ptlrpc_request *request = NULL;
1875 struct ptlrpc_request *root_request = NULL;
1876 struct lov_user_md __user *lump;
1877 struct lov_mds_md *lmm = NULL;
1878 struct mdt_body *body;
1879 char *filename = NULL;
1880 lstat_t __user *statp = NULL;
1881 lstatx_t __user *stxp = NULL;
1882 __u64 __user *flagsp = NULL;
1883 __u32 __user *lmmsizep = NULL;
1884 struct lu_fid __user *fidp = NULL;
1888 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1889 cmd == IOC_MDC_GETFILEINFO_V2 ||
1890 cmd == IOC_MDC_GETFILESTRIPE) {
1891 filename = ll_getname(uarg);
1892 if (IS_ERR(filename))
1893 RETURN(PTR_ERR(filename));
1895 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1896 &lmmsize, &request);
1898 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1904 body = req_capsule_server_get(&request->rq_pill,
1906 LASSERT(body != NULL);
1911 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1912 cmd == LL_IOC_MDC_GETINFO_V1 ||
1913 cmd == IOC_MDC_GETFILEINFO_V2 ||
1914 cmd == LL_IOC_MDC_GETINFO_V2)) {
1922 if (cmd == IOC_MDC_GETFILESTRIPE ||
1923 cmd == LL_IOC_LOV_GETSTRIPE ||
1924 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1926 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1927 cmd == LL_IOC_MDC_GETINFO_V1){
1928 struct lov_user_mds_data_v1 __user *lmdp;
1931 statp = &lmdp->lmd_st;
1932 lump = &lmdp->lmd_lmm;
1934 struct lov_user_mds_data __user *lmdp;
1937 fidp = &lmdp->lmd_fid;
1938 stxp = &lmdp->lmd_stx;
1939 flagsp = &lmdp->lmd_flags;
1940 lmmsizep = &lmdp->lmd_lmmsize;
1941 lump = &lmdp->lmd_lmm;
1945 /* If the file has no striping then zero out *lump so
1946 * that the caller isn't confused by garbage. */
1947 if (clear_user(lump, sizeof(*lump)))
1948 GOTO(out_req, rc = -EFAULT);
1949 } else if (copy_to_user(lump, lmm, lmmsize)) {
1950 if (copy_to_user(lump, lmm, sizeof(*lump)))
1951 GOTO(out_req, rc = -EFAULT);
1954 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1956 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1957 cmd == LL_IOC_MDC_GETINFO_V1) {
1960 st.st_dev = inode->i_sb->s_dev;
1961 st.st_mode = body->mbo_mode;
1962 st.st_nlink = body->mbo_nlink;
1963 st.st_uid = body->mbo_uid;
1964 st.st_gid = body->mbo_gid;
1965 st.st_rdev = body->mbo_rdev;
1966 if (llcrypt_require_key(inode) == -ENOKEY)
1967 st.st_size = round_up(st.st_size,
1968 LUSTRE_ENCRYPTION_UNIT_SIZE);
1970 st.st_size = body->mbo_size;
1971 st.st_blksize = PAGE_SIZE;
1972 st.st_blocks = body->mbo_blocks;
1973 st.st_atime = body->mbo_atime;
1974 st.st_mtime = body->mbo_mtime;
1975 st.st_ctime = body->mbo_ctime;
1976 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1979 if (copy_to_user(statp, &st, sizeof(st)))
1980 GOTO(out_req, rc = -EFAULT);
1981 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1982 cmd == LL_IOC_MDC_GETINFO_V2) {
1983 lstatx_t stx = { 0 };
1984 __u64 valid = body->mbo_valid;
1986 stx.stx_blksize = PAGE_SIZE;
1987 stx.stx_nlink = body->mbo_nlink;
1988 stx.stx_uid = body->mbo_uid;
1989 stx.stx_gid = body->mbo_gid;
1990 stx.stx_mode = body->mbo_mode;
1991 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1993 if (llcrypt_require_key(inode) == -ENOKEY)
1994 stx.stx_size = round_up(stx.stx_size,
1995 LUSTRE_ENCRYPTION_UNIT_SIZE);
1997 stx.stx_size = body->mbo_size;
1998 stx.stx_blocks = body->mbo_blocks;
1999 stx.stx_atime.tv_sec = body->mbo_atime;
2000 stx.stx_ctime.tv_sec = body->mbo_ctime;
2001 stx.stx_mtime.tv_sec = body->mbo_mtime;
2002 stx.stx_btime.tv_sec = body->mbo_btime;
2003 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
2004 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
2005 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
2006 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
2007 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
2009 /* For a striped directory, the size and blocks returned
2010 * from MDT is not correct.
2011 * The size and blocks are aggregated by client across
2013 * Thus for a striped directory, do not return the valid
2014 * FLSIZE and FLBLOCKS flags to the caller.
2015 * However, this whould be better decided by the MDS
2016 * instead of the client.
2018 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
2019 ll_i2info(inode)->lli_lsm_md != NULL)
2020 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
2022 if (flagsp && copy_to_user(flagsp, &valid,
2024 GOTO(out_req, rc = -EFAULT);
2026 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
2028 GOTO(out_req, rc = -EFAULT);
2030 if (!(valid & OBD_MD_FLSIZE))
2031 stx.stx_mask &= ~STATX_SIZE;
2032 if (!(valid & OBD_MD_FLBLOCKS))
2033 stx.stx_mask &= ~STATX_BLOCKS;
2035 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
2036 GOTO(out_req, rc = -EFAULT);
2038 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2040 GOTO(out_req, rc = -EFAULT);
2045 ptlrpc_req_finished(request);
2046 ptlrpc_req_finished(root_request);
2048 ll_putname(filename);
2051 case OBD_IOC_QUOTACTL: {
2052 struct if_quotactl *qctl;
2053 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2055 OBD_ALLOC(qctl, qctl_len);
2059 if (copy_from_user(qctl, uarg, sizeof(*qctl)))
2060 GOTO(out_quotactl, rc = -EFAULT);
2062 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2063 char __user *from = uarg +
2064 offsetof(typeof(*qctl), qc_poolname);
2065 if (copy_from_user(qctl->qc_poolname, from,
2066 LOV_MAXPOOLNAME + 1))
2067 GOTO(out_quotactl, rc = -EFAULT);
2070 rc = quotactl_ioctl(inode->i_sb, qctl);
2071 if ((rc == 0 || rc == -ENODATA) &&
2072 copy_to_user(uarg, qctl, sizeof(*qctl)))
2075 OBD_FREE(qctl, qctl_len);
2078 case LL_IOC_GETOBDCOUNT: {
2080 struct obd_export *exp;
2082 if (copy_from_user(&count, uarg, sizeof(count)))
2085 /* get ost count when count is zero, get mdt count otherwise */
2086 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2087 vallen = sizeof(count);
2088 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2089 KEY_TGT_COUNT, &vallen, &count);
2091 CERROR("%s: get target count failed: rc = %d\n",
2092 sbi->ll_fsname, rc);
2096 if (copy_to_user(uarg, &count, sizeof(count)))
2101 case LL_IOC_GET_CONNECT_FLAGS:
2102 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, uarg));
2103 case LL_IOC_FID2MDTIDX: {
2104 struct obd_export *exp = ll_i2mdexp(inode);
2108 if (copy_from_user(&fid, uarg, sizeof(fid)))
2111 /* Call mdc_iocontrol */
2112 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2113 (__u32 __user *)&index);
2119 case LL_IOC_HSM_REQUEST: {
2120 struct hsm_user_request *hur;
2127 /* We don't know the true size yet; copy the fixed-size part */
2128 if (copy_from_user(hur, uarg, sizeof(*hur))) {
2133 /* Compute the whole struct size */
2134 totalsize = hur_len(hur);
2139 /* Final size will be more than double totalsize */
2140 if (totalsize >= MDS_MAXREQSIZE / 3)
2143 OBD_ALLOC_LARGE(hur, totalsize);
2147 /* Copy the whole struct */
2148 if (copy_from_user(hur, uarg, totalsize))
2149 GOTO(out_hur, rc = -EFAULT);
2151 if (hur->hur_request.hr_action == HUA_RELEASE) {
2152 const struct lu_fid *fid;
2156 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2157 fid = &hur->hur_user_item[i].hui_fid;
2158 f = search_inode_for_lustre(inode->i_sb, fid);
2164 rc = ll_hsm_release(f);
2170 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2174 OBD_FREE_LARGE(hur, totalsize);
2178 case LL_IOC_HSM_PROGRESS: {
2179 struct hsm_progress_kernel hpk;
2180 struct hsm_progress hp;
2182 if (copy_from_user(&hp, uarg, sizeof(hp)))
2185 hpk.hpk_fid = hp.hp_fid;
2186 hpk.hpk_cookie = hp.hp_cookie;
2187 hpk.hpk_extent = hp.hp_extent;
2188 hpk.hpk_flags = hp.hp_flags;
2189 hpk.hpk_errval = hp.hp_errval;
2190 hpk.hpk_data_version = 0;
2192 /* File may not exist in Lustre; all progress
2193 * reported to Lustre root */
2194 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2198 case LL_IOC_HSM_CT_START:
2199 if (!capable(CAP_SYS_ADMIN))
2202 rc = copy_and_ct_start(cmd, sbi->ll_md_exp, uarg);
2205 case LL_IOC_HSM_COPY_START: {
2206 struct hsm_copy *copy;
2209 OBD_ALLOC_PTR(copy);
2212 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2217 rc = ll_ioc_copy_start(inode->i_sb, copy);
2218 if (copy_to_user(uarg, copy, sizeof(*copy)))
2224 case LL_IOC_HSM_COPY_END: {
2225 struct hsm_copy *copy;
2228 OBD_ALLOC_PTR(copy);
2231 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2236 rc = ll_ioc_copy_end(inode->i_sb, copy);
2237 if (copy_to_user(uarg, copy, sizeof(*copy)))
2243 case LL_IOC_MIGRATE: {
2244 struct lmv_user_md *lum;
2251 rc = obd_ioctl_getdata(&data, &len, uarg);
2255 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2256 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2257 GOTO(migrate_free, rc = -EINVAL);
2259 filename = data->ioc_inlbuf1;
2260 namelen = data->ioc_inllen1;
2261 flags = data->ioc_type;
2263 if (namelen < 1 || namelen != strlen(filename) + 1) {
2264 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2265 GOTO(migrate_free, rc = -EINVAL);
2268 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2269 if (lum->lum_magic != LMV_USER_MAGIC &&
2270 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2272 CERROR("%s: wrong lum magic %x: rc = %d\n",
2273 filename, lum->lum_magic, rc);
2274 GOTO(migrate_free, rc);
2277 rc = ll_migrate(inode, file, lum, filename, flags);
2279 OBD_FREE_LARGE(data, len);
2283 case LL_IOC_PCC_DETACH_BY_FID: {
2284 struct lu_pcc_detach_fid *detach;
2286 struct inode *inode2;
2289 OBD_ALLOC_PTR(detach);
2293 if (copy_from_user(detach, uarg, sizeof(*detach)))
2294 GOTO(out_detach, rc = -EFAULT);
2296 fid = &detach->pccd_fid;
2297 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2298 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2300 /* Target inode is not in inode cache, and PCC file
2301 * has aleady released, return immdiately.
2303 GOTO(out_detach, rc = 0);
2305 if (!S_ISREG(inode2->i_mode))
2306 GOTO(out_iput, rc = -EINVAL);
2308 if (!inode_owner_or_capable(&init_user_ns, inode2))
2309 GOTO(out_iput, rc = -EPERM);
2311 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2315 OBD_FREE_PTR(detach);
2319 rc = ll_iocontrol(inode, file, cmd, uarg);
2322 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL, uarg));
2326 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2328 struct inode *inode = file->f_mapping->host;
2329 struct ll_file_data *fd = file->private_data;
2330 struct ll_sb_info *sbi = ll_i2sbi(inode);
2331 int api32 = ll_need_32bit_api(sbi);
2332 loff_t ret = -EINVAL;
2335 ll_inode_lock(inode);
2340 offset += file->f_pos;
2346 offset += LL_DIR_END_OFF_32BIT;
2348 offset += LL_DIR_END_OFF;
2355 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2356 (!api32 && offset <= LL_DIR_END_OFF))) {
2357 if (offset != file->f_pos) {
2360 hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2361 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2362 (!api32 && offset == LL_DIR_END_OFF))
2363 fd->lfd_pos = MDS_DIR_END_OFF;
2364 else if (api32 && hash64)
2365 fd->lfd_pos = offset << 32;
2367 fd->lfd_pos = offset;
2368 file->f_pos = offset;
2369 file->f_version = 0;
2376 ll_inode_unlock(inode);
2380 static int ll_dir_open(struct inode *inode, struct file *file)
2383 RETURN(ll_file_open(inode, file));
2386 static int ll_dir_release(struct inode *inode, struct file *file)
2389 RETURN(ll_file_release(inode, file));
2392 /* notify error if partially read striped directory */
2393 static int ll_dir_flush(struct file *file, fl_owner_t id)
2395 struct ll_file_data *lfd = file->private_data;
2396 int rc = lfd->fd_partial_readdir_rc;
2398 lfd->fd_partial_readdir_rc = 0;
2403 const struct file_operations ll_dir_operations = {
2404 .llseek = ll_dir_seek,
2405 .open = ll_dir_open,
2406 .release = ll_dir_release,
2407 .read = generic_read_dir,
2408 #ifdef HAVE_DIR_CONTEXT
2409 .iterate_shared = ll_iterate,
2411 .readdir = ll_readdir,
2413 .unlocked_ioctl = ll_dir_ioctl,
2415 .flush = ll_dir_flush,