4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Directory code for lustre client.
37 #include <linux/pagemap.h>
39 #include <linux/version.h>
40 #include <linux/security.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43 #include <linux/uaccess.h>
44 #include <linux/buffer_head.h> // for wait_on_buffer
45 #include <linux/pagevec.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_lib.h>
53 #include <lustre_dlm.h>
54 #include <lustre_compat.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
58 #include <libcfs/libcfs_crypto.h>
60 #include "llite_internal.h"
63 * (new) readdir implementation overview.
65 * Original lustre readdir implementation cached exact copy of raw directory
66 * pages on the client. These pages were indexed in client page cache by
67 * logical offset in the directory file. This design, while very simple and
68 * intuitive had some inherent problems:
70 * . it implies that byte offset to the directory entry serves as a
71 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72 * ext3/htree directory entries may move due to splits, and more
75 * . it is incompatible with the design of split directories for cmd3,
76 * that assumes that names are distributed across nodes based on their
77 * hash, and so readdir should be done in hash order.
79 * New readdir implementation does readdir in hash order, and uses hash of a
80 * file name as a telldir/seekdir cookie. This led to number of complications:
82 * . hash is not unique, so it cannot be used to index cached directory
83 * pages on the client (note, that it requires a whole pageful of hash
84 * collided entries to cause two pages to have identical hashes);
86 * . hash is not unique, so it cannot, strictly speaking, be used as an
87 * entry cookie. ext3/htree has the same problem and lustre implementation
88 * mimics their solution: seekdir(hash) positions directory at the first
89 * entry with the given hash.
95 * Client caches directory pages using hash of the first entry as an index. As
96 * noted above hash is not unique, so this solution doesn't work as is:
97 * special processing is needed for "page hash chains" (i.e., sequences of
98 * pages filled with entries all having the same hash value).
100 * First, such chains have to be detected. To this end, server returns to the
101 * client the hash of the first entry on the page next to one returned. When
102 * client detects that this hash is the same as hash of the first entry on the
103 * returned page, page hash collision has to be handled. Pages in the
104 * hash chain, except first one, are termed "overflow pages".
106 * Proposed (unimplimented) solution to index uniqueness problem is to
107 * not cache overflow pages. Instead, when page hash collision is
108 * detected, all overflow pages from emerging chain should be
109 * immediately requested from the server and placed in a special data
110 * structure. This data structure can be used by ll_readdir() to
111 * process entries from overflow pages. When readdir invocation
112 * finishes, overflow pages are discarded. If page hash collision chain
113 * weren't completely processed, next call to readdir will again detect
114 * page hash collision, again read overflow pages in, process next
115 * portion of entries and again discard the pages. This is not as
116 * wasteful as it looks, because, given reasonable hash, page hash
117 * collisions are extremely rare.
119 * 1. directory positioning
121 * When seekdir(hash) is called.
123 * seekdir() sets the location in the directory stream from which the next
124 * readdir() call will start. mdc_page_locate() is used to find page with
125 * starting hash and will issue RPC to fetch that page. If there is a hash
126 * collision the concerned page is removed.
131 * identification of and access to overflow pages
135 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
136 * a header lu_dirpage which describes the start/end hash, and whether this
137 * page is empty (contains no dir entry) or hash collide with next page.
138 * After client receives reply, several pages will be integrated into dir page
139 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
140 * lu_dirpage for this integrated page will be adjusted. See
141 * mdc_adjust_dirpages().
144 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
145 __u64 offset, int *partial_readdir_rc)
147 struct md_readdir_info mrinfo = {
148 .mr_blocking_ast = ll_md_blocking_ast };
152 rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
156 if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
157 *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
162 void ll_release_page(struct inode *inode, struct page *page,
167 /* Always remove the page for striped dir, because the page is
168 * built from temporarily in LMV layer */
169 if (inode && ll_dir_striped(inode)) {
176 if (likely(page->mapping != NULL))
177 cfs_delete_from_page_cache(page);
183 #ifdef HAVE_DIR_CONTEXT
184 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
185 struct dir_context *ctx, int *partial_readdir_rc)
188 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
189 void *cookie, filldir_t filldir, int *partial_readdir_rc)
192 struct ll_sb_info *sbi = ll_i2sbi(inode);
194 bool is_api32 = ll_need_32bit_api(sbi);
195 bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
198 struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
202 if (IS_ENCRYPTED(inode)) {
203 rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
208 page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
210 while (rc == 0 && !done) {
211 struct lu_dirpage *dp;
212 struct lu_dirent *ent;
221 hash = MDS_DIR_END_OFF;
222 dp = page_address(page);
223 for (ent = lu_dirent_start(dp); ent != NULL && !done;
224 ent = lu_dirent_next(ent)) {
231 hash = le64_to_cpu(ent->lde_hash);
232 if (hash < pos) /* Skip until we find target hash */
235 namelen = le16_to_cpu(ent->lde_namelen);
236 if (namelen == 0) /* Skip dummy record */
239 if (is_api32 && is_hash64)
243 fid_le_to_cpu(&fid, &ent->lde_fid);
244 ino = cl_fid_build_ino(&fid, is_api32);
245 type = S_DT(lu_dirent_type_get(ent));
246 /* For ll_nfs_get_name_filldir(), it will try to access
247 * 'ent' through 'lde_name', so the parameter 'name'
248 * for 'filldir()' must be part of the 'ent'. */
249 #ifdef HAVE_DIR_CONTEXT
251 if (!IS_ENCRYPTED(inode)) {
252 done = !dir_emit(ctx, ent->lde_name, namelen,
255 /* Directory is encrypted */
256 int save_len = lltr.len;
257 struct llcrypt_str de_name =
258 LLTR_INIT(ent->lde_name, namelen);
260 rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
268 done = !dir_emit(ctx, de_name.name, de_name.len,
272 /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
273 * IS_ENCRYPTED is brought by kernel 4.14.
274 * So there is no need to handle encryption case here.
276 done = filldir(cookie, ent->lde_name, namelen, lhash,
283 ll_release_page(inode, page, false);
287 next = le64_to_cpu(dp->ldp_hash_end);
289 if (pos == MDS_DIR_END_OFF) {
290 /* End of directory reached. */
292 ll_release_page(inode, page, false);
294 /* Normal case: continue to the next page.*/
295 ll_release_page(inode, page,
296 le32_to_cpu(dp->ldp_flags) &
299 page = ll_get_dir_page(inode, op_data, pos,
303 #ifdef HAVE_DIR_CONTEXT
308 llcrypt_fname_free_buffer(&lltr);
312 #ifdef HAVE_DIR_CONTEXT
313 static int ll_iterate(struct file *filp, struct dir_context *ctx)
315 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
318 struct inode *inode = file_inode(filp);
319 struct ll_file_data *lfd = filp->private_data;
320 struct ll_sb_info *sbi = ll_i2sbi(inode);
321 bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
322 int api32 = ll_need_32bit_api(sbi);
323 struct md_op_data *op_data;
324 struct lu_fid pfid = { 0 };
325 ktime_t kstart = ktime_get();
326 /* result of possible partial readdir */
327 int partial_readdir_rc = 0;
333 LASSERT(lfd != NULL);
337 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
338 PFID(ll_inode2fid(inode)),
339 inode, (unsigned long)pos, i_size_read(inode), api32);
341 if (IS_ENCRYPTED(inode)) {
342 rc = llcrypt_prepare_readdir(inode);
343 if (rc && rc != -ENOKEY)
347 if (pos == MDS_DIR_END_OFF)
351 if (unlikely(ll_dir_striped(inode))) {
352 /* Only needed for striped dir to fill ..see lmv_read_page() */
353 if (file_dentry(filp)->d_parent != NULL &&
354 file_dentry(filp)->d_parent->d_inode != NULL) {
355 __u64 ibits = MDS_INODELOCK_LOOKUP;
356 struct inode *parent =
357 file_dentry(filp)->d_parent->d_inode;
359 if (ll_have_md_lock(ll_i2mdexp(parent), parent, &ibits,
361 pfid = *ll_inode2fid(parent);
364 /* If it can not find in cache, do lookup .. on the master
366 if (fid_is_zero(&pfid)) {
367 rc = ll_dir_get_parent_fid(inode, &pfid);
373 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
374 LUSTRE_OPC_ANY, inode);
376 GOTO(out, rc = PTR_ERR(op_data));
378 /* foreign dirs are browsed out of Lustre */
379 if (unlikely(lmv_dir_foreign(op_data->op_lso1))) {
380 ll_finish_md_op_data(op_data);
384 op_data->op_fid3 = pfid;
386 #ifdef HAVE_DIR_CONTEXT
388 rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
391 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
392 &partial_readdir_rc);
395 if (!lfd->fd_partial_readdir_rc)
396 lfd->fd_partial_readdir_rc = partial_readdir_rc;
398 if (pos == MDS_DIR_END_OFF) {
400 pos = LL_DIR_END_OFF_32BIT;
402 pos = LL_DIR_END_OFF;
407 #ifdef HAVE_DIR_CONTEXT
412 ll_finish_md_op_data(op_data);
416 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
417 ktime_us_delta(ktime_get(), kstart));
423 * Create striped directory with specified stripe(@lump)
425 * \param[in] dparent the parent of the directory.
426 * \param[in] lump the specified stripes.
427 * \param[in] dirname the name of the directory.
428 * \param[in] mode the specified mode of the directory.
430 * \retval =0 if striped directory is being created successfully.
431 * <0 if the creation is failed.
433 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
434 size_t len, const char *dirname, umode_t mode,
437 struct inode *parent = dparent->d_inode;
438 struct ptlrpc_request *request = NULL;
439 struct md_op_data *op_data;
440 struct ll_sb_info *sbi = ll_i2sbi(parent);
441 struct inode *inode = NULL;
442 struct dentry dentry = {
446 .len = strlen(dirname),
447 .hash = ll_full_name_hash(dparent, dirname,
450 .d_sb = dparent->d_sb,
452 bool encrypt = false;
457 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
460 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
462 "VFS Op:inode="DFID"(%p) name=%s stripe_offset=%d stripe_count=%u, hash_type=%x\n",
463 PFID(ll_inode2fid(parent)), parent, dirname,
464 (int)lump->lum_stripe_offset, lump->lum_stripe_count,
465 lump->lum_hash_type);
467 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
470 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
471 PFID(ll_inode2fid(parent)), parent, dirname,
472 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
475 if (lump->lum_stripe_count > 1 &&
476 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
479 if (IS_DEADDIR(parent) &&
480 !CFS_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
483 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
484 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
486 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE)) {
487 lump->lum_hash_type = cfs_fail_val;
488 } else if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
489 enum lmv_hash_type type = lump->lum_hash_type &
492 if (type >= LMV_HASH_TYPE_CRUSH ||
493 type == LMV_HASH_TYPE_UNKNOWN)
494 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
495 LMV_HASH_TYPE_FNV_1A_64;
498 hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
499 if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
502 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
503 lustre_swab_lmv_user_md(lump);
505 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
506 mode &= ~current_umask();
507 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
508 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
509 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
512 RETURN(PTR_ERR(op_data));
514 op_data->op_dir_depth = ll_i2info(parent)->lli_inherit_depth ?:
515 ll_i2info(parent)->lli_dir_depth;
517 if (ll_sbi_has_encrypt(sbi) &&
518 (IS_ENCRYPTED(parent) ||
519 unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
520 err = llcrypt_prepare_readdir(parent);
522 GOTO(out_op_data, err);
523 if (!llcrypt_has_encryption_key(parent))
524 GOTO(out_op_data, err = -ENOKEY);
528 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
529 /* selinux_dentry_init_security() uses dentry->d_parent and name
530 * to determine the security context for the file. So our fake
531 * dentry should be real enough for this purpose. */
532 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
533 &op_data->op_file_secctx_name,
534 &op_data->op_file_secctx_name_size,
535 &op_data->op_file_secctx,
536 &op_data->op_file_secctx_size,
537 &op_data->op_file_secctx_slot);
539 GOTO(out_op_data, err);
543 err = llcrypt_inherit_context(parent, NULL, op_data, false);
545 GOTO(out_op_data, err);
548 op_data->op_cli_flags |= CLI_SET_MEA;
550 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
552 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
553 from_kuid(&init_user_ns, current_fsuid()),
554 from_kgid(&init_user_ns, current_fsgid()),
555 current_cap(), 0, &request);
557 GOTO(out_request, err);
559 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
561 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
563 GOTO(out_inode, err);
565 dentry.d_inode = inode;
567 if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags))
568 err = ll_inode_notifysecctx(inode, op_data->op_file_secctx,
569 op_data->op_file_secctx_size);
571 err = ll_inode_init_security(&dentry, inode, parent);
574 GOTO(out_inode, err);
577 err = ll_set_encflags(inode, op_data->op_file_encctx,
578 op_data->op_file_encctx_size, false);
580 GOTO(out_inode, err);
586 ptlrpc_req_finished(request);
588 ll_finish_md_op_data(op_data);
593 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
596 struct ll_sb_info *sbi = ll_i2sbi(inode);
597 struct md_op_data *op_data;
598 struct ptlrpc_request *req = NULL;
604 switch (lump->lmm_magic) {
605 case LOV_USER_MAGIC_V1:
606 lum_size = sizeof(struct lov_user_md_v1);
608 case LOV_USER_MAGIC_V3:
609 lum_size = sizeof(struct lov_user_md_v3);
611 case LOV_USER_MAGIC_COMP_V1:
612 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
614 case LMV_USER_MAGIC: {
615 struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
617 /* MDS < 2.14 doesn't support 'crush' hash type, and
618 * cannot handle unknown hash if client doesn't set a
619 * valid one. switch to fnv_1a_64.
621 if (!(exp_connect_flags2(sbi->ll_md_exp) &
622 OBD_CONNECT2_CRUSH)) {
623 enum lmv_hash_type type = lmv->lum_hash_type &
626 if (type >= LMV_HASH_TYPE_CRUSH ||
627 type == LMV_HASH_TYPE_UNKNOWN)
629 (lmv->lum_hash_type ^ type) |
630 LMV_HASH_TYPE_FNV_1A_64;
632 if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
633 lustre_swab_lmv_user_md(lmv);
634 lum_size = sizeof(*lmv);
637 case LOV_USER_MAGIC_SPECIFIC: {
638 struct lov_user_md_v3 *v3 =
639 (struct lov_user_md_v3 *)lump;
640 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
642 lum_size = lov_user_md_size(v3->lmm_stripe_count,
643 LOV_USER_MAGIC_SPECIFIC);
648 "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
649 lump->lmm_magic, LOV_USER_MAGIC_V1,
654 /* This is coming from userspace, so should be in
655 * local endian. But the MDS would like it in little
656 * endian, so we swab it before we send it.
658 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
659 le32_to_cpu(LOV_MAGIC_MAGIC))
660 lustre_swab_lov_user_md(lump, 0);
662 lum_size = sizeof(struct lov_user_md_v1);
665 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
666 LUSTRE_OPC_ANY, NULL);
668 RETURN(PTR_ERR(op_data));
670 /* swabbing is done in lov_setstripe() on server side */
671 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
672 ll_finish_md_op_data(op_data);
673 ptlrpc_req_finished(req);
680 /* get default LMV from client cache */
681 static int ll_dir_get_default_lmv(struct inode *inode, struct lmv_user_md *lum)
683 struct ll_inode_info *lli = ll_i2info(inode);
684 const struct lmv_stripe_md *lsm;
685 bool fs_dmv_got = false;
690 if (lli->lli_def_lsm_obj) {
691 down_read(&lli->lli_lsm_sem);
692 lsm = &lli->lli_def_lsm_obj->lso_lsm;
694 lum->lum_magic = lsm->lsm_md_magic;
695 lum->lum_stripe_count = lsm->lsm_md_stripe_count;
696 lum->lum_stripe_offset = lsm->lsm_md_master_mdt_index;
697 lum->lum_hash_type = lsm->lsm_md_hash_type;
698 lum->lum_max_inherit = lsm->lsm_md_max_inherit;
699 lum->lum_max_inherit_rr = lsm->lsm_md_max_inherit_rr;
702 up_read(&lli->lli_lsm_sem);
705 if (rc == -ENODATA && !is_root_inode(inode) && !fs_dmv_got) {
706 lli = ll_i2info(inode->i_sb->s_root->d_inode);
711 if (!rc && fs_dmv_got) {
712 lli = ll_i2info(inode);
713 if (lum->lum_max_inherit != LMV_INHERIT_UNLIMITED) {
714 if (lum->lum_max_inherit == LMV_INHERIT_NONE ||
715 lum->lum_max_inherit < LMV_INHERIT_END ||
716 lum->lum_max_inherit > LMV_INHERIT_MAX ||
717 lum->lum_max_inherit <= lli->lli_dir_depth)
718 GOTO(out, rc = -ENODATA);
720 lum->lum_max_inherit -= lli->lli_dir_depth;
723 if (lum->lum_max_inherit_rr != LMV_INHERIT_RR_UNLIMITED) {
724 if (lum->lum_max_inherit_rr == LMV_INHERIT_NONE ||
725 lum->lum_max_inherit_rr < LMV_INHERIT_RR_END ||
726 lum->lum_max_inherit_rr > LMV_INHERIT_RR_MAX ||
727 lum->lum_max_inherit_rr <= lli->lli_dir_depth)
728 lum->lum_max_inherit_rr = LMV_INHERIT_RR_NONE;
730 if (lum->lum_max_inherit_rr > lli->lli_dir_depth)
731 lum->lum_max_inherit_rr -= lli->lli_dir_depth;
738 int ll_dir_get_default_layout(struct inode *inode, void **plmm, int *plmm_size,
739 struct ptlrpc_request **request, u64 valid,
740 enum get_default_layout_type type)
742 struct ll_sb_info *sbi = ll_i2sbi(inode);
743 struct mdt_body *body;
744 struct lov_mds_md *lmm = NULL;
745 struct ptlrpc_request *req = NULL;
746 int lmm_size = OBD_MAX_DEFAULT_EA_SIZE;
747 struct md_op_data *op_data;
753 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, lmm_size,
754 LUSTRE_OPC_ANY, NULL);
756 RETURN(PTR_ERR(op_data));
758 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
760 if (type == GET_DEFAULT_LAYOUT_ROOT) {
761 lu_root_fid(&op_data->op_fid1);
762 fid = op_data->op_fid1;
764 fid = *ll_inode2fid(inode);
767 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
768 ll_finish_md_op_data(op_data);
770 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
775 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
776 LASSERT(body != NULL);
778 lmm_size = body->mbo_eadatasize;
780 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
782 GOTO(out, rc = -ENODATA);
785 lmm = req_capsule_server_sized_get(&req->rq_pill,
786 &RMF_MDT_MD, lmm_size);
787 LASSERT(lmm != NULL);
789 /* This is coming from the MDS, so is probably in
790 * little endian. We convert it to host endian before
791 * passing it to userspace.
793 /* We don't swab objects for directories */
794 switch (le32_to_cpu(lmm->lmm_magic)) {
797 case LOV_MAGIC_COMP_V1:
798 case LOV_USER_MAGIC_SPECIFIC:
799 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
800 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
803 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
804 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
807 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
808 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
810 case LMV_MAGIC_FOREIGN: {
811 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
813 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
814 __swab32s(&lfm->lfm_magic);
815 __swab32s(&lfm->lfm_length);
816 __swab32s(&lfm->lfm_type);
817 __swab32s(&lfm->lfm_flags);
823 CERROR("%s: unknown magic: %lX: rc = %d\n", sbi->ll_fsname,
824 (unsigned long)lmm->lmm_magic, rc);
828 *plmm_size = lmm_size;
834 * This function will be used to get default LOV/LMV/Default LMV
835 * @valid will be used to indicate which stripe it will retrieve.
836 * If the directory does not have its own default layout, then the
837 * function will request the default layout from root FID.
838 * OBD_MD_MEA LMV stripe EA
839 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
840 * otherwise Default LOV EA.
841 * Each time, it can only retrieve 1 stripe EA
843 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
844 struct ptlrpc_request **request,
845 struct ptlrpc_request **root_request,
848 struct ptlrpc_request *req = NULL;
849 struct ptlrpc_request *root_req = NULL;
850 struct lov_mds_md *lmm = NULL;
855 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
857 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
858 !(valid & OBD_MD_MEA) && root_request != NULL) {
859 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
860 &lmm_size, &root_req, valid,
861 GET_DEFAULT_LAYOUT_ROOT);
867 *plmm_size = lmm_size;
869 if (root_request != NULL)
870 *root_request = root_req;
876 * This function will be used to get default LOV/LMV/Default LMV
877 * @valid will be used to indicate which stripe it will retrieve
878 * OBD_MD_MEA LMV stripe EA
879 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
880 * otherwise Default LOV EA.
881 * Each time, it can only retrieve 1 stripe EA
883 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
884 struct ptlrpc_request **request, u64 valid)
886 struct ptlrpc_request *req = NULL;
887 struct lov_mds_md *lmm = NULL;
892 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
896 *plmm_size = lmm_size;
902 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
904 struct md_op_data *op_data;
909 OBD_ALLOC_PTR(op_data);
913 op_data->op_flags |= MF_GET_MDT_IDX;
914 op_data->op_fid1 = *fid;
915 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
916 mdt_index = op_data->op_mds;
917 OBD_FREE_PTR(op_data);
925 * Get MDT index for the inode.
927 int ll_get_mdt_idx(struct inode *inode)
929 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
933 * Generic handler to do any pre-copy work.
935 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
936 * first information for it that real work has started.
938 * Moreover, for a ARCHIVE request, it will sample the file data version and
939 * store it in \a copy.
941 * \return 0 on success.
943 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
945 struct ll_sb_info *sbi = ll_s2sbi(sb);
946 struct hsm_progress_kernel hpk;
951 /* Forge a hsm_progress based on data from copy. */
952 hpk.hpk_fid = copy->hc_hai.hai_fid;
953 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
954 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
955 hpk.hpk_extent.length = 0;
958 hpk.hpk_data_version = 0;
961 /* For archive request, we need to read the current file version. */
962 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
964 __u64 data_version = 0;
966 /* Get inode for this fid */
967 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
969 hpk.hpk_flags |= HP_FLAG_RETRY;
970 /* hpk_errval is >= 0 */
971 hpk.hpk_errval = -PTR_ERR(inode);
972 GOTO(progress, rc = PTR_ERR(inode));
975 /* Read current file data version */
976 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
979 CDEBUG(D_HSM, "Could not read file data version of "
980 DFID" (rc = %d). Archive request ("
981 "%#llx) could not be done.\n",
982 PFID(©->hc_hai.hai_fid), rc,
983 copy->hc_hai.hai_cookie);
984 hpk.hpk_flags |= HP_FLAG_RETRY;
985 /* hpk_errval must be >= 0 */
986 hpk.hpk_errval = -rc;
990 /* Store in the hsm_copy for later copytool use.
991 * Always modified even if no lsm. */
992 copy->hc_data_version = data_version;
996 /* On error, the request should be considered as completed */
997 if (hpk.hpk_errval > 0)
998 hpk.hpk_flags |= HP_FLAG_COMPLETED;
1000 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1003 /* Return first error */
1004 RETURN(rc != 0 ? rc : rc2);
1008 * Generic handler to do any post-copy work.
1010 * It will send the last hsm_progress update to coordinator to inform it
1011 * that copy is finished and whether it was successful or not.
1014 * - for ARCHIVE request, it will sample the file data version and compare it
1015 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
1016 * will be considered as failed.
1017 * - for RESTORE request, it will sample the file data version and send it to
1018 * coordinator which is useful if the file was imported as 'released'.
1020 * \return 0 on success.
1022 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
1024 struct ll_sb_info *sbi = ll_s2sbi(sb);
1025 struct hsm_progress_kernel hpk;
1030 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
1031 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
1032 * initialized if copy_end was called with copy == NULL.
1035 /* Forge a hsm_progress based on data from copy. */
1036 hpk.hpk_fid = copy->hc_hai.hai_fid;
1037 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1038 hpk.hpk_extent = copy->hc_hai.hai_extent;
1039 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1040 hpk.hpk_errval = copy->hc_errval;
1041 hpk.hpk_data_version = 0;
1043 /* For archive request, we need to check the file data was not changed.
1045 * For restore request, we need to send the file data version, this is
1046 * useful when the file was created using hsm_import.
1048 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1049 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1050 (copy->hc_errval == 0)) {
1051 struct inode *inode;
1052 __u64 data_version = 0;
1054 /* Get lsm for this fid */
1055 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1056 if (IS_ERR(inode)) {
1057 hpk.hpk_flags |= HP_FLAG_RETRY;
1058 /* hpk_errval must be >= 0 */
1059 hpk.hpk_errval = -PTR_ERR(inode);
1060 GOTO(progress, rc = PTR_ERR(inode));
1063 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1067 "Could not read file data version. Request could not be confirmed.\n");
1068 if (hpk.hpk_errval == 0)
1069 hpk.hpk_errval = -rc;
1073 /* Store in the hsm_copy for later copytool use.
1074 * Always modified even if no lsm. */
1075 hpk.hpk_data_version = data_version;
1077 /* File could have been stripped during archiving, so we need
1078 * to check anyway. */
1079 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1080 (copy->hc_data_version != data_version)) {
1081 CDEBUG(D_HSM, "File data version mismatched. "
1082 "File content was changed during archiving. "
1083 DFID", start:%#llx current:%#llx\n",
1084 PFID(©->hc_hai.hai_fid),
1085 copy->hc_data_version, data_version);
1086 /* File was changed, send error to cdt. Do not ask for
1087 * retry because if a file is modified frequently,
1088 * the cdt will loop on retried archive requests.
1089 * The policy engine will ask for a new archive later
1090 * when the file will not be modified for some tunable
1092 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1094 /* hpk_errval must be >= 0 */
1095 hpk.hpk_errval = -rc;
1102 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1105 /* Return first error */
1106 RETURN(rc != 0 ? rc : rc2);
1110 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1111 const struct lustre_kernelcomm __user *data)
1113 struct lustre_kernelcomm *lk;
1114 struct lustre_kernelcomm *tmp;
1115 size_t size = sizeof(*lk);
1120 /* copy data from userspace to get numbers of archive_id */
1121 OBD_ALLOC(lk, size);
1125 if (copy_from_user(lk, data, size))
1126 GOTO(out_lk, rc = -EFAULT);
1128 if (lk->lk_flags & LK_FLG_STOP)
1131 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1132 __u32 archive_mask = lk->lk_data_count;
1135 /* old hsm agent to old MDS */
1136 if (!exp_connect_archive_id_array(exp))
1139 /* old hsm agent to new MDS */
1140 lk->lk_flags |= LK_FLG_DATANR;
1142 if (archive_mask == 0)
1145 count = hweight32(archive_mask);
1146 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1147 OBD_ALLOC(tmp, new_size);
1149 GOTO(out_lk, rc = -ENOMEM);
1151 memcpy(tmp, lk, size);
1152 tmp->lk_data_count = count;
1158 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1159 if (BIT(i) & archive_mask) {
1160 lk->lk_data[count] = i + 1;
1167 /* new hsm agent to new mds */
1168 if (lk->lk_data_count > 0) {
1169 new_size = offsetof(struct lustre_kernelcomm,
1170 lk_data[lk->lk_data_count]);
1171 OBD_ALLOC(tmp, new_size);
1173 GOTO(out_lk, rc = -ENOMEM);
1179 if (copy_from_user(lk, data, size))
1180 GOTO(out_lk, rc = -EFAULT);
1183 /* new hsm agent to old MDS */
1184 if (!exp_connect_archive_id_array(exp)) {
1187 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1188 GOTO(out_lk, rc = -EINVAL);
1190 for (i = 0; i < lk->lk_data_count; i++) {
1191 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1193 CERROR("%s: archive id %d requested but only [0 - %zu] supported: rc = %d\n",
1194 exp->exp_obd->obd_name, lk->lk_data[i],
1195 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1199 if (lk->lk_data[i] == 0) {
1204 archives |= (1 << (lk->lk_data[i] - 1));
1206 lk->lk_flags &= ~LK_FLG_DATANR;
1207 lk->lk_data_count = archives;
1210 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1216 static int check_owner(int type, int id)
1220 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1224 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1233 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1235 struct ll_sb_info *sbi = ll_s2sbi(sb);
1236 int cmd = qctl->qc_cmd;
1237 int type = qctl->qc_type;
1238 int id = qctl->qc_id;
1239 int valid = qctl->qc_valid;
1247 case LUSTRE_Q_SETDEFAULT:
1248 case LUSTRE_Q_SETQUOTAPOOL:
1249 case LUSTRE_Q_SETINFOPOOL:
1250 case LUSTRE_Q_SETDEFAULT_POOL:
1251 case LUSTRE_Q_DELETEQID:
1252 case LUSTRE_Q_RESETQID:
1253 if (!capable(CAP_SYS_ADMIN))
1256 if (sb->s_flags & SB_RDONLY)
1260 case LUSTRE_Q_GETDEFAULT:
1261 case LUSTRE_Q_GETQUOTAPOOL:
1262 case LUSTRE_Q_GETDEFAULT_POOL:
1263 if (check_owner(type, id) &&
1264 (!capable(CAP_SYS_ADMIN)))
1268 case LUSTRE_Q_GETINFOPOOL:
1271 CERROR("%s: unsupported quotactl op: %#x: rc = %d\n",
1272 sbi->ll_fsname, cmd, -EOPNOTSUPP);
1273 RETURN(-EOPNOTSUPP);
1276 if (valid != QC_GENERAL) {
1277 if (cmd == Q_GETINFO)
1278 qctl->qc_cmd = Q_GETOINFO;
1279 else if (cmd == Q_GETQUOTA ||
1280 cmd == LUSTRE_Q_GETQUOTAPOOL)
1281 qctl->qc_cmd = Q_GETOQUOTA;
1287 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1288 sizeof(*qctl), qctl, NULL);
1291 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1292 sizeof(*qctl), qctl, NULL);
1295 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1296 sizeof(*qctl), qctl, NULL);
1298 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1300 sizeof(*qctl), qctl, NULL);
1311 struct obd_quotactl *oqctl;
1312 int oqctl_len = sizeof(*oqctl);
1314 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1315 oqctl_len += LOV_MAXPOOLNAME + 1;
1317 OBD_ALLOC(oqctl, oqctl_len);
1321 QCTL_COPY(oqctl, qctl);
1322 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1324 OBD_FREE(oqctl, oqctl_len);
1327 /* If QIF_SPACE is not set, client should collect the
1328 * space usage from OSSs by itself
1330 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1331 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1332 !oqctl->qc_dqblk.dqb_curspace) {
1333 struct obd_quotactl *oqctl_tmp;
1334 int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1336 OBD_ALLOC(oqctl_tmp, qctl_len);
1337 if (oqctl_tmp == NULL)
1338 GOTO(out, rc = -ENOMEM);
1340 if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1341 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1342 memcpy(oqctl_tmp->qc_poolname,
1344 LOV_MAXPOOLNAME + 1);
1346 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1348 oqctl_tmp->qc_id = oqctl->qc_id;
1349 oqctl_tmp->qc_type = oqctl->qc_type;
1351 /* collect space usage from OSTs */
1352 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1353 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1354 if (!rc || rc == -EREMOTEIO) {
1355 oqctl->qc_dqblk.dqb_curspace =
1356 oqctl_tmp->qc_dqblk.dqb_curspace;
1357 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1360 /* collect space & inode usage from MDTs */
1361 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1362 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1363 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1364 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1365 if (!rc || rc == -EREMOTEIO) {
1366 oqctl->qc_dqblk.dqb_curspace +=
1367 oqctl_tmp->qc_dqblk.dqb_curspace;
1368 oqctl->qc_dqblk.dqb_curinodes =
1369 oqctl_tmp->qc_dqblk.dqb_curinodes;
1370 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1372 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1375 OBD_FREE(oqctl_tmp, qctl_len);
1378 QCTL_COPY(qctl, oqctl);
1379 OBD_FREE(oqctl, oqctl_len);
1385 static int ll_rmfid(struct file *file, void __user *arg)
1387 const struct fid_array __user *ufa = arg;
1388 struct inode *inode = file_inode(file);
1389 struct ll_sb_info *sbi = ll_i2sbi(inode);
1390 struct fid_array *lfa = NULL;
1393 int i, rc, *rcs = NULL;
1396 if (!capable(CAP_DAC_READ_SEARCH) &&
1397 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1399 /* Only need to get the buflen */
1400 if (get_user(nr, &ufa->fa_nr))
1402 /* DoS protection */
1403 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1406 size = offsetof(struct fid_array, fa_fids[nr]);
1407 OBD_ALLOC(lfa, size);
1410 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1412 GOTO(free_lfa, rc = -ENOMEM);
1414 if (copy_from_user(lfa, arg, size))
1415 GOTO(free_rcs, rc = -EFAULT);
1417 /* In case of subdirectory mount, we need to make sure all the files
1418 * for which we want to remove FID are visible in the namespace.
1420 if (!fid_is_root(&sbi->ll_root_fid)) {
1421 struct fid_array *lfa_new = NULL;
1422 int path_len = PATH_MAX, linkno;
1423 struct getinfo_fid2path *gf;
1424 int idx, last_idx = nr - 1;
1426 OBD_ALLOC(lfa_new, size);
1428 GOTO(free_rcs, rc = -ENOMEM);
1431 gf = kmalloc(sizeof(*gf) + path_len + 1, GFP_NOFS);
1433 GOTO(free_rcs, rc = -ENOMEM);
1435 for (idx = 0; idx < nr; idx++) {
1438 memset(gf, 0, sizeof(*gf) + path_len + 1);
1439 gf->gf_fid = lfa->fa_fids[idx];
1440 gf->gf_pathlen = path_len;
1441 gf->gf_linkno = linkno;
1442 rc = __ll_fid2path(inode, gf,
1443 sizeof(*gf) + gf->gf_pathlen,
1445 if (rc == -ENAMETOOLONG) {
1446 struct getinfo_fid2path *tmpgf;
1448 path_len += PATH_MAX;
1449 tmpgf = krealloc(gf,
1450 sizeof(*gf) + path_len + 1,
1454 OBD_FREE(lfa_new, size);
1455 GOTO(free_rcs, rc = -ENOMEM);
1462 if (gf->gf_linkno == linkno)
1464 linkno = gf->gf_linkno;
1468 /* All the links for this fid are visible in the
1469 * mounted subdir. So add it to the list of fids
1472 lfa_new->fa_fids[lfa_new->fa_nr++] =
1475 /* At least one link for this fid is not visible
1476 * in the mounted subdir. So add it at the end
1477 * of the list that will be hidden to lower
1478 * layers, and set -ENOENT as ret code.
1480 lfa_new->fa_fids[last_idx] = lfa->fa_fids[idx];
1481 rcs[last_idx--] = rc;
1485 OBD_FREE(lfa, size);
1489 if (lfa->fa_nr == 0)
1490 GOTO(free_rcs, rc = rcs[nr - 1]);
1492 /* Call mdc_iocontrol */
1493 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1496 for (i = 0; i < nr; i++)
1498 lfa->fa_fids[i].f_ver = rcs[i];
1499 if (copy_to_user(arg, lfa, size))
1504 OBD_FREE_PTR_ARRAY(rcs, nr);
1506 OBD_FREE(lfa, size);
1511 /* This function tries to get a single name component,
1512 * to send to the server. No actual path traversal involved,
1513 * so we limit to NAME_MAX */
1514 static char *ll_getname(const char __user *filename)
1519 OBD_ALLOC(tmp, NAME_MAX + 1);
1522 return ERR_PTR(-ENOMEM);
1524 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1527 else if (len > NAME_MAX)
1528 ret = -ENAMETOOLONG;
1531 OBD_FREE(tmp, NAME_MAX + 1);
1537 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1539 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1541 struct dentry *dentry = file_dentry(file);
1542 struct inode *inode = file_inode(file);
1543 struct ll_sb_info *sbi = ll_i2sbi(inode);
1544 struct obd_ioctl_data *data = NULL;
1545 void __user *uarg = (void __user *)arg;
1549 CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
1550 PFID(ll_inode2fid(inode)), inode, cmd, arg);
1552 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1553 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1556 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1558 case IOC_MDC_LOOKUP: {
1559 int namelen, len = 0;
1562 rc = obd_ioctl_getdata(&data, &len, uarg);
1566 filename = data->ioc_inlbuf1;
1567 namelen = strlen(filename);
1569 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1570 GOTO(out_free, rc = -EINVAL);
1573 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1575 CERROR("%s: lookup %.*s failed: rc = %d\n",
1576 sbi->ll_fsname, namelen, filename, rc);
1580 OBD_FREE_LARGE(data, len);
1583 case LL_IOC_LMV_SETSTRIPE: {
1584 struct lmv_user_md *lum;
1589 bool createonly = false;
1593 rc = obd_ioctl_getdata(&data, &len, uarg);
1597 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1598 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1599 GOTO(lmv_out_free, rc = -EINVAL);
1601 filename = data->ioc_inlbuf1;
1602 namelen = data->ioc_inllen1;
1605 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1606 GOTO(lmv_out_free, rc = -EINVAL);
1608 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1609 lumlen = data->ioc_inllen2;
1611 if (!lmv_user_magic_supported(lum->lum_magic)) {
1612 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1613 lum->lum_magic, -EINVAL);
1614 GOTO(lmv_out_free, rc = -EINVAL);
1617 if ((lum->lum_magic == LMV_USER_MAGIC ||
1618 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1619 lumlen < sizeof(*lum)) {
1620 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1621 filename, lumlen, lum->lum_magic, -EINVAL);
1622 GOTO(lmv_out_free, rc = -EINVAL);
1625 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1626 lumlen < sizeof(struct lmv_foreign_md)) {
1627 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1628 filename, lum->lum_magic, lumlen, -EFAULT);
1629 GOTO(lmv_out_free, rc = -EINVAL);
1632 mode = data->ioc_type;
1633 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1634 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1637 OBD_FREE_LARGE(data, len);
1641 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1642 struct lmv_user_md lum;
1643 struct lmv_user_md __user *ulump = uarg;
1646 if (copy_from_user(&lum, ulump, sizeof(lum)))
1649 if (lum.lum_magic != LMV_USER_MAGIC)
1652 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1656 case LL_IOC_LOV_SETSTRIPE_NEW:
1657 case LL_IOC_LOV_SETSTRIPE: {
1658 struct lov_user_md_v3 *lumv3 = NULL;
1659 struct lov_user_md_v1 lumv1;
1660 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1661 struct lov_user_md_v1 __user *lumv1p = uarg;
1662 struct lov_user_md_v3 __user *lumv3p = uarg;
1664 int set_default = 0;
1666 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1667 sizeof(struct lov_comp_md_v1));
1668 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1669 /* first try with v1 which is smaller than v3 */
1670 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1673 if (is_root_inode(inode))
1676 switch (lumv1.lmm_magic) {
1677 case LOV_USER_MAGIC_V3:
1678 case LOV_USER_MAGIC_SPECIFIC:
1679 lum_size = ll_lov_user_md_size(&lumv1);
1682 OBD_ALLOC(lumv3, lum_size);
1685 if (copy_from_user(lumv3, lumv3p, lum_size))
1686 GOTO(out, rc = -EFAULT);
1687 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1689 case LOV_USER_MAGIC_V1:
1692 GOTO(out, rc = -EOPNOTSUPP);
1695 /* in v1 and v3 cases lumv1 points to data */
1696 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1699 OBD_FREE(lumv3, lum_size);
1702 case LL_IOC_LMV_GETSTRIPE: {
1703 struct lmv_user_md __user *ulmv = uarg;
1704 struct lmv_user_md lum;
1705 struct ptlrpc_request *request = NULL;
1706 union lmv_mds_md *lmm = NULL;
1709 struct lmv_user_md *tmp = NULL;
1713 int max_stripe_count;
1717 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1720 /* get default LMV */
1721 if (lum.lum_magic == LMV_USER_MAGIC &&
1722 lum.lum_type != LMV_TYPE_RAW) {
1723 rc = ll_dir_get_default_lmv(inode, &lum);
1727 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1733 max_stripe_count = lum.lum_stripe_count;
1734 /* lum_magic will indicate which stripe the ioctl will like
1735 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1736 * is for default LMV stripe */
1737 if (lum.lum_magic == LMV_MAGIC_V1)
1738 valid |= OBD_MD_MEA;
1739 else if (lum.lum_magic == LMV_USER_MAGIC)
1740 valid |= OBD_MD_DEFAULT_MEA;
1744 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1745 &request, NULL, valid);
1747 GOTO(finish_req, rc);
1749 /* get default LMV in raw mode */
1750 if (lum.lum_magic == LMV_USER_MAGIC) {
1751 if (copy_to_user(ulmv, lmm, lmmsize))
1752 GOTO(finish_req, rc = -EFAULT);
1753 GOTO(finish_req, rc);
1756 /* if foreign LMV case, fake stripes number */
1757 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1758 struct lmv_foreign_md *lfm;
1760 lfm = (struct lmv_foreign_md *)lmm;
1761 if (lfm->lfm_length < XATTR_SIZE_MAX -
1762 offsetof(typeof(*lfm), lfm_value)) {
1763 __u32 size = lfm->lfm_length +
1764 offsetof(typeof(*lfm), lfm_value);
1766 stripe_count = lmv_foreign_to_md_stripes(size);
1768 CERROR("%s: invalid %d foreign size returned: rc = %d\n",
1769 sbi->ll_fsname, lfm->lfm_length,
1774 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1776 if (max_stripe_count < stripe_count) {
1777 lum.lum_stripe_count = stripe_count;
1778 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1779 GOTO(finish_req, rc = -EFAULT);
1780 GOTO(finish_req, rc = -E2BIG);
1783 /* enough room on user side and foreign case */
1784 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1785 struct lmv_foreign_md *lfm;
1788 lfm = (struct lmv_foreign_md *)lmm;
1789 size = lfm->lfm_length +
1790 offsetof(struct lmv_foreign_md, lfm_value);
1791 if (copy_to_user(ulmv, lfm, size))
1792 GOTO(finish_req, rc = -EFAULT);
1793 GOTO(finish_req, rc);
1796 lum_size = lmv_user_md_size(stripe_count,
1797 LMV_USER_MAGIC_SPECIFIC);
1798 OBD_ALLOC(tmp, lum_size);
1800 GOTO(finish_req, rc = -ENOMEM);
1802 mdt_index = ll_get_mdt_idx(inode);
1804 GOTO(out_tmp, rc = -ENOMEM);
1806 tmp->lum_magic = LMV_MAGIC_V1;
1807 tmp->lum_stripe_count = 0;
1808 tmp->lum_stripe_offset = mdt_index;
1809 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1810 for (i = 0; i < stripe_count; i++) {
1813 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1814 if (fid_is_sane(&fid)) {
1815 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1817 GOTO(out_tmp, rc = mdt_index);
1819 tmp->lum_objects[i].lum_mds = mdt_index;
1820 tmp->lum_objects[i].lum_fid = fid;
1823 tmp->lum_stripe_count++;
1826 if (copy_to_user(ulmv, tmp, lum_size))
1827 GOTO(out_tmp, rc = -EFAULT);
1829 OBD_FREE(tmp, lum_size);
1831 ptlrpc_req_finished(request);
1834 case LL_IOC_REMOVE_ENTRY: {
1835 char *filename = NULL;
1839 /* Here is a little hack to avoid sending REINT_RMENTRY to
1840 * unsupported server, which might crash the server(LU-2730),
1841 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1842 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1843 * server will support REINT_RMENTRY XXX*/
1844 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1845 RETURN(-EOPNOTSUPP);
1847 filename = ll_getname(uarg);
1848 if (IS_ERR(filename))
1849 RETURN(PTR_ERR(filename));
1851 namelen = strlen(filename);
1853 GOTO(out_rmdir, rc = -EINVAL);
1855 rc = ll_rmdir_entry(inode, filename, namelen);
1858 ll_putname(filename);
1862 RETURN(ll_rmfid(file, uarg));
1863 case LL_IOC_LOV_SWAP_LAYOUTS:
1865 case LL_IOC_LOV_GETSTRIPE:
1866 case LL_IOC_LOV_GETSTRIPE_NEW:
1867 case LL_IOC_MDC_GETINFO_V1:
1868 case LL_IOC_MDC_GETINFO_V2:
1869 case IOC_MDC_GETFILEINFO_V1:
1870 case IOC_MDC_GETFILEINFO_V2:
1871 case IOC_MDC_GETFILESTRIPE: {
1872 struct ptlrpc_request *request = NULL;
1873 struct ptlrpc_request *root_request = NULL;
1874 struct lov_user_md __user *lump;
1875 struct lov_mds_md *lmm = NULL;
1876 struct mdt_body *body;
1877 char *filename = NULL;
1878 lstat_t __user *statp = NULL;
1879 lstatx_t __user *stxp = NULL;
1880 __u64 __user *flagsp = NULL;
1881 __u32 __user *lmmsizep = NULL;
1882 struct lu_fid __user *fidp = NULL;
1886 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1887 cmd == IOC_MDC_GETFILEINFO_V2 ||
1888 cmd == IOC_MDC_GETFILESTRIPE) {
1889 filename = ll_getname(uarg);
1890 if (IS_ERR(filename))
1891 RETURN(PTR_ERR(filename));
1893 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1894 &lmmsize, &request);
1896 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1902 body = req_capsule_server_get(&request->rq_pill,
1904 LASSERT(body != NULL);
1909 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1910 cmd == LL_IOC_MDC_GETINFO_V1 ||
1911 cmd == IOC_MDC_GETFILEINFO_V2 ||
1912 cmd == LL_IOC_MDC_GETINFO_V2)) {
1920 if (cmd == IOC_MDC_GETFILESTRIPE ||
1921 cmd == LL_IOC_LOV_GETSTRIPE ||
1922 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1924 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1925 cmd == LL_IOC_MDC_GETINFO_V1){
1926 struct lov_user_mds_data_v1 __user *lmdp;
1929 statp = &lmdp->lmd_st;
1930 lump = &lmdp->lmd_lmm;
1932 struct lov_user_mds_data __user *lmdp;
1935 fidp = &lmdp->lmd_fid;
1936 stxp = &lmdp->lmd_stx;
1937 flagsp = &lmdp->lmd_flags;
1938 lmmsizep = &lmdp->lmd_lmmsize;
1939 lump = &lmdp->lmd_lmm;
1943 /* If the file has no striping then zero out *lump so
1944 * that the caller isn't confused by garbage. */
1945 if (clear_user(lump, sizeof(*lump)))
1946 GOTO(out_req, rc = -EFAULT);
1947 } else if (copy_to_user(lump, lmm, lmmsize)) {
1948 if (copy_to_user(lump, lmm, sizeof(*lump)))
1949 GOTO(out_req, rc = -EFAULT);
1952 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1954 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1955 cmd == LL_IOC_MDC_GETINFO_V1) {
1958 st.st_dev = inode->i_sb->s_dev;
1959 st.st_mode = body->mbo_mode;
1960 st.st_nlink = body->mbo_nlink;
1961 st.st_uid = body->mbo_uid;
1962 st.st_gid = body->mbo_gid;
1963 st.st_rdev = body->mbo_rdev;
1964 if (llcrypt_require_key(inode) == -ENOKEY)
1965 st.st_size = round_up(st.st_size,
1966 LUSTRE_ENCRYPTION_UNIT_SIZE);
1968 st.st_size = body->mbo_size;
1969 st.st_blksize = PAGE_SIZE;
1970 st.st_blocks = body->mbo_blocks;
1971 st.st_atime = body->mbo_atime;
1972 st.st_mtime = body->mbo_mtime;
1973 st.st_ctime = body->mbo_ctime;
1974 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1977 if (copy_to_user(statp, &st, sizeof(st)))
1978 GOTO(out_req, rc = -EFAULT);
1979 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1980 cmd == LL_IOC_MDC_GETINFO_V2) {
1981 lstatx_t stx = { 0 };
1982 __u64 valid = body->mbo_valid;
1984 stx.stx_blksize = PAGE_SIZE;
1985 stx.stx_nlink = body->mbo_nlink;
1986 stx.stx_uid = body->mbo_uid;
1987 stx.stx_gid = body->mbo_gid;
1988 stx.stx_mode = body->mbo_mode;
1989 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1991 if (llcrypt_require_key(inode) == -ENOKEY)
1992 stx.stx_size = round_up(stx.stx_size,
1993 LUSTRE_ENCRYPTION_UNIT_SIZE);
1995 stx.stx_size = body->mbo_size;
1996 stx.stx_blocks = body->mbo_blocks;
1997 stx.stx_atime.tv_sec = body->mbo_atime;
1998 stx.stx_ctime.tv_sec = body->mbo_ctime;
1999 stx.stx_mtime.tv_sec = body->mbo_mtime;
2000 stx.stx_btime.tv_sec = body->mbo_btime;
2001 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
2002 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
2003 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
2004 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
2005 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
2007 stx.stx_attributes_mask = STATX_ATTR_IMMUTABLE |
2009 #ifdef HAVE_LUSTRE_CRYPTO
2010 stx.stx_attributes_mask |= STATX_ATTR_ENCRYPTED;
2012 if (body->mbo_valid & OBD_MD_FLFLAGS) {
2013 stx.stx_attributes |= body->mbo_flags;
2014 /* if Lustre specific LUSTRE_ENCRYPT_FL flag is
2015 * set, also set ext4 equivalent to please statx
2017 if (body->mbo_flags & LUSTRE_ENCRYPT_FL)
2018 stx.stx_attributes |= STATX_ATTR_ENCRYPTED;
2021 /* For a striped directory, the size and blocks returned
2022 * from MDT is not correct.
2023 * The size and blocks are aggregated by client across
2025 * Thus for a striped directory, do not return the valid
2026 * FLSIZE and FLBLOCKS flags to the caller.
2027 * However, this whould be better decided by the MDS
2028 * instead of the client.
2030 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
2031 ll_dir_striped(inode))
2032 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
2034 if (flagsp && copy_to_user(flagsp, &valid,
2036 GOTO(out_req, rc = -EFAULT);
2038 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
2040 GOTO(out_req, rc = -EFAULT);
2042 if (!(valid & OBD_MD_FLSIZE))
2043 stx.stx_mask &= ~STATX_SIZE;
2044 if (!(valid & OBD_MD_FLBLOCKS))
2045 stx.stx_mask &= ~STATX_BLOCKS;
2047 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
2048 GOTO(out_req, rc = -EFAULT);
2050 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2052 GOTO(out_req, rc = -EFAULT);
2057 ptlrpc_req_finished(request);
2058 ptlrpc_req_finished(root_request);
2060 ll_putname(filename);
2063 case OBD_IOC_QUOTACTL: {
2064 struct if_quotactl *qctl;
2065 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2067 OBD_ALLOC(qctl, qctl_len);
2071 if (copy_from_user(qctl, uarg, sizeof(*qctl)))
2072 GOTO(out_quotactl, rc = -EFAULT);
2074 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2075 char __user *from = uarg +
2076 offsetof(typeof(*qctl), qc_poolname);
2077 if (copy_from_user(qctl->qc_poolname, from,
2078 LOV_MAXPOOLNAME + 1))
2079 GOTO(out_quotactl, rc = -EFAULT);
2082 rc = quotactl_ioctl(inode->i_sb, qctl);
2083 if ((rc == 0 || rc == -ENODATA) &&
2084 copy_to_user(uarg, qctl, sizeof(*qctl)))
2087 OBD_FREE(qctl, qctl_len);
2090 case LL_IOC_GETOBDCOUNT: {
2092 struct obd_export *exp;
2094 if (copy_from_user(&count, uarg, sizeof(count)))
2097 /* get ost count when count is zero, get mdt count otherwise */
2098 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2099 vallen = sizeof(count);
2100 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2101 KEY_TGT_COUNT, &vallen, &count);
2103 CERROR("%s: get target count failed: rc = %d\n",
2104 sbi->ll_fsname, rc);
2108 if (copy_to_user(uarg, &count, sizeof(count)))
2113 case LL_IOC_GET_CONNECT_FLAGS:
2114 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, uarg));
2115 case LL_IOC_FID2MDTIDX: {
2116 struct obd_export *exp = ll_i2mdexp(inode);
2120 if (copy_from_user(&fid, uarg, sizeof(fid)))
2123 /* Call mdc_iocontrol */
2124 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2125 (__u32 __user *)&index);
2131 case LL_IOC_HSM_REQUEST: {
2132 struct hsm_user_request *hur;
2139 /* We don't know the true size yet; copy the fixed-size part */
2140 if (copy_from_user(hur, uarg, sizeof(*hur))) {
2145 /* Compute the whole struct size */
2146 totalsize = hur_len(hur);
2151 /* Final size will be more than double totalsize */
2152 if (totalsize >= MDS_MAXREQSIZE / 3)
2155 OBD_ALLOC_LARGE(hur, totalsize);
2159 /* Copy the whole struct */
2160 if (copy_from_user(hur, uarg, totalsize))
2161 GOTO(out_hur, rc = -EFAULT);
2163 if (hur->hur_request.hr_action == HUA_RELEASE) {
2164 const struct lu_fid *fid;
2168 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2169 fid = &hur->hur_user_item[i].hui_fid;
2170 f = search_inode_for_lustre(inode->i_sb, fid);
2176 rc = ll_hsm_release(f);
2182 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2186 OBD_FREE_LARGE(hur, totalsize);
2190 case LL_IOC_HSM_PROGRESS: {
2191 struct hsm_progress_kernel hpk;
2192 struct hsm_progress hp;
2194 if (copy_from_user(&hp, uarg, sizeof(hp)))
2197 hpk.hpk_fid = hp.hp_fid;
2198 hpk.hpk_cookie = hp.hp_cookie;
2199 hpk.hpk_extent = hp.hp_extent;
2200 hpk.hpk_flags = hp.hp_flags;
2201 hpk.hpk_errval = hp.hp_errval;
2202 hpk.hpk_data_version = 0;
2204 /* File may not exist in Lustre; all progress
2205 * reported to Lustre root */
2206 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2210 case LL_IOC_HSM_CT_START:
2211 if (!capable(CAP_SYS_ADMIN))
2214 rc = copy_and_ct_start(cmd, sbi->ll_md_exp, uarg);
2217 case LL_IOC_HSM_COPY_START: {
2218 struct hsm_copy *copy;
2221 OBD_ALLOC_PTR(copy);
2224 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2229 rc = ll_ioc_copy_start(inode->i_sb, copy);
2230 if (copy_to_user(uarg, copy, sizeof(*copy)))
2236 case LL_IOC_HSM_COPY_END: {
2237 struct hsm_copy *copy;
2240 OBD_ALLOC_PTR(copy);
2243 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2248 rc = ll_ioc_copy_end(inode->i_sb, copy);
2249 if (copy_to_user(uarg, copy, sizeof(*copy)))
2255 case LL_IOC_MIGRATE: {
2256 struct lmv_user_md *lum;
2263 rc = obd_ioctl_getdata(&data, &len, uarg);
2267 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2268 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2269 GOTO(migrate_free, rc = -EINVAL);
2271 filename = data->ioc_inlbuf1;
2272 namelen = data->ioc_inllen1;
2273 flags = data->ioc_type;
2275 if (namelen < 1 || namelen != strlen(filename) + 1) {
2276 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2277 GOTO(migrate_free, rc = -EINVAL);
2280 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2281 if (lum->lum_magic != LMV_USER_MAGIC &&
2282 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2284 CERROR("%s: wrong lum magic %x: rc = %d\n",
2285 filename, lum->lum_magic, rc);
2286 GOTO(migrate_free, rc);
2289 rc = ll_migrate(inode, file, lum, filename, flags);
2291 OBD_FREE_LARGE(data, len);
2295 case LL_IOC_PCC_DETACH_BY_FID: {
2296 struct lu_pcc_detach_fid *detach;
2298 struct inode *inode2;
2301 OBD_ALLOC_PTR(detach);
2305 if (copy_from_user(detach, uarg, sizeof(*detach)))
2306 GOTO(out_detach, rc = -EFAULT);
2308 fid = &detach->pccd_fid;
2309 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2310 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2312 /* Target inode is not in inode cache, and PCC file
2313 * has aleady released, return immdiately.
2315 GOTO(out_detach, rc = 0);
2317 if (!S_ISREG(inode2->i_mode))
2318 GOTO(out_iput, rc = -EINVAL);
2320 if (!inode_owner_or_capable(&nop_mnt_idmap, inode2))
2321 GOTO(out_iput, rc = -EPERM);
2323 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2327 OBD_FREE_PTR(detach);
2331 rc = ll_iocontrol(inode, file, cmd, uarg);
2334 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL, uarg));
2338 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2340 struct inode *inode = file->f_mapping->host;
2341 struct ll_file_data *fd = file->private_data;
2342 struct ll_sb_info *sbi = ll_i2sbi(inode);
2343 int api32 = ll_need_32bit_api(sbi);
2344 loff_t ret = -EINVAL;
2347 ll_inode_lock(inode);
2352 offset += file->f_pos;
2358 offset += LL_DIR_END_OFF_32BIT;
2360 offset += LL_DIR_END_OFF;
2367 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2368 (!api32 && offset <= LL_DIR_END_OFF))) {
2369 if (offset != file->f_pos) {
2372 hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2373 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2374 (!api32 && offset == LL_DIR_END_OFF))
2375 fd->lfd_pos = MDS_DIR_END_OFF;
2376 else if (api32 && hash64)
2377 fd->lfd_pos = offset << 32;
2379 fd->lfd_pos = offset;
2380 file->f_pos = offset;
2381 file->f_version = 0;
2388 ll_inode_unlock(inode);
2392 static int ll_dir_open(struct inode *inode, struct file *file)
2395 RETURN(ll_file_open(inode, file));
2398 static int ll_dir_release(struct inode *inode, struct file *file)
2401 RETURN(ll_file_release(inode, file));
2404 /* notify error if partially read striped directory */
2405 static int ll_dir_flush(struct file *file, fl_owner_t id)
2407 struct ll_file_data *lfd = file->private_data;
2408 int rc = lfd->fd_partial_readdir_rc;
2410 lfd->fd_partial_readdir_rc = 0;
2415 const struct file_operations ll_dir_operations = {
2416 .llseek = ll_dir_seek,
2417 .open = ll_dir_open,
2418 .release = ll_dir_release,
2419 .read = generic_read_dir,
2420 #ifdef HAVE_DIR_CONTEXT
2421 .iterate_shared = ll_iterate,
2423 .readdir = ll_readdir,
2425 .unlocked_ioctl = ll_dir_ioctl,
2427 .flush = ll_dir_flush,