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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Directory code for lustre client.
38 #include <linux/pagemap.h>
40 #include <linux/version.h>
41 #include <linux/security.h>
42 #include <linux/user_namespace.h>
43 #ifdef HAVE_UIDGID_HEADER
44 # include <linux/uidgid.h>
46 #include <linux/uaccess.h>
47 #include <linux/buffer_head.h> // for wait_on_buffer
48 #include <linux/pagevec.h>
50 #define DEBUG_SUBSYSTEM S_LLITE
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include <lustre_lib.h>
56 #include <lustre_dlm.h>
57 #include <lustre_fid.h>
58 #include <lustre_kernelcomm.h>
59 #include <lustre_swab.h>
61 #include "llite_internal.h"
64 * (new) readdir implementation overview.
66 * Original lustre readdir implementation cached exact copy of raw directory
67 * pages on the client. These pages were indexed in client page cache by
68 * logical offset in the directory file. This design, while very simple and
69 * intuitive had some inherent problems:
71 * . it implies that byte offset to the directory entry serves as a
72 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
73 * ext3/htree directory entries may move due to splits, and more
76 * . it is incompatible with the design of split directories for cmd3,
77 * that assumes that names are distributed across nodes based on their
78 * hash, and so readdir should be done in hash order.
80 * New readdir implementation does readdir in hash order, and uses hash of a
81 * file name as a telldir/seekdir cookie. This led to number of complications:
83 * . hash is not unique, so it cannot be used to index cached directory
84 * pages on the client (note, that it requires a whole pageful of hash
85 * collided entries to cause two pages to have identical hashes);
87 * . hash is not unique, so it cannot, strictly speaking, be used as an
88 * entry cookie. ext3/htree has the same problem and lustre implementation
89 * mimics their solution: seekdir(hash) positions directory at the first
90 * entry with the given hash.
96 * Client caches directory pages using hash of the first entry as an index. As
97 * noted above hash is not unique, so this solution doesn't work as is:
98 * special processing is needed for "page hash chains" (i.e., sequences of
99 * pages filled with entries all having the same hash value).
101 * First, such chains have to be detected. To this end, server returns to the
102 * client the hash of the first entry on the page next to one returned. When
103 * client detects that this hash is the same as hash of the first entry on the
104 * returned page, page hash collision has to be handled. Pages in the
105 * hash chain, except first one, are termed "overflow pages".
107 * Solution to index uniqueness problem is to not cache overflow
108 * pages. Instead, when page hash collision is detected, all overflow pages
109 * from emerging chain are immediately requested from the server and placed in
110 * a special data structure (struct ll_dir_chain). This data structure is used
111 * by ll_readdir() to process entries from overflow pages. When readdir
112 * invocation finishes, overflow pages are discarded. If page hash collision
113 * chain weren't completely processed, next call to readdir will again detect
114 * page hash collision, again read overflow pages in, process next portion of
115 * entries and again discard the pages. This is not as wasteful as it looks,
116 * because, given reasonable hash, page hash collisions are extremely rare.
118 * 1. directory positioning
120 * When seekdir(hash) is called, original
131 * identification of and access to overflow pages
135 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
136 * a header lu_dirpage which describes the start/end hash, and whether this
137 * page is empty (contains no dir entry) or hash collide with next page.
138 * After client receives reply, several pages will be integrated into dir page
139 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
140 * lu_dirpage for this integrated page will be adjusted. See
141 * mdc_adjust_dirpages().
144 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
145 __u64 offset, struct ll_dir_chain *chain)
147 struct md_callback cb_op;
151 cb_op.md_blocking_ast = ll_md_blocking_ast;
152 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
159 void ll_release_page(struct inode *inode, struct page *page,
164 /* Always remove the page for striped dir, because the page is
165 * built from temporarily in LMV layer */
166 if (inode != NULL && S_ISDIR(inode->i_mode) &&
167 ll_i2info(inode)->lli_lsm_md != NULL) {
174 if (likely(page->mapping != NULL))
175 truncate_complete_page(page->mapping, page);
182 * return IF_* type for given lu_dirent entry.
183 * IF_* flag shld be converted to particular OS file type in
184 * platform llite module.
186 static u16 ll_dirent_type_get(struct lu_dirent *ent)
189 struct luda_type *lt;
192 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
193 const unsigned align = sizeof(struct luda_type) - 1;
195 len = le16_to_cpu(ent->lde_namelen);
196 len = (len + align) & ~align;
197 lt = (void *)ent->lde_name + len;
198 type = IFTODT(le16_to_cpu(lt->lt_type));
204 #ifdef HAVE_DIR_CONTEXT
205 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
206 struct dir_context *ctx)
209 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
210 void *cookie, filldir_t filldir)
213 struct ll_sb_info *sbi = ll_i2sbi(inode);
215 bool is_api32 = ll_need_32bit_api(sbi);
216 bool is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
218 struct ll_dir_chain chain;
223 ll_dir_chain_init(&chain);
225 page = ll_get_dir_page(inode, op_data, pos, &chain);
227 while (rc == 0 && !done) {
228 struct lu_dirpage *dp;
229 struct lu_dirent *ent;
238 hash = MDS_DIR_END_OFF;
239 dp = page_address(page);
240 for (ent = lu_dirent_start(dp); ent != NULL && !done;
241 ent = lu_dirent_next(ent)) {
248 hash = le64_to_cpu(ent->lde_hash);
249 if (hash < pos) /* Skip until we find target hash */
252 namelen = le16_to_cpu(ent->lde_namelen);
253 if (namelen == 0) /* Skip dummy record */
256 if (is_api32 && is_hash64)
260 fid_le_to_cpu(&fid, &ent->lde_fid);
261 ino = cl_fid_build_ino(&fid, is_api32);
262 type = ll_dirent_type_get(ent);
263 /* For ll_nfs_get_name_filldir(), it will try to access
264 * 'ent' through 'lde_name', so the parameter 'name'
265 * for 'filldir()' must be part of the 'ent'. */
266 #ifdef HAVE_DIR_CONTEXT
268 done = !dir_emit(ctx, ent->lde_name, namelen, ino,
271 done = filldir(cookie, ent->lde_name, namelen, lhash,
278 ll_release_page(inode, page, false);
282 next = le64_to_cpu(dp->ldp_hash_end);
284 if (pos == MDS_DIR_END_OFF) {
286 * End of directory reached.
289 ll_release_page(inode, page, false);
292 * Normal case: continue to the next
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 ll_dir_chain_fini(&chain);
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 = LUSTRE_FPRIVATE(filp);
320 struct ll_sb_info *sbi = ll_i2sbi(inode);
321 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
322 int api32 = ll_need_32bit_api(sbi);
323 struct md_op_data *op_data;
324 struct lu_fid pfid = { 0 };
334 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
335 "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
336 inode, (unsigned long)pos, i_size_read(inode), api32);
338 if (pos == MDS_DIR_END_OFF)
344 if (unlikely(ll_i2info(inode)->lli_lsm_md != NULL)) {
346 * This is only needed for striped dir to fill ..,
347 * see lmv_read_page()
349 if (file_dentry(filp)->d_parent != NULL &&
350 file_dentry(filp)->d_parent->d_inode != NULL) {
351 __u64 ibits = MDS_INODELOCK_LOOKUP;
352 struct inode *parent =
353 file_dentry(filp)->d_parent->d_inode;
355 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
356 pfid = *ll_inode2fid(parent);
359 /* If it can not find in cache, do lookup .. on the master
361 if (fid_is_zero(&pfid)) {
362 rc = ll_dir_get_parent_fid(inode, &pfid);
368 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
369 LUSTRE_OPC_ANY, inode);
371 GOTO(out, rc = PTR_ERR(op_data));
372 op_data->op_fid3 = pfid;
374 #ifdef HAVE_DIR_CONTEXT
376 rc = ll_dir_read(inode, &pos, op_data, ctx);
379 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
384 if (pos == MDS_DIR_END_OFF) {
386 pos = LL_DIR_END_OFF_32BIT;
388 pos = LL_DIR_END_OFF;
393 #ifdef HAVE_DIR_CONTEXT
398 ll_finish_md_op_data(op_data);
402 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
407 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
408 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
410 struct mgs_send_param *msp;
417 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
418 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
419 sizeof(struct mgs_send_param), msp, NULL);
421 CERROR("Failed to set parameter: %d\n", rc);
429 * Create striped directory with specified stripe(@lump)
431 * \param[in] dparent the parent of the directory.
432 * \param[in] lump the specified stripes.
433 * \param[in] dirname the name of the directory.
434 * \param[in] mode the specified mode of the directory.
436 * \retval =0 if striped directory is being created successfully.
437 * <0 if the creation is failed.
439 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
440 size_t len, const char *dirname, umode_t mode)
442 struct inode *parent = dparent->d_inode;
443 struct ptlrpc_request *request = NULL;
444 struct md_op_data *op_data;
445 struct ll_sb_info *sbi = ll_i2sbi(parent);
446 struct inode *inode = NULL;
447 struct dentry dentry = {
451 .len = strlen(dirname),
452 .hash = ll_full_name_hash(dparent, dirname,
459 if (unlikely(lump->lum_magic != LMV_USER_MAGIC &&
460 lump->lum_magic != LMV_USER_MAGIC_SPECIFIC))
463 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) name %s "
464 "stripe_offset %d, stripe_count: %u\n",
465 PFID(ll_inode2fid(parent)), parent, dirname,
466 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
468 if (lump->lum_stripe_count > 1 &&
469 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
472 if (IS_DEADDIR(parent) &&
473 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
476 if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
477 lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
478 lustre_swab_lmv_user_md(lump);
480 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
481 mode &= ~current_umask();
482 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
483 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
484 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
487 RETURN(PTR_ERR(op_data));
489 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
490 /* selinux_dentry_init_security() uses dentry->d_parent and name
491 * to determine the security context for the file. So our fake
492 * dentry should be real enough for this purpose. */
493 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
494 &op_data->op_file_secctx_name,
495 &op_data->op_file_secctx,
496 &op_data->op_file_secctx_size);
498 GOTO(out_op_data, err);
501 op_data->op_cli_flags |= CLI_SET_MEA;
502 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
503 from_kuid(&init_user_ns, current_fsuid()),
504 from_kgid(&init_user_ns, current_fsgid()),
505 cfs_curproc_cap_pack(), 0, &request);
507 GOTO(out_request, err);
509 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
511 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
513 GOTO(out_inode, err);
515 dentry.d_inode = inode;
517 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
519 err = security_inode_notifysecctx(inode,
520 op_data->op_file_secctx,
521 op_data->op_file_secctx_size);
524 err = ll_inode_init_security(&dentry, inode, parent);
527 GOTO(out_inode, err);
533 ptlrpc_req_finished(request);
535 ll_finish_md_op_data(op_data);
540 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
543 struct ll_sb_info *sbi = ll_i2sbi(inode);
544 struct md_op_data *op_data;
545 struct ptlrpc_request *req = NULL;
547 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
548 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
549 struct obd_device *mgc = lsi->lsi_mgc;
555 switch (lump->lmm_magic) {
556 case LOV_USER_MAGIC_V1:
557 lum_size = sizeof(struct lov_user_md_v1);
559 case LOV_USER_MAGIC_V3:
560 lum_size = sizeof(struct lov_user_md_v3);
562 case LOV_USER_MAGIC_COMP_V1:
563 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
566 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
567 lustre_swab_lmv_user_md(
568 (struct lmv_user_md *)lump);
569 lum_size = sizeof(struct lmv_user_md);
571 case LOV_USER_MAGIC_SPECIFIC: {
572 struct lov_user_md_v3 *v3 =
573 (struct lov_user_md_v3 *)lump;
574 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
576 lum_size = lov_user_md_size(v3->lmm_stripe_count,
577 LOV_USER_MAGIC_SPECIFIC);
581 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
582 " %#08x != %#08x nor %#08x\n",
583 lump->lmm_magic, LOV_USER_MAGIC_V1,
589 * This is coming from userspace, so should be in
590 * local endian. But the MDS would like it in little
591 * endian, so we swab it before we send it.
593 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
594 le32_to_cpu(LOV_MAGIC_MAGIC))
595 lustre_swab_lov_user_md(lump, 0);
597 lum_size = sizeof(struct lov_user_md_v1);
600 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
601 LUSTRE_OPC_ANY, NULL);
603 RETURN(PTR_ERR(op_data));
605 /* swabbing is done in lov_setstripe() on server side */
606 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
607 ll_finish_md_op_data(op_data);
608 ptlrpc_req_finished(req);
612 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
614 * 2.9 server has stored filesystem default stripe in ROOT xattr,
615 * and it's stored into system config for backward compatibility.
617 * In the following we use the fact that LOV_USER_MAGIC_V1 and
618 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
619 * need the make the distiction between the 2 versions
621 if (set_default && mgc->u.cli.cl_mgc_mgsexp &&
623 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V1 ||
624 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V3)) {
628 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
630 GOTO(end, rc = -ENOMEM);
633 /* Get fsname and assume devname to be -MDT0000. */
634 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
635 strcat(buf, "-MDT0000.lov");
638 /* Set root stripesize */
639 snprintf(buf, MGS_PARAM_MAXLEN, ".stripesize=%u",
640 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
641 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
645 /* Set root stripecount */
646 snprintf(buf, MGS_PARAM_MAXLEN, ".stripecount=%hd",
647 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
648 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
652 /* Set root stripeoffset */
653 snprintf(buf, MGS_PARAM_MAXLEN, ".stripeoffset=%hd",
654 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
655 (typeof(lump->lmm_stripe_offset))(-1));
656 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
660 OBD_FREE(param, MGS_PARAM_MAXLEN);
666 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
668 struct ptlrpc_request **request, u64 valid,
669 enum get_default_layout_type type)
671 struct ll_sb_info *sbi = ll_i2sbi(inode);
672 struct mdt_body *body;
673 struct lov_mds_md *lmm = NULL;
674 struct ptlrpc_request *req = NULL;
676 struct md_op_data *op_data;
680 rc = ll_get_default_mdsize(sbi, &lmm_size);
684 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
685 0, lmm_size, LUSTRE_OPC_ANY,
688 RETURN(PTR_ERR(op_data));
690 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
692 if (type == GET_DEFAULT_LAYOUT_ROOT) {
693 lu_root_fid(&op_data->op_fid1);
694 fid = op_data->op_fid1;
696 fid = *ll_inode2fid(inode);
699 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
700 ll_finish_md_op_data(op_data);
702 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
707 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
708 LASSERT(body != NULL);
710 lmm_size = body->mbo_eadatasize;
712 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
714 GOTO(out, rc = -ENODATA);
717 lmm = req_capsule_server_sized_get(&req->rq_pill,
718 &RMF_MDT_MD, lmm_size);
719 LASSERT(lmm != NULL);
722 * This is coming from the MDS, so is probably in
723 * little endian. We convert it to host endian before
724 * passing it to userspace.
726 /* We don't swab objects for directories */
727 switch (le32_to_cpu(lmm->lmm_magic)) {
730 case LOV_MAGIC_COMP_V1:
731 case LOV_USER_MAGIC_SPECIFIC:
732 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
733 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
736 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
737 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
740 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
741 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
744 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
749 *plmm_size = lmm_size;
755 * This function will be used to get default LOV/LMV/Default LMV
756 * @valid will be used to indicate which stripe it will retrieve.
757 * If the directory does not have its own default layout, then the
758 * function will request the default layout from root FID.
759 * OBD_MD_MEA LMV stripe EA
760 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
761 * otherwise Default LOV EA.
762 * Each time, it can only retrieve 1 stripe EA
764 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
765 struct ptlrpc_request **request,
766 struct ptlrpc_request **root_request,
769 struct ptlrpc_request *req = NULL;
770 struct ptlrpc_request *root_req = NULL;
771 struct lov_mds_md *lmm = NULL;
776 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
778 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
779 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL){
780 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
781 &lmm_size, &root_req, valid,
782 GET_DEFAULT_LAYOUT_ROOT);
788 *plmm_size = lmm_size;
790 if (root_request != NULL)
791 *root_request = root_req;
797 * This function will be used to get default LOV/LMV/Default LMV
798 * @valid will be used to indicate which stripe it will retrieve
799 * OBD_MD_MEA LMV stripe EA
800 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
801 * otherwise Default LOV EA.
802 * Each time, it can only retrieve 1 stripe EA
804 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
805 struct ptlrpc_request **request, u64 valid)
807 struct ptlrpc_request *req = NULL;
808 struct lov_mds_md *lmm = NULL;
813 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
817 *plmm_size = lmm_size;
823 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
825 struct md_op_data *op_data;
830 OBD_ALLOC_PTR(op_data);
834 op_data->op_flags |= MF_GET_MDT_IDX;
835 op_data->op_fid1 = *fid;
836 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
837 mdt_index = op_data->op_mds;
838 OBD_FREE_PTR(op_data);
846 * Get MDT index for the inode.
848 int ll_get_mdt_idx(struct inode *inode)
850 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
854 * Generic handler to do any pre-copy work.
856 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
857 * first information for it that real work has started.
859 * Moreover, for a ARCHIVE request, it will sample the file data version and
860 * store it in \a copy.
862 * \return 0 on success.
864 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
866 struct ll_sb_info *sbi = ll_s2sbi(sb);
867 struct hsm_progress_kernel hpk;
872 /* Forge a hsm_progress based on data from copy. */
873 hpk.hpk_fid = copy->hc_hai.hai_fid;
874 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
875 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
876 hpk.hpk_extent.length = 0;
879 hpk.hpk_data_version = 0;
882 /* For archive request, we need to read the current file version. */
883 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
885 __u64 data_version = 0;
887 /* Get inode for this fid */
888 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
890 hpk.hpk_flags |= HP_FLAG_RETRY;
891 /* hpk_errval is >= 0 */
892 hpk.hpk_errval = -PTR_ERR(inode);
893 GOTO(progress, rc = PTR_ERR(inode));
896 /* Read current file data version */
897 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
900 CDEBUG(D_HSM, "Could not read file data version of "
901 DFID" (rc = %d). Archive request ("
902 "%#llx) could not be done.\n",
903 PFID(©->hc_hai.hai_fid), rc,
904 copy->hc_hai.hai_cookie);
905 hpk.hpk_flags |= HP_FLAG_RETRY;
906 /* hpk_errval must be >= 0 */
907 hpk.hpk_errval = -rc;
911 /* Store in the hsm_copy for later copytool use.
912 * Always modified even if no lsm. */
913 copy->hc_data_version = data_version;
917 /* On error, the request should be considered as completed */
918 if (hpk.hpk_errval > 0)
919 hpk.hpk_flags |= HP_FLAG_COMPLETED;
921 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
924 /* Return first error */
925 RETURN(rc != 0 ? rc : rc2);
929 * Generic handler to do any post-copy work.
931 * It will send the last hsm_progress update to coordinator to inform it
932 * that copy is finished and whether it was successful or not.
935 * - for ARCHIVE request, it will sample the file data version and compare it
936 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
937 * will be considered as failed.
938 * - for RESTORE request, it will sample the file data version and send it to
939 * coordinator which is useful if the file was imported as 'released'.
941 * \return 0 on success.
943 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
945 struct ll_sb_info *sbi = ll_s2sbi(sb);
946 struct hsm_progress_kernel hpk;
951 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
952 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
953 * initialized if copy_end was called with copy == NULL.
956 /* Forge a hsm_progress based on data from copy. */
957 hpk.hpk_fid = copy->hc_hai.hai_fid;
958 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
959 hpk.hpk_extent = copy->hc_hai.hai_extent;
960 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
961 hpk.hpk_errval = copy->hc_errval;
962 hpk.hpk_data_version = 0;
964 /* For archive request, we need to check the file data was not changed.
966 * For restore request, we need to send the file data version, this is
967 * useful when the file was created using hsm_import.
969 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
970 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
971 (copy->hc_errval == 0)) {
973 __u64 data_version = 0;
975 /* Get lsm for this fid */
976 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
978 hpk.hpk_flags |= HP_FLAG_RETRY;
979 /* hpk_errval must be >= 0 */
980 hpk.hpk_errval = -PTR_ERR(inode);
981 GOTO(progress, rc = PTR_ERR(inode));
984 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
987 CDEBUG(D_HSM, "Could not read file data version. "
988 "Request could not be confirmed.\n");
989 if (hpk.hpk_errval == 0)
990 hpk.hpk_errval = -rc;
994 /* Store in the hsm_copy for later copytool use.
995 * Always modified even if no lsm. */
996 hpk.hpk_data_version = data_version;
998 /* File could have been stripped during archiving, so we need
999 * to check anyway. */
1000 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1001 (copy->hc_data_version != data_version)) {
1002 CDEBUG(D_HSM, "File data version mismatched. "
1003 "File content was changed during archiving. "
1004 DFID", start:%#llx current:%#llx\n",
1005 PFID(©->hc_hai.hai_fid),
1006 copy->hc_data_version, data_version);
1007 /* File was changed, send error to cdt. Do not ask for
1008 * retry because if a file is modified frequently,
1009 * the cdt will loop on retried archive requests.
1010 * The policy engine will ask for a new archive later
1011 * when the file will not be modified for some tunable
1013 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1015 /* hpk_errval must be >= 0 */
1016 hpk.hpk_errval = -rc;
1023 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1026 /* Return first error */
1027 RETURN(rc != 0 ? rc : rc2);
1031 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1032 const struct lustre_kernelcomm __user *data)
1034 struct lustre_kernelcomm *lk;
1035 struct lustre_kernelcomm *tmp;
1036 size_t size = sizeof(*lk);
1041 /* copy data from userspace to get numbers of archive_id */
1042 OBD_ALLOC(lk, size);
1046 if (copy_from_user(lk, data, size))
1047 GOTO(out_lk, rc = -EFAULT);
1049 if (lk->lk_flags & LK_FLG_STOP)
1052 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1053 __u32 archive_mask = lk->lk_data_count;
1056 /* old hsm agent to old MDS */
1057 if (!exp_connect_archive_id_array(exp))
1060 /* old hsm agent to new MDS */
1061 lk->lk_flags |= LK_FLG_DATANR;
1063 if (archive_mask == 0)
1066 count = hweight32(archive_mask);
1067 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1068 OBD_ALLOC(tmp, new_size);
1070 GOTO(out_lk, rc = -ENOMEM);
1072 memcpy(tmp, lk, size);
1073 tmp->lk_data_count = count;
1079 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1080 if ((1 << i) & archive_mask) {
1081 lk->lk_data[count] = i + 1;
1088 /* new hsm agent to new mds */
1089 if (lk->lk_data_count > 0) {
1090 new_size = offsetof(struct lustre_kernelcomm,
1091 lk_data[lk->lk_data_count]);
1092 OBD_ALLOC(tmp, new_size);
1094 GOTO(out_lk, rc = -ENOMEM);
1100 if (copy_from_user(lk, data, size))
1101 GOTO(out_lk, rc = -EFAULT);
1104 /* new hsm agent to old MDS */
1105 if (!exp_connect_archive_id_array(exp)) {
1108 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1109 GOTO(out_lk, rc = -EINVAL);
1111 for (i = 0; i < lk->lk_data_count; i++) {
1112 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1114 CERROR("%s: archive id %d requested but only "
1115 "[0 - %zu] supported: rc = %d\n",
1116 exp->exp_obd->obd_name, lk->lk_data[i],
1117 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1121 if (lk->lk_data[i] == 0) {
1126 archives |= (1 << (lk->lk_data[i] - 1));
1128 lk->lk_flags &= ~LK_FLG_DATANR;
1129 lk->lk_data_count = archives;
1132 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1138 static int check_owner(int type, int id)
1142 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1146 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1155 static int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1157 struct ll_sb_info *sbi = ll_s2sbi(sb);
1158 int cmd = qctl->qc_cmd;
1159 int type = qctl->qc_type;
1160 int id = qctl->qc_id;
1161 int valid = qctl->qc_valid;
1168 case LUSTRE_Q_SETDEFAULT:
1169 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1172 if (sb->s_flags & SB_RDONLY)
1176 case LUSTRE_Q_GETDEFAULT:
1177 if (check_owner(type, id) &&
1178 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1184 CERROR("unsupported quotactl op: %#x\n", cmd);
1188 if (valid != QC_GENERAL) {
1189 if (cmd == Q_GETINFO)
1190 qctl->qc_cmd = Q_GETOINFO;
1191 else if (cmd == Q_GETQUOTA)
1192 qctl->qc_cmd = Q_GETOQUOTA;
1198 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1199 sizeof(*qctl), qctl, NULL);
1202 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1203 sizeof(*qctl), qctl, NULL);
1206 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1207 sizeof(*qctl), qctl, NULL);
1209 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1211 sizeof(*qctl), qctl, NULL);
1223 struct obd_quotactl *oqctl;
1225 OBD_ALLOC_PTR(oqctl);
1229 QCTL_COPY(oqctl, qctl);
1230 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1232 OBD_FREE_PTR(oqctl);
1235 /* If QIF_SPACE is not set, client should collect the
1236 * space usage from OSSs by itself */
1237 if (cmd == Q_GETQUOTA &&
1238 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1239 !oqctl->qc_dqblk.dqb_curspace) {
1240 struct obd_quotactl *oqctl_tmp;
1242 OBD_ALLOC_PTR(oqctl_tmp);
1243 if (oqctl_tmp == NULL)
1244 GOTO(out, rc = -ENOMEM);
1246 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1247 oqctl_tmp->qc_id = oqctl->qc_id;
1248 oqctl_tmp->qc_type = oqctl->qc_type;
1250 /* collect space usage from OSTs */
1251 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1252 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1253 if (!rc || rc == -EREMOTEIO) {
1254 oqctl->qc_dqblk.dqb_curspace =
1255 oqctl_tmp->qc_dqblk.dqb_curspace;
1256 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1259 /* collect space & inode usage from MDTs */
1260 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1261 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1262 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1263 if (!rc || rc == -EREMOTEIO) {
1264 oqctl->qc_dqblk.dqb_curspace +=
1265 oqctl_tmp->qc_dqblk.dqb_curspace;
1266 oqctl->qc_dqblk.dqb_curinodes =
1267 oqctl_tmp->qc_dqblk.dqb_curinodes;
1268 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1270 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1273 OBD_FREE_PTR(oqctl_tmp);
1276 QCTL_COPY(qctl, oqctl);
1277 OBD_FREE_PTR(oqctl);
1283 int ll_rmfid(struct file *file, void __user *arg)
1285 const struct fid_array __user *ufa = arg;
1286 struct fid_array *lfa = NULL;
1289 int i, rc, *rcs = NULL;
1292 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1293 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1295 /* Only need to get the buflen */
1296 if (get_user(nr, &ufa->fa_nr))
1298 /* DoS protection */
1299 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1302 size = offsetof(struct fid_array, fa_fids[nr]);
1303 OBD_ALLOC(lfa, size);
1306 OBD_ALLOC(rcs, sizeof(int) * nr);
1308 GOTO(free_lfa, rc = -ENOMEM);
1310 if (copy_from_user(lfa, arg, size))
1311 GOTO(free_rcs, rc = -EFAULT);
1313 /* Call mdc_iocontrol */
1314 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1316 for (i = 0; i < nr; i++)
1318 lfa->fa_fids[i].f_ver = rcs[i];
1319 if (copy_to_user(arg, lfa, size))
1324 OBD_FREE(rcs, sizeof(int) * nr);
1326 OBD_FREE(lfa, size);
1331 /* This function tries to get a single name component,
1332 * to send to the server. No actual path traversal involved,
1333 * so we limit to NAME_MAX */
1334 static char *ll_getname(const char __user *filename)
1339 OBD_ALLOC(tmp, NAME_MAX + 1);
1342 return ERR_PTR(-ENOMEM);
1344 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1347 else if (len > NAME_MAX)
1348 ret = -ENAMETOOLONG;
1351 OBD_FREE(tmp, NAME_MAX + 1);
1357 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1359 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1361 struct dentry *dentry = file_dentry(file);
1362 struct inode *inode = file_inode(file);
1363 struct ll_sb_info *sbi = ll_i2sbi(inode);
1364 struct obd_ioctl_data *data;
1368 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1369 PFID(ll_inode2fid(inode)), inode, cmd);
1371 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1372 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1375 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1377 case FS_IOC_GETFLAGS:
1378 case FS_IOC_SETFLAGS:
1379 RETURN(ll_iocontrol(inode, file, cmd, arg));
1380 case FSFILT_IOC_GETVERSION:
1381 case FS_IOC_GETVERSION:
1382 RETURN(put_user(inode->i_generation, (int __user *)arg));
1383 /* We need to special case any other ioctls we want to handle,
1384 * to send them to the MDS/OST as appropriate and to properly
1385 * network encode the arg field. */
1386 case FS_IOC_SETVERSION:
1389 case LL_IOC_GET_MDTIDX: {
1392 mdtidx = ll_get_mdt_idx(inode);
1396 if (put_user((int)mdtidx, (int __user *)arg))
1401 case IOC_MDC_LOOKUP: {
1402 int namelen, len = 0;
1406 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1411 filename = data->ioc_inlbuf1;
1412 namelen = strlen(filename);
1414 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1415 GOTO(out_free, rc = -EINVAL);
1418 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1420 CERROR("%s: lookup %.*s failed: rc = %d\n",
1421 ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1426 OBD_FREE_LARGE(buf, len);
1429 case LL_IOC_LMV_SETSTRIPE: {
1430 struct lmv_user_md *lum;
1439 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1444 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1445 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1446 GOTO(lmv_out_free, rc = -EINVAL);
1448 filename = data->ioc_inlbuf1;
1449 namelen = data->ioc_inllen1;
1452 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1453 GOTO(lmv_out_free, rc = -EINVAL);
1455 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1456 lumlen = data->ioc_inllen2;
1458 if ((lum->lum_magic != LMV_USER_MAGIC &&
1459 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) ||
1460 lumlen < sizeof(*lum)) {
1461 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1462 filename, lum->lum_magic, lumlen, -EFAULT);
1463 GOTO(lmv_out_free, rc = -EINVAL);
1466 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1467 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1469 mode = data->ioc_type;
1471 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1473 OBD_FREE_LARGE(buf, len);
1477 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1478 struct lmv_user_md lum;
1479 struct lmv_user_md __user *ulump =
1480 (struct lmv_user_md __user *)arg;
1483 if (copy_from_user(&lum, ulump, sizeof(lum)))
1486 if (lum.lum_magic != LMV_USER_MAGIC)
1489 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1493 case LL_IOC_LOV_SETSTRIPE_NEW:
1494 case LL_IOC_LOV_SETSTRIPE: {
1495 struct lov_user_md_v3 *lumv3 = NULL;
1496 struct lov_user_md_v1 lumv1;
1497 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1498 struct lov_user_md_v1 __user *lumv1p =
1499 (struct lov_user_md_v1 __user *)arg;
1500 struct lov_user_md_v3 __user *lumv3p =
1501 (struct lov_user_md_v3 __user *)arg;
1504 int set_default = 0;
1506 CLASSERT(sizeof(struct lov_user_md_v3) >
1507 sizeof(struct lov_comp_md_v1));
1508 CLASSERT(sizeof(*lumv3) == sizeof(*lumv3p));
1509 /* first try with v1 which is smaller than v3 */
1510 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1513 if (inode->i_sb->s_root == file_dentry(file))
1516 switch (lumv1.lmm_magic) {
1517 case LOV_USER_MAGIC_V3:
1518 case LOV_USER_MAGIC_SPECIFIC:
1519 lum_size = ll_lov_user_md_size(&lumv1);
1522 OBD_ALLOC(lumv3, lum_size);
1525 if (copy_from_user(lumv3, lumv3p, lum_size))
1526 GOTO(out, rc = -EFAULT);
1527 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1529 case LOV_USER_MAGIC_V1:
1532 GOTO(out, rc = -ENOTSUPP);
1535 /* in v1 and v3 cases lumv1 points to data */
1536 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1539 OBD_FREE(lumv3, lum_size);
1542 case LL_IOC_LMV_GETSTRIPE: {
1543 struct lmv_user_md __user *ulmv =
1544 (struct lmv_user_md __user *)arg;
1545 struct lmv_user_md lum;
1546 struct ptlrpc_request *request = NULL;
1547 struct ptlrpc_request *root_request = NULL;
1548 union lmv_mds_md *lmm = NULL;
1551 struct lmv_user_md *tmp = NULL;
1555 int max_stripe_count;
1559 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1562 max_stripe_count = lum.lum_stripe_count;
1563 /* lum_magic will indicate which stripe the ioctl will like
1564 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1565 * is for default LMV stripe */
1566 if (lum.lum_magic == LMV_MAGIC_V1)
1567 valid |= OBD_MD_MEA;
1568 else if (lum.lum_magic == LMV_USER_MAGIC)
1569 valid |= OBD_MD_DEFAULT_MEA;
1573 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1574 &request, &root_request, valid);
1576 GOTO(finish_req, rc);
1578 /* Get default LMV EA */
1579 if (lum.lum_magic == LMV_USER_MAGIC) {
1580 if (lmmsize > sizeof(*ulmv))
1581 GOTO(finish_req, rc = -EINVAL);
1583 if (copy_to_user(ulmv, lmm, lmmsize))
1584 GOTO(finish_req, rc = -EFAULT);
1586 GOTO(finish_req, rc);
1589 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1590 if (max_stripe_count < stripe_count) {
1591 lum.lum_stripe_count = stripe_count;
1592 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1593 GOTO(finish_req, rc = -EFAULT);
1594 GOTO(finish_req, rc = -E2BIG);
1597 lum_size = lmv_user_md_size(stripe_count,
1598 LMV_USER_MAGIC_SPECIFIC);
1599 OBD_ALLOC(tmp, lum_size);
1601 GOTO(finish_req, rc = -ENOMEM);
1603 mdt_index = ll_get_mdt_idx(inode);
1605 GOTO(out_tmp, rc = -ENOMEM);
1607 tmp->lum_magic = LMV_MAGIC_V1;
1608 tmp->lum_stripe_count = 0;
1609 tmp->lum_stripe_offset = mdt_index;
1610 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1611 for (i = 0; i < stripe_count; i++) {
1614 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1615 if (fid_is_sane(&fid)) {
1616 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1618 GOTO(out_tmp, rc = mdt_index);
1620 tmp->lum_objects[i].lum_mds = mdt_index;
1621 tmp->lum_objects[i].lum_fid = fid;
1624 tmp->lum_stripe_count++;
1627 if (copy_to_user(ulmv, tmp, lum_size))
1628 GOTO(out_tmp, rc = -EFAULT);
1630 OBD_FREE(tmp, lum_size);
1632 ptlrpc_req_finished(request);
1633 ptlrpc_req_finished(root_request);
1637 case LL_IOC_REMOVE_ENTRY: {
1638 char *filename = NULL;
1642 /* Here is a little hack to avoid sending REINT_RMENTRY to
1643 * unsupported server, which might crash the server(LU-2730),
1644 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1645 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1646 * server will support REINT_RMENTRY XXX*/
1647 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1650 filename = ll_getname((const char __user *)arg);
1651 if (IS_ERR(filename))
1652 RETURN(PTR_ERR(filename));
1654 namelen = strlen(filename);
1656 GOTO(out_rmdir, rc = -EINVAL);
1658 rc = ll_rmdir_entry(inode, filename, namelen);
1661 ll_putname(filename);
1665 RETURN(ll_rmfid(file, (void __user *)arg));
1666 case LL_IOC_LOV_SWAP_LAYOUTS:
1668 case IOC_OBD_STATFS:
1669 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1670 case LL_IOC_LOV_GETSTRIPE:
1671 case LL_IOC_LOV_GETSTRIPE_NEW:
1672 case LL_IOC_MDC_GETINFO:
1673 case LL_IOC_MDC_GETINFO_OLD:
1674 case IOC_MDC_GETFILEINFO:
1675 case IOC_MDC_GETFILEINFO_OLD:
1676 case IOC_MDC_GETFILESTRIPE: {
1677 struct ptlrpc_request *request = NULL;
1678 struct ptlrpc_request *root_request = NULL;
1679 struct lov_user_md __user *lump;
1680 struct lov_mds_md *lmm = NULL;
1681 struct mdt_body *body;
1682 char *filename = NULL;
1683 lstat_t __user *statp = NULL;
1684 lstatx_t __user *stxp = NULL;
1685 __u64 __user *flagsp = NULL;
1686 __u32 __user *lmmsizep = NULL;
1687 struct lu_fid __user *fidp = NULL;
1690 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1691 cmd == IOC_MDC_GETFILEINFO ||
1692 cmd == IOC_MDC_GETFILESTRIPE) {
1693 filename = ll_getname((const char __user *)arg);
1694 if (IS_ERR(filename))
1695 RETURN(PTR_ERR(filename));
1697 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1698 &lmmsize, &request);
1700 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1706 body = req_capsule_server_get(&request->rq_pill,
1708 LASSERT(body != NULL);
1713 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1714 cmd == LL_IOC_MDC_GETINFO ||
1715 cmd == IOC_MDC_GETFILEINFO_OLD ||
1716 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1724 if (cmd == IOC_MDC_GETFILESTRIPE ||
1725 cmd == LL_IOC_LOV_GETSTRIPE ||
1726 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1727 lump = (struct lov_user_md __user *)arg;
1728 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1729 cmd == LL_IOC_MDC_GETINFO_OLD){
1730 struct lov_user_mds_data_v1 __user *lmdp;
1732 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1733 statp = &lmdp->lmd_st;
1734 lump = &lmdp->lmd_lmm;
1736 struct lov_user_mds_data __user *lmdp;
1738 lmdp = (struct lov_user_mds_data __user *)arg;
1739 fidp = &lmdp->lmd_fid;
1740 stxp = &lmdp->lmd_stx;
1741 flagsp = &lmdp->lmd_flags;
1742 lmmsizep = &lmdp->lmd_lmmsize;
1743 lump = &lmdp->lmd_lmm;
1747 /* If the file has no striping then zero out *lump so
1748 * that the caller isn't confused by garbage. */
1749 if (clear_user(lump, sizeof(*lump)))
1750 GOTO(out_req, rc = -EFAULT);
1751 } else if (copy_to_user(lump, lmm, lmmsize)) {
1752 if (copy_to_user(lump, lmm, sizeof(*lump)))
1753 GOTO(out_req, rc = -EFAULT);
1757 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1758 cmd == LL_IOC_MDC_GETINFO_OLD) {
1761 st.st_dev = inode->i_sb->s_dev;
1762 st.st_mode = body->mbo_mode;
1763 st.st_nlink = body->mbo_nlink;
1764 st.st_uid = body->mbo_uid;
1765 st.st_gid = body->mbo_gid;
1766 st.st_rdev = body->mbo_rdev;
1767 st.st_size = body->mbo_size;
1768 st.st_blksize = PAGE_SIZE;
1769 st.st_blocks = body->mbo_blocks;
1770 st.st_atime = body->mbo_atime;
1771 st.st_mtime = body->mbo_mtime;
1772 st.st_ctime = body->mbo_ctime;
1773 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1777 if (copy_to_user(statp, &st, sizeof(st)))
1778 GOTO(out_req, rc = -EFAULT);
1779 } else if (cmd == IOC_MDC_GETFILEINFO ||
1780 cmd == LL_IOC_MDC_GETINFO) {
1781 lstatx_t stx = { 0 };
1782 __u64 valid = body->mbo_valid;
1784 stx.stx_blksize = PAGE_SIZE;
1785 stx.stx_nlink = body->mbo_nlink;
1786 stx.stx_uid = body->mbo_uid;
1787 stx.stx_gid = body->mbo_gid;
1788 stx.stx_mode = body->mbo_mode;
1789 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1792 stx.stx_size = body->mbo_size;
1793 stx.stx_blocks = body->mbo_blocks;
1794 stx.stx_atime.tv_sec = body->mbo_atime;
1795 stx.stx_ctime.tv_sec = body->mbo_ctime;
1796 stx.stx_mtime.tv_sec = body->mbo_mtime;
1797 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1798 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1799 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1800 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1801 stx.stx_mask |= STATX_BASIC_STATS;
1804 * For a striped directory, the size and blocks returned
1805 * from MDT is not correct.
1806 * The size and blocks are aggregated by client across
1808 * Thus for a striped directory, do not return the valid
1809 * FLSIZE and FLBLOCKS flags to the caller.
1810 * However, this whould be better decided by the MDS
1811 * instead of the client.
1813 if (cmd == LL_IOC_MDC_GETINFO &&
1814 ll_i2info(inode)->lli_lsm_md != NULL)
1815 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1817 if (flagsp && copy_to_user(flagsp, &valid,
1819 GOTO(out_req, rc = -EFAULT);
1821 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1823 GOTO(out_req, rc = -EFAULT);
1825 if (!(valid & OBD_MD_FLSIZE))
1826 stx.stx_mask &= ~STATX_SIZE;
1827 if (!(valid & OBD_MD_FLBLOCKS))
1828 stx.stx_mask &= ~STATX_BLOCKS;
1830 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1831 GOTO(out_req, rc = -EFAULT);
1833 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1835 GOTO(out_req, rc = -EFAULT);
1840 ptlrpc_req_finished(request);
1841 ptlrpc_req_finished(root_request);
1843 ll_putname(filename);
1846 case OBD_IOC_QUOTACTL: {
1847 struct if_quotactl *qctl;
1849 OBD_ALLOC_PTR(qctl);
1853 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1854 GOTO(out_quotactl, rc = -EFAULT);
1856 rc = quotactl_ioctl(inode->i_sb, qctl);
1859 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1866 case OBD_IOC_GETDTNAME:
1867 case OBD_IOC_GETMDNAME:
1868 RETURN(ll_get_obd_name(inode, cmd, arg));
1869 case LL_IOC_FLUSHCTX:
1870 RETURN(ll_flush_ctx(inode));
1871 case LL_IOC_GETOBDCOUNT: {
1873 struct obd_export *exp;
1875 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1878 /* get ost count when count is zero, get mdt count otherwise */
1879 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1880 vallen = sizeof(count);
1881 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1882 KEY_TGT_COUNT, &vallen, &count);
1884 CERROR("get target count failed: %d\n", rc);
1888 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1893 case LL_IOC_PATH2FID:
1894 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1895 sizeof(struct lu_fid)))
1898 case LL_IOC_GET_CONNECT_FLAGS: {
1899 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1900 (void __user *)arg));
1902 case OBD_IOC_FID2PATH:
1903 RETURN(ll_fid2path(inode, (void __user *)arg));
1904 case LL_IOC_GETPARENT:
1905 RETURN(ll_getparent(file, (void __user *)arg));
1906 case LL_IOC_FID2MDTIDX: {
1907 struct obd_export *exp = ll_i2mdexp(inode);
1911 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1915 /* Call mdc_iocontrol */
1916 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1917 (__u32 __user *)&index);
1923 case LL_IOC_HSM_REQUEST: {
1924 struct hsm_user_request *hur;
1931 /* We don't know the true size yet; copy the fixed-size part */
1932 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1937 /* Compute the whole struct size */
1938 totalsize = hur_len(hur);
1943 /* Final size will be more than double totalsize */
1944 if (totalsize >= MDS_MAXREQSIZE / 3)
1947 OBD_ALLOC_LARGE(hur, totalsize);
1951 /* Copy the whole struct */
1952 if (copy_from_user(hur, (void __user *)arg, totalsize))
1953 GOTO(out_hur, rc = -EFAULT);
1955 if (hur->hur_request.hr_action == HUA_RELEASE) {
1956 const struct lu_fid *fid;
1960 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1961 fid = &hur->hur_user_item[i].hui_fid;
1962 f = search_inode_for_lustre(inode->i_sb, fid);
1968 rc = ll_hsm_release(f);
1974 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1979 OBD_FREE_LARGE(hur, totalsize);
1983 case LL_IOC_HSM_PROGRESS: {
1984 struct hsm_progress_kernel hpk;
1985 struct hsm_progress hp;
1987 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1990 hpk.hpk_fid = hp.hp_fid;
1991 hpk.hpk_cookie = hp.hp_cookie;
1992 hpk.hpk_extent = hp.hp_extent;
1993 hpk.hpk_flags = hp.hp_flags;
1994 hpk.hpk_errval = hp.hp_errval;
1995 hpk.hpk_data_version = 0;
1997 /* File may not exist in Lustre; all progress
1998 * reported to Lustre root */
1999 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2003 case LL_IOC_HSM_CT_START:
2004 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2007 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2008 (struct lustre_kernelcomm __user *)arg);
2011 case LL_IOC_HSM_COPY_START: {
2012 struct hsm_copy *copy;
2015 OBD_ALLOC_PTR(copy);
2018 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2023 rc = ll_ioc_copy_start(inode->i_sb, copy);
2024 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2030 case LL_IOC_HSM_COPY_END: {
2031 struct hsm_copy *copy;
2034 OBD_ALLOC_PTR(copy);
2037 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2042 rc = ll_ioc_copy_end(inode->i_sb, copy);
2043 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2049 case LL_IOC_MIGRATE: {
2050 struct lmv_user_md *lum;
2057 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2061 data = (struct obd_ioctl_data *)buf;
2062 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2063 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2064 GOTO(migrate_free, rc = -EINVAL);
2066 filename = data->ioc_inlbuf1;
2067 namelen = data->ioc_inllen1;
2069 if (namelen < 1 || namelen != strlen(filename) + 1) {
2070 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2071 GOTO(migrate_free, rc = -EINVAL);
2074 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2075 if (lum->lum_magic != LMV_USER_MAGIC &&
2076 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2078 CERROR("%s: wrong lum magic %x: rc = %d\n",
2079 filename, lum->lum_magic, rc);
2080 GOTO(migrate_free, rc);
2083 rc = ll_migrate(inode, file, lum, filename);
2085 OBD_FREE_LARGE(buf, len);
2089 case LL_IOC_FSGETXATTR:
2090 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2091 case LL_IOC_FSSETXATTR:
2092 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2094 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2095 (void __user *)arg));
2099 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2101 struct inode *inode = file->f_mapping->host;
2102 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2103 struct ll_sb_info *sbi = ll_i2sbi(inode);
2104 int api32 = ll_need_32bit_api(sbi);
2105 loff_t ret = -EINVAL;
2113 offset += file->f_pos;
2119 offset += LL_DIR_END_OFF_32BIT;
2121 offset += LL_DIR_END_OFF;
2128 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2129 (!api32 && offset <= LL_DIR_END_OFF))) {
2130 if (offset != file->f_pos) {
2131 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2132 (!api32 && offset == LL_DIR_END_OFF))
2133 fd->lfd_pos = MDS_DIR_END_OFF;
2134 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2135 fd->lfd_pos = offset << 32;
2137 fd->lfd_pos = offset;
2138 file->f_pos = offset;
2139 file->f_version = 0;
2146 inode_unlock(inode);
2150 static int ll_dir_open(struct inode *inode, struct file *file)
2153 RETURN(ll_file_open(inode, file));
2156 static int ll_dir_release(struct inode *inode, struct file *file)
2159 RETURN(ll_file_release(inode, file));
2162 const struct file_operations ll_dir_operations = {
2163 .llseek = ll_dir_seek,
2164 .open = ll_dir_open,
2165 .release = ll_dir_release,
2166 .read = generic_read_dir,
2167 #ifdef HAVE_DIR_CONTEXT
2168 .iterate = ll_iterate,
2170 .readdir = ll_readdir,
2172 .unlocked_ioctl = ll_dir_ioctl,