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 #include <linux/uidgid.h>
44 #include <linux/uaccess.h>
45 #include <linux/buffer_head.h> // for wait_on_buffer
46 #include <linux/pagevec.h>
48 #define DEBUG_SUBSYSTEM S_LLITE
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lib.h>
54 #include <lustre_dlm.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
59 #include "llite_internal.h"
62 * (new) readdir implementation overview.
64 * Original lustre readdir implementation cached exact copy of raw directory
65 * pages on the client. These pages were indexed in client page cache by
66 * logical offset in the directory file. This design, while very simple and
67 * intuitive had some inherent problems:
69 * . it implies that byte offset to the directory entry serves as a
70 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71 * ext3/htree directory entries may move due to splits, and more
74 * . it is incompatible with the design of split directories for cmd3,
75 * that assumes that names are distributed across nodes based on their
76 * hash, and so readdir should be done in hash order.
78 * New readdir implementation does readdir in hash order, and uses hash of a
79 * file name as a telldir/seekdir cookie. This led to number of complications:
81 * . hash is not unique, so it cannot be used to index cached directory
82 * pages on the client (note, that it requires a whole pageful of hash
83 * collided entries to cause two pages to have identical hashes);
85 * . hash is not unique, so it cannot, strictly speaking, be used as an
86 * entry cookie. ext3/htree has the same problem and lustre implementation
87 * mimics their solution: seekdir(hash) positions directory at the first
88 * entry with the given hash.
94 * Client caches directory pages using hash of the first entry as an index. As
95 * noted above hash is not unique, so this solution doesn't work as is:
96 * special processing is needed for "page hash chains" (i.e., sequences of
97 * pages filled with entries all having the same hash value).
99 * First, such chains have to be detected. To this end, server returns to the
100 * client the hash of the first entry on the page next to one returned. When
101 * client detects that this hash is the same as hash of the first entry on the
102 * returned page, page hash collision has to be handled. Pages in the
103 * hash chain, except first one, are termed "overflow pages".
105 * Solution to index uniqueness problem is to not cache overflow
106 * pages. Instead, when page hash collision is detected, all overflow pages
107 * from emerging chain are immediately requested from the server and placed in
108 * a special data structure (struct ll_dir_chain). This data structure is used
109 * by ll_readdir() to process entries from overflow pages. When readdir
110 * invocation finishes, overflow pages are discarded. If page hash collision
111 * chain weren't completely processed, next call to readdir will again detect
112 * page hash collision, again read overflow pages in, process next portion of
113 * entries and again discard the pages. This is not as wasteful as it looks,
114 * because, given reasonable hash, page hash collisions are extremely rare.
116 * 1. directory positioning
118 * When seekdir(hash) is called, original
129 * identification of and access to overflow pages
133 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
134 * a header lu_dirpage which describes the start/end hash, and whether this
135 * page is empty (contains no dir entry) or hash collide with next page.
136 * After client receives reply, several pages will be integrated into dir page
137 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
138 * lu_dirpage for this integrated page will be adjusted. See
139 * mdc_adjust_dirpages().
142 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
143 __u64 offset, struct ll_dir_chain *chain)
145 struct md_callback cb_op;
149 cb_op.md_blocking_ast = ll_md_blocking_ast;
150 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
157 void ll_release_page(struct inode *inode, struct page *page,
162 /* Always remove the page for striped dir, because the page is
163 * built from temporarily in LMV layer */
164 if (inode && ll_dir_striped(inode)) {
171 if (likely(page->mapping != NULL))
172 delete_from_page_cache(page);
179 * return IF_* type for given lu_dirent entry.
180 * IF_* flag shld be converted to particular OS file type in
181 * platform llite module.
183 static u16 ll_dirent_type_get(struct lu_dirent *ent)
186 struct luda_type *lt;
189 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
190 const unsigned align = sizeof(struct luda_type) - 1;
192 len = le16_to_cpu(ent->lde_namelen);
193 len = (len + align) & ~align;
194 lt = (void *)ent->lde_name + len;
195 type = IFTODT(le16_to_cpu(lt->lt_type));
201 #ifdef HAVE_DIR_CONTEXT
202 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
203 struct dir_context *ctx)
206 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
207 void *cookie, filldir_t filldir)
210 struct ll_sb_info *sbi = ll_i2sbi(inode);
212 bool is_api32 = ll_need_32bit_api(sbi);
213 bool is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
215 struct ll_dir_chain chain;
220 ll_dir_chain_init(&chain);
222 page = ll_get_dir_page(inode, op_data, pos, &chain);
224 while (rc == 0 && !done) {
225 struct lu_dirpage *dp;
226 struct lu_dirent *ent;
235 hash = MDS_DIR_END_OFF;
236 dp = page_address(page);
237 for (ent = lu_dirent_start(dp); ent != NULL && !done;
238 ent = lu_dirent_next(ent)) {
245 hash = le64_to_cpu(ent->lde_hash);
246 if (hash < pos) /* Skip until we find target hash */
249 namelen = le16_to_cpu(ent->lde_namelen);
250 if (namelen == 0) /* Skip dummy record */
253 if (is_api32 && is_hash64)
257 fid_le_to_cpu(&fid, &ent->lde_fid);
258 ino = cl_fid_build_ino(&fid, is_api32);
259 type = ll_dirent_type_get(ent);
260 /* For ll_nfs_get_name_filldir(), it will try to access
261 * 'ent' through 'lde_name', so the parameter 'name'
262 * for 'filldir()' must be part of the 'ent'. */
263 #ifdef HAVE_DIR_CONTEXT
265 done = !dir_emit(ctx, ent->lde_name, namelen, ino,
268 done = filldir(cookie, ent->lde_name, namelen, lhash,
275 ll_release_page(inode, page, false);
279 next = le64_to_cpu(dp->ldp_hash_end);
281 if (pos == MDS_DIR_END_OFF) {
283 * End of directory reached.
286 ll_release_page(inode, page, false);
289 * Normal case: continue to the next
292 ll_release_page(inode, page,
293 le32_to_cpu(dp->ldp_flags) &
296 page = ll_get_dir_page(inode, op_data, pos,
300 #ifdef HAVE_DIR_CONTEXT
305 ll_dir_chain_fini(&chain);
309 #ifdef HAVE_DIR_CONTEXT
310 static int ll_iterate(struct file *filp, struct dir_context *ctx)
312 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
315 struct inode *inode = file_inode(filp);
316 struct ll_file_data *lfd = filp->private_data;
317 struct ll_sb_info *sbi = ll_i2sbi(inode);
318 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
319 int api32 = ll_need_32bit_api(sbi);
320 struct md_op_data *op_data;
321 struct lu_fid pfid = { 0 };
322 ktime_t kstart = ktime_get();
333 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
334 PFID(ll_inode2fid(inode)),
335 inode, (unsigned long)pos, i_size_read(inode), api32);
337 if (pos == MDS_DIR_END_OFF)
343 if (unlikely(ll_dir_striped(inode))) {
345 * This is only needed for striped dir to fill ..,
346 * see lmv_read_page()
348 if (file_dentry(filp)->d_parent != NULL &&
349 file_dentry(filp)->d_parent->d_inode != NULL) {
350 __u64 ibits = MDS_INODELOCK_LOOKUP;
351 struct inode *parent =
352 file_dentry(filp)->d_parent->d_inode;
354 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
355 pfid = *ll_inode2fid(parent);
358 /* If it can not find in cache, do lookup .. on the master
360 if (fid_is_zero(&pfid)) {
361 rc = ll_dir_get_parent_fid(inode, &pfid);
367 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
368 LUSTRE_OPC_ANY, inode);
370 GOTO(out, rc = PTR_ERR(op_data));
372 /* foreign dirs are browsed out of Lustre */
373 if (unlikely(op_data->op_mea1 != NULL &&
374 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
375 ll_finish_md_op_data(op_data);
379 op_data->op_fid3 = pfid;
381 #ifdef HAVE_DIR_CONTEXT
383 rc = ll_dir_read(inode, &pos, op_data, ctx);
386 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
391 if (pos == MDS_DIR_END_OFF) {
393 pos = LL_DIR_END_OFF_32BIT;
395 pos = LL_DIR_END_OFF;
400 #ifdef HAVE_DIR_CONTEXT
405 ll_finish_md_op_data(op_data);
409 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
410 ktime_us_delta(ktime_get(), kstart));
415 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
416 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
418 struct mgs_send_param *msp;
425 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
426 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
427 sizeof(struct mgs_send_param), msp, NULL);
429 CERROR("Failed to set parameter: %d\n", rc);
437 * Create striped directory with specified stripe(@lump)
439 * \param[in] dparent the parent of the directory.
440 * \param[in] lump the specified stripes.
441 * \param[in] dirname the name of the directory.
442 * \param[in] mode the specified mode of the directory.
444 * \retval =0 if striped directory is being created successfully.
445 * <0 if the creation is failed.
447 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
448 size_t len, const char *dirname, umode_t mode)
450 struct inode *parent = dparent->d_inode;
451 struct ptlrpc_request *request = NULL;
452 struct md_op_data *op_data;
453 struct ll_sb_info *sbi = ll_i2sbi(parent);
454 struct inode *inode = NULL;
455 struct dentry dentry = {
459 .len = strlen(dirname),
460 .hash = ll_full_name_hash(dparent, dirname,
467 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
470 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
472 "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
473 PFID(ll_inode2fid(parent)), parent, dirname,
474 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
476 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
479 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
480 PFID(ll_inode2fid(parent)), parent, dirname,
481 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
484 if (lump->lum_stripe_count > 1 &&
485 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
488 if (IS_DEADDIR(parent) &&
489 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
492 if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
493 if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
494 LMV_HASH_TYPE_CRUSH) {
495 /* if server doesn't support 'crush' hash type,
496 * switch to fnv_1a_64.
498 lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
499 lump->lum_hash_type |= LMV_HASH_TYPE_FNV_1A_64;
500 } else if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
501 LMV_HASH_TYPE_UNKNOWN) {
502 /* from 2.14 MDT will choose default hash type if client
503 * doesn't set a valid one, while old server doesn't
506 lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
507 lump->lum_hash_type |= LMV_HASH_TYPE_DEFAULT;
511 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
512 lustre_swab_lmv_user_md(lump);
514 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
515 mode &= ~current_umask();
516 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
517 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
518 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
521 RETURN(PTR_ERR(op_data));
523 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
524 /* selinux_dentry_init_security() uses dentry->d_parent and name
525 * to determine the security context for the file. So our fake
526 * dentry should be real enough for this purpose. */
527 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
528 &op_data->op_file_secctx_name,
529 &op_data->op_file_secctx,
530 &op_data->op_file_secctx_size);
532 GOTO(out_op_data, err);
535 op_data->op_cli_flags |= CLI_SET_MEA;
536 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
537 from_kuid(&init_user_ns, current_fsuid()),
538 from_kgid(&init_user_ns, current_fsgid()),
539 cfs_curproc_cap_pack(), 0, &request);
541 GOTO(out_request, err);
543 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
545 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
547 GOTO(out_inode, err);
549 dentry.d_inode = inode;
551 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
553 err = security_inode_notifysecctx(inode,
554 op_data->op_file_secctx,
555 op_data->op_file_secctx_size);
558 err = ll_inode_init_security(&dentry, inode, parent);
561 GOTO(out_inode, err);
567 ptlrpc_req_finished(request);
569 ll_finish_md_op_data(op_data);
574 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
577 struct ll_sb_info *sbi = ll_i2sbi(inode);
578 struct md_op_data *op_data;
579 struct ptlrpc_request *req = NULL;
581 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
582 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
583 struct obd_device *mgc = lsi->lsi_mgc;
589 switch (lump->lmm_magic) {
590 case LOV_USER_MAGIC_V1:
591 lum_size = sizeof(struct lov_user_md_v1);
593 case LOV_USER_MAGIC_V3:
594 lum_size = sizeof(struct lov_user_md_v3);
596 case LOV_USER_MAGIC_COMP_V1:
597 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
600 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
601 lustre_swab_lmv_user_md(
602 (struct lmv_user_md *)lump);
603 lum_size = sizeof(struct lmv_user_md);
605 case LOV_USER_MAGIC_SPECIFIC: {
606 struct lov_user_md_v3 *v3 =
607 (struct lov_user_md_v3 *)lump;
608 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
610 lum_size = lov_user_md_size(v3->lmm_stripe_count,
611 LOV_USER_MAGIC_SPECIFIC);
615 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
616 " %#08x != %#08x nor %#08x\n",
617 lump->lmm_magic, LOV_USER_MAGIC_V1,
623 * This is coming from userspace, so should be in
624 * local endian. But the MDS would like it in little
625 * endian, so we swab it before we send it.
627 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
628 le32_to_cpu(LOV_MAGIC_MAGIC))
629 lustre_swab_lov_user_md(lump, 0);
631 lum_size = sizeof(struct lov_user_md_v1);
634 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
635 LUSTRE_OPC_ANY, NULL);
637 RETURN(PTR_ERR(op_data));
639 /* swabbing is done in lov_setstripe() on server side */
640 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
641 ll_finish_md_op_data(op_data);
642 ptlrpc_req_finished(req);
646 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
648 * 2.9 server has stored filesystem default stripe in ROOT xattr,
649 * and it's stored into system config for backward compatibility.
651 * In the following we use the fact that LOV_USER_MAGIC_V1 and
652 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
653 * need the make the distiction between the 2 versions
655 if (set_default && mgc->u.cli.cl_mgc_mgsexp &&
657 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V1 ||
658 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V3)) {
662 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
664 GOTO(end, rc = -ENOMEM);
667 /* Get fsname and assume devname to be -MDT0000. */
668 snprintf(buf, MGS_PARAM_MAXLEN, "%s-MDT0000.lov",
672 /* Set root stripesize */
673 snprintf(buf, MGS_PARAM_MAXLEN, ".stripesize=%u",
674 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
675 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
679 /* Set root stripecount */
680 snprintf(buf, MGS_PARAM_MAXLEN, ".stripecount=%hd",
681 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
682 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
686 /* Set root stripeoffset */
687 snprintf(buf, MGS_PARAM_MAXLEN, ".stripeoffset=%hd",
688 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
689 (typeof(lump->lmm_stripe_offset))(-1));
690 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
694 OBD_FREE(param, MGS_PARAM_MAXLEN);
700 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
702 struct ptlrpc_request **request, u64 valid,
703 enum get_default_layout_type type)
705 struct ll_sb_info *sbi = ll_i2sbi(inode);
706 struct mdt_body *body;
707 struct lov_mds_md *lmm = NULL;
708 struct ptlrpc_request *req = NULL;
710 struct md_op_data *op_data;
714 rc = ll_get_default_mdsize(sbi, &lmm_size);
718 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
719 0, lmm_size, LUSTRE_OPC_ANY,
722 RETURN(PTR_ERR(op_data));
724 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
726 if (type == GET_DEFAULT_LAYOUT_ROOT) {
727 lu_root_fid(&op_data->op_fid1);
728 fid = op_data->op_fid1;
730 fid = *ll_inode2fid(inode);
733 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
734 ll_finish_md_op_data(op_data);
736 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
741 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
742 LASSERT(body != NULL);
744 lmm_size = body->mbo_eadatasize;
746 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
748 GOTO(out, rc = -ENODATA);
751 lmm = req_capsule_server_sized_get(&req->rq_pill,
752 &RMF_MDT_MD, lmm_size);
753 LASSERT(lmm != NULL);
756 * This is coming from the MDS, so is probably in
757 * little endian. We convert it to host endian before
758 * passing it to userspace.
760 /* We don't swab objects for directories */
761 switch (le32_to_cpu(lmm->lmm_magic)) {
764 case LOV_MAGIC_COMP_V1:
765 case LOV_USER_MAGIC_SPECIFIC:
766 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
767 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
770 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
771 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
774 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
775 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
777 case LMV_MAGIC_FOREIGN: {
778 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
780 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
781 __swab32s(&lfm->lfm_magic);
782 __swab32s(&lfm->lfm_length);
783 __swab32s(&lfm->lfm_type);
784 __swab32s(&lfm->lfm_flags);
789 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
794 *plmm_size = lmm_size;
800 * This function will be used to get default LOV/LMV/Default LMV
801 * @valid will be used to indicate which stripe it will retrieve.
802 * If the directory does not have its own default layout, then the
803 * function will request the default layout from root FID.
804 * OBD_MD_MEA LMV stripe EA
805 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
806 * otherwise Default LOV EA.
807 * Each time, it can only retrieve 1 stripe EA
809 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
810 struct ptlrpc_request **request,
811 struct ptlrpc_request **root_request,
814 struct ptlrpc_request *req = NULL;
815 struct ptlrpc_request *root_req = NULL;
816 struct lov_mds_md *lmm = NULL;
821 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
823 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
824 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
825 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
827 GET_DEFAULT_LAYOUT_ROOT);
830 *plmm_size = lmm_size;
832 if (root_request != NULL)
833 *root_request = root_req;
839 * This function will be used to get default LOV/LMV/Default LMV
840 * @valid will be used to indicate which stripe it will retrieve
841 * OBD_MD_MEA LMV stripe EA
842 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
843 * otherwise Default LOV EA.
844 * Each time, it can only retrieve 1 stripe EA
846 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
847 struct ptlrpc_request **request, u64 valid)
849 struct ptlrpc_request *req = NULL;
850 struct lov_mds_md *lmm = NULL;
855 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
859 *plmm_size = lmm_size;
865 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
867 struct md_op_data *op_data;
872 OBD_ALLOC_PTR(op_data);
876 op_data->op_flags |= MF_GET_MDT_IDX;
877 op_data->op_fid1 = *fid;
878 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
879 mdt_index = op_data->op_mds;
880 OBD_FREE_PTR(op_data);
888 * Get MDT index for the inode.
890 int ll_get_mdt_idx(struct inode *inode)
892 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
896 * Generic handler to do any pre-copy work.
898 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
899 * first information for it that real work has started.
901 * Moreover, for a ARCHIVE request, it will sample the file data version and
902 * store it in \a copy.
904 * \return 0 on success.
906 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
908 struct ll_sb_info *sbi = ll_s2sbi(sb);
909 struct hsm_progress_kernel hpk;
914 /* Forge a hsm_progress based on data from copy. */
915 hpk.hpk_fid = copy->hc_hai.hai_fid;
916 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
917 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
918 hpk.hpk_extent.length = 0;
921 hpk.hpk_data_version = 0;
924 /* For archive request, we need to read the current file version. */
925 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
927 __u64 data_version = 0;
929 /* Get inode for this fid */
930 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
932 hpk.hpk_flags |= HP_FLAG_RETRY;
933 /* hpk_errval is >= 0 */
934 hpk.hpk_errval = -PTR_ERR(inode);
935 GOTO(progress, rc = PTR_ERR(inode));
938 /* Read current file data version */
939 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
942 CDEBUG(D_HSM, "Could not read file data version of "
943 DFID" (rc = %d). Archive request ("
944 "%#llx) could not be done.\n",
945 PFID(©->hc_hai.hai_fid), rc,
946 copy->hc_hai.hai_cookie);
947 hpk.hpk_flags |= HP_FLAG_RETRY;
948 /* hpk_errval must be >= 0 */
949 hpk.hpk_errval = -rc;
953 /* Store in the hsm_copy for later copytool use.
954 * Always modified even if no lsm. */
955 copy->hc_data_version = data_version;
959 /* On error, the request should be considered as completed */
960 if (hpk.hpk_errval > 0)
961 hpk.hpk_flags |= HP_FLAG_COMPLETED;
963 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
966 /* Return first error */
967 RETURN(rc != 0 ? rc : rc2);
971 * Generic handler to do any post-copy work.
973 * It will send the last hsm_progress update to coordinator to inform it
974 * that copy is finished and whether it was successful or not.
977 * - for ARCHIVE request, it will sample the file data version and compare it
978 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
979 * will be considered as failed.
980 * - for RESTORE request, it will sample the file data version and send it to
981 * coordinator which is useful if the file was imported as 'released'.
983 * \return 0 on success.
985 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
987 struct ll_sb_info *sbi = ll_s2sbi(sb);
988 struct hsm_progress_kernel hpk;
993 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
994 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
995 * initialized if copy_end was called with copy == NULL.
998 /* Forge a hsm_progress based on data from copy. */
999 hpk.hpk_fid = copy->hc_hai.hai_fid;
1000 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1001 hpk.hpk_extent = copy->hc_hai.hai_extent;
1002 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1003 hpk.hpk_errval = copy->hc_errval;
1004 hpk.hpk_data_version = 0;
1006 /* For archive request, we need to check the file data was not changed.
1008 * For restore request, we need to send the file data version, this is
1009 * useful when the file was created using hsm_import.
1011 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1012 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1013 (copy->hc_errval == 0)) {
1014 struct inode *inode;
1015 __u64 data_version = 0;
1017 /* Get lsm for this fid */
1018 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1019 if (IS_ERR(inode)) {
1020 hpk.hpk_flags |= HP_FLAG_RETRY;
1021 /* hpk_errval must be >= 0 */
1022 hpk.hpk_errval = -PTR_ERR(inode);
1023 GOTO(progress, rc = PTR_ERR(inode));
1026 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1029 CDEBUG(D_HSM, "Could not read file data version. "
1030 "Request could not be confirmed.\n");
1031 if (hpk.hpk_errval == 0)
1032 hpk.hpk_errval = -rc;
1036 /* Store in the hsm_copy for later copytool use.
1037 * Always modified even if no lsm. */
1038 hpk.hpk_data_version = data_version;
1040 /* File could have been stripped during archiving, so we need
1041 * to check anyway. */
1042 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1043 (copy->hc_data_version != data_version)) {
1044 CDEBUG(D_HSM, "File data version mismatched. "
1045 "File content was changed during archiving. "
1046 DFID", start:%#llx current:%#llx\n",
1047 PFID(©->hc_hai.hai_fid),
1048 copy->hc_data_version, data_version);
1049 /* File was changed, send error to cdt. Do not ask for
1050 * retry because if a file is modified frequently,
1051 * the cdt will loop on retried archive requests.
1052 * The policy engine will ask for a new archive later
1053 * when the file will not be modified for some tunable
1055 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1057 /* hpk_errval must be >= 0 */
1058 hpk.hpk_errval = -rc;
1065 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1068 /* Return first error */
1069 RETURN(rc != 0 ? rc : rc2);
1073 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1074 const struct lustre_kernelcomm __user *data)
1076 struct lustre_kernelcomm *lk;
1077 struct lustre_kernelcomm *tmp;
1078 size_t size = sizeof(*lk);
1083 /* copy data from userspace to get numbers of archive_id */
1084 OBD_ALLOC(lk, size);
1088 if (copy_from_user(lk, data, size))
1089 GOTO(out_lk, rc = -EFAULT);
1091 if (lk->lk_flags & LK_FLG_STOP)
1094 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1095 __u32 archive_mask = lk->lk_data_count;
1098 /* old hsm agent to old MDS */
1099 if (!exp_connect_archive_id_array(exp))
1102 /* old hsm agent to new MDS */
1103 lk->lk_flags |= LK_FLG_DATANR;
1105 if (archive_mask == 0)
1108 count = hweight32(archive_mask);
1109 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1110 OBD_ALLOC(tmp, new_size);
1112 GOTO(out_lk, rc = -ENOMEM);
1114 memcpy(tmp, lk, size);
1115 tmp->lk_data_count = count;
1121 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1122 if ((1 << i) & archive_mask) {
1123 lk->lk_data[count] = i + 1;
1130 /* new hsm agent to new mds */
1131 if (lk->lk_data_count > 0) {
1132 new_size = offsetof(struct lustre_kernelcomm,
1133 lk_data[lk->lk_data_count]);
1134 OBD_ALLOC(tmp, new_size);
1136 GOTO(out_lk, rc = -ENOMEM);
1142 if (copy_from_user(lk, data, size))
1143 GOTO(out_lk, rc = -EFAULT);
1146 /* new hsm agent to old MDS */
1147 if (!exp_connect_archive_id_array(exp)) {
1150 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1151 GOTO(out_lk, rc = -EINVAL);
1153 for (i = 0; i < lk->lk_data_count; i++) {
1154 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1156 CERROR("%s: archive id %d requested but only "
1157 "[0 - %zu] supported: rc = %d\n",
1158 exp->exp_obd->obd_name, lk->lk_data[i],
1159 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1163 if (lk->lk_data[i] == 0) {
1168 archives |= (1 << (lk->lk_data[i] - 1));
1170 lk->lk_flags &= ~LK_FLG_DATANR;
1171 lk->lk_data_count = archives;
1174 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1180 static int check_owner(int type, int id)
1184 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1188 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1197 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1199 int cmd = qctl->qc_cmd;
1200 int type = qctl->qc_type;
1201 int id = qctl->qc_id;
1202 int valid = qctl->qc_valid;
1209 case LUSTRE_Q_SETDEFAULT:
1210 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1214 case LUSTRE_Q_GETDEFAULT:
1215 if (check_owner(type, id) &&
1216 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1222 CERROR("unsupported quotactl op: %#x\n", cmd);
1226 if (valid != QC_GENERAL) {
1227 if (cmd == Q_GETINFO)
1228 qctl->qc_cmd = Q_GETOINFO;
1229 else if (cmd == Q_GETQUOTA)
1230 qctl->qc_cmd = Q_GETOQUOTA;
1236 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1237 sizeof(*qctl), qctl, NULL);
1240 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1241 sizeof(*qctl), qctl, NULL);
1244 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1245 sizeof(*qctl), qctl, NULL);
1247 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1249 sizeof(*qctl), qctl, NULL);
1261 struct obd_quotactl *oqctl;
1263 OBD_ALLOC_PTR(oqctl);
1267 QCTL_COPY(oqctl, qctl);
1268 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1270 OBD_FREE_PTR(oqctl);
1273 /* If QIF_SPACE is not set, client should collect the
1274 * space usage from OSSs by itself */
1275 if (cmd == Q_GETQUOTA &&
1276 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1277 !oqctl->qc_dqblk.dqb_curspace) {
1278 struct obd_quotactl *oqctl_tmp;
1280 OBD_ALLOC_PTR(oqctl_tmp);
1281 if (oqctl_tmp == NULL)
1282 GOTO(out, rc = -ENOMEM);
1284 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1285 oqctl_tmp->qc_id = oqctl->qc_id;
1286 oqctl_tmp->qc_type = oqctl->qc_type;
1288 /* collect space usage from OSTs */
1289 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1290 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1291 if (!rc || rc == -EREMOTEIO) {
1292 oqctl->qc_dqblk.dqb_curspace =
1293 oqctl_tmp->qc_dqblk.dqb_curspace;
1294 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1297 /* collect space & inode usage from MDTs */
1298 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1299 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1300 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1301 if (!rc || rc == -EREMOTEIO) {
1302 oqctl->qc_dqblk.dqb_curspace +=
1303 oqctl_tmp->qc_dqblk.dqb_curspace;
1304 oqctl->qc_dqblk.dqb_curinodes =
1305 oqctl_tmp->qc_dqblk.dqb_curinodes;
1306 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1308 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1311 OBD_FREE_PTR(oqctl_tmp);
1314 QCTL_COPY(qctl, oqctl);
1315 OBD_FREE_PTR(oqctl);
1321 int ll_rmfid(struct file *file, void __user *arg)
1323 const struct fid_array __user *ufa = arg;
1324 struct fid_array *lfa = NULL;
1327 int i, rc, *rcs = NULL;
1330 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1331 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1333 /* Only need to get the buflen */
1334 if (get_user(nr, &ufa->fa_nr))
1336 /* DoS protection */
1337 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1340 size = offsetof(struct fid_array, fa_fids[nr]);
1341 OBD_ALLOC(lfa, size);
1344 OBD_ALLOC(rcs, sizeof(int) * nr);
1346 GOTO(free_lfa, rc = -ENOMEM);
1348 if (copy_from_user(lfa, arg, size))
1349 GOTO(free_rcs, rc = -EFAULT);
1351 /* Call mdc_iocontrol */
1352 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1354 for (i = 0; i < nr; i++)
1356 lfa->fa_fids[i].f_ver = rcs[i];
1357 if (copy_to_user(arg, lfa, size))
1362 OBD_FREE(rcs, sizeof(int) * nr);
1364 OBD_FREE(lfa, size);
1369 /* This function tries to get a single name component,
1370 * to send to the server. No actual path traversal involved,
1371 * so we limit to NAME_MAX */
1372 static char *ll_getname(const char __user *filename)
1377 OBD_ALLOC(tmp, NAME_MAX + 1);
1380 return ERR_PTR(-ENOMEM);
1382 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1385 else if (len > NAME_MAX)
1386 ret = -ENAMETOOLONG;
1389 OBD_FREE(tmp, NAME_MAX + 1);
1395 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1397 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1399 struct dentry *dentry = file_dentry(file);
1400 struct inode *inode = file_inode(file);
1401 struct ll_sb_info *sbi = ll_i2sbi(inode);
1402 struct obd_ioctl_data *data;
1406 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1407 PFID(ll_inode2fid(inode)), inode, cmd);
1409 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1410 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1413 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1415 case FS_IOC_GETFLAGS:
1416 case FS_IOC_SETFLAGS:
1417 RETURN(ll_iocontrol(inode, file, cmd, arg));
1418 case FSFILT_IOC_GETVERSION:
1419 case FS_IOC_GETVERSION:
1420 RETURN(put_user(inode->i_generation, (int __user *)arg));
1421 /* We need to special case any other ioctls we want to handle,
1422 * to send them to the MDS/OST as appropriate and to properly
1423 * network encode the arg field. */
1424 case FS_IOC_SETVERSION:
1427 case LL_IOC_GET_MDTIDX: {
1430 mdtidx = ll_get_mdt_idx(inode);
1434 if (put_user((int)mdtidx, (int __user *)arg))
1439 case IOC_MDC_LOOKUP: {
1440 int namelen, len = 0;
1444 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1449 filename = data->ioc_inlbuf1;
1450 namelen = strlen(filename);
1452 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1453 GOTO(out_free, rc = -EINVAL);
1456 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1458 CERROR("%s: lookup %.*s failed: rc = %d\n",
1459 sbi->ll_fsname, namelen, filename, rc);
1463 OBD_FREE_LARGE(buf, len);
1466 case LL_IOC_LMV_SETSTRIPE: {
1467 struct lmv_user_md *lum;
1476 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1481 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1482 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1483 GOTO(lmv_out_free, rc = -EINVAL);
1485 filename = data->ioc_inlbuf1;
1486 namelen = data->ioc_inllen1;
1489 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1490 GOTO(lmv_out_free, rc = -EINVAL);
1492 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1493 lumlen = data->ioc_inllen2;
1495 if (!lmv_user_magic_supported(lum->lum_magic)) {
1496 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1497 lum->lum_magic, -EINVAL);
1498 GOTO(lmv_out_free, rc = -EINVAL);
1501 if ((lum->lum_magic == LMV_USER_MAGIC ||
1502 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1503 lumlen < sizeof(*lum)) {
1504 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1505 filename, lumlen, lum->lum_magic, -EINVAL);
1506 GOTO(lmv_out_free, rc = -EINVAL);
1509 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1510 lumlen < sizeof(struct lmv_foreign_md)) {
1511 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1512 filename, lum->lum_magic, lumlen, -EFAULT);
1513 GOTO(lmv_out_free, rc = -EINVAL);
1516 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1517 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1519 mode = data->ioc_type;
1521 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1523 OBD_FREE_LARGE(buf, len);
1527 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1528 struct lmv_user_md lum;
1529 struct lmv_user_md __user *ulump =
1530 (struct lmv_user_md __user *)arg;
1533 if (copy_from_user(&lum, ulump, sizeof(lum)))
1536 if (lum.lum_magic != LMV_USER_MAGIC)
1539 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1543 case LL_IOC_LOV_SETSTRIPE_NEW:
1544 case LL_IOC_LOV_SETSTRIPE: {
1545 struct lov_user_md_v3 *lumv3 = NULL;
1546 struct lov_user_md_v1 lumv1;
1547 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1548 struct lov_user_md_v1 __user *lumv1p =
1549 (struct lov_user_md_v1 __user *)arg;
1550 struct lov_user_md_v3 __user *lumv3p =
1551 (struct lov_user_md_v3 __user *)arg;
1554 int set_default = 0;
1556 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1557 sizeof(struct lov_comp_md_v1));
1558 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1559 /* first try with v1 which is smaller than v3 */
1560 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1563 if (inode->i_sb->s_root == file_dentry(file))
1566 switch (lumv1.lmm_magic) {
1567 case LOV_USER_MAGIC_V3:
1568 case LOV_USER_MAGIC_SPECIFIC:
1569 lum_size = ll_lov_user_md_size(&lumv1);
1572 OBD_ALLOC(lumv3, lum_size);
1575 if (copy_from_user(lumv3, lumv3p, lum_size))
1576 GOTO(out, rc = -EFAULT);
1577 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1579 case LOV_USER_MAGIC_V1:
1582 GOTO(out, rc = -ENOTSUPP);
1585 /* in v1 and v3 cases lumv1 points to data */
1586 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1589 OBD_FREE(lumv3, lum_size);
1592 case LL_IOC_LMV_GETSTRIPE: {
1593 struct lmv_user_md __user *ulmv =
1594 (struct lmv_user_md __user *)arg;
1595 struct lmv_user_md lum;
1596 struct ptlrpc_request *request = NULL;
1597 struct ptlrpc_request *root_request = NULL;
1598 union lmv_mds_md *lmm = NULL;
1601 struct lmv_user_md *tmp = NULL;
1605 int max_stripe_count;
1609 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1612 max_stripe_count = lum.lum_stripe_count;
1613 /* lum_magic will indicate which stripe the ioctl will like
1614 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1615 * is for default LMV stripe */
1616 if (lum.lum_magic == LMV_MAGIC_V1)
1617 valid |= OBD_MD_MEA;
1618 else if (lum.lum_magic == LMV_USER_MAGIC)
1619 valid |= OBD_MD_DEFAULT_MEA;
1623 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1624 &request, &root_request, valid);
1626 GOTO(finish_req, rc);
1628 /* Get default LMV EA */
1629 if (lum.lum_magic == LMV_USER_MAGIC) {
1630 if (lmmsize > sizeof(*ulmv))
1631 GOTO(finish_req, rc = -EINVAL);
1633 if (copy_to_user(ulmv, lmm, lmmsize))
1634 GOTO(finish_req, rc = -EFAULT);
1636 GOTO(finish_req, rc);
1639 /* if foreign LMV case, fake stripes number */
1640 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1641 struct lmv_foreign_md *lfm;
1643 lfm = (struct lmv_foreign_md *)lmm;
1644 if (lfm->lfm_length < XATTR_SIZE_MAX -
1645 offsetof(typeof(*lfm), lfm_value)) {
1646 __u32 size = lfm->lfm_length +
1647 offsetof(typeof(*lfm), lfm_value);
1649 stripe_count = lmv_foreign_to_md_stripes(size);
1651 CERROR("invalid %d foreign size returned\n",
1656 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1658 if (max_stripe_count < stripe_count) {
1659 lum.lum_stripe_count = stripe_count;
1660 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1661 GOTO(finish_req, rc = -EFAULT);
1662 GOTO(finish_req, rc = -E2BIG);
1665 /* enough room on user side and foreign case */
1666 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1667 struct lmv_foreign_md *lfm;
1670 lfm = (struct lmv_foreign_md *)lmm;
1671 size = lfm->lfm_length +
1672 offsetof(struct lmv_foreign_md, lfm_value);
1673 if (copy_to_user(ulmv, lfm, size))
1674 GOTO(finish_req, rc = -EFAULT);
1675 GOTO(finish_req, rc);
1678 lum_size = lmv_user_md_size(stripe_count,
1679 LMV_USER_MAGIC_SPECIFIC);
1680 OBD_ALLOC(tmp, lum_size);
1682 GOTO(finish_req, rc = -ENOMEM);
1684 mdt_index = ll_get_mdt_idx(inode);
1686 GOTO(out_tmp, rc = -ENOMEM);
1688 tmp->lum_magic = LMV_MAGIC_V1;
1689 tmp->lum_stripe_count = 0;
1690 tmp->lum_stripe_offset = mdt_index;
1691 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1692 for (i = 0; i < stripe_count; i++) {
1695 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1696 if (fid_is_sane(&fid)) {
1697 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1699 GOTO(out_tmp, rc = mdt_index);
1701 tmp->lum_objects[i].lum_mds = mdt_index;
1702 tmp->lum_objects[i].lum_fid = fid;
1705 tmp->lum_stripe_count++;
1708 if (copy_to_user(ulmv, tmp, lum_size))
1709 GOTO(out_tmp, rc = -EFAULT);
1711 OBD_FREE(tmp, lum_size);
1713 ptlrpc_req_finished(request);
1714 ptlrpc_req_finished(root_request);
1718 case LL_IOC_REMOVE_ENTRY: {
1719 char *filename = NULL;
1723 /* Here is a little hack to avoid sending REINT_RMENTRY to
1724 * unsupported server, which might crash the server(LU-2730),
1725 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1726 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1727 * server will support REINT_RMENTRY XXX*/
1728 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1731 filename = ll_getname((const char __user *)arg);
1732 if (IS_ERR(filename))
1733 RETURN(PTR_ERR(filename));
1735 namelen = strlen(filename);
1737 GOTO(out_rmdir, rc = -EINVAL);
1739 rc = ll_rmdir_entry(inode, filename, namelen);
1742 ll_putname(filename);
1746 RETURN(ll_rmfid(file, (void __user *)arg));
1747 case LL_IOC_LOV_SWAP_LAYOUTS:
1749 case IOC_OBD_STATFS:
1750 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1751 case LL_IOC_LOV_GETSTRIPE:
1752 case LL_IOC_LOV_GETSTRIPE_NEW:
1753 case LL_IOC_MDC_GETINFO:
1754 case LL_IOC_MDC_GETINFO_OLD:
1755 case IOC_MDC_GETFILEINFO:
1756 case IOC_MDC_GETFILEINFO_OLD:
1757 case IOC_MDC_GETFILESTRIPE: {
1758 struct ptlrpc_request *request = NULL;
1759 struct ptlrpc_request *root_request = NULL;
1760 struct lov_user_md __user *lump;
1761 struct lov_mds_md *lmm = NULL;
1762 struct mdt_body *body;
1763 char *filename = NULL;
1764 lstat_t __user *statp = NULL;
1765 lstatx_t __user *stxp = NULL;
1766 __u64 __user *flagsp = NULL;
1767 __u32 __user *lmmsizep = NULL;
1768 struct lu_fid __user *fidp = NULL;
1771 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1772 cmd == IOC_MDC_GETFILEINFO ||
1773 cmd == IOC_MDC_GETFILESTRIPE) {
1774 filename = ll_getname((const char __user *)arg);
1775 if (IS_ERR(filename))
1776 RETURN(PTR_ERR(filename));
1778 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1779 &lmmsize, &request);
1781 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1787 body = req_capsule_server_get(&request->rq_pill,
1789 LASSERT(body != NULL);
1794 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1795 cmd == LL_IOC_MDC_GETINFO ||
1796 cmd == IOC_MDC_GETFILEINFO_OLD ||
1797 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1805 if (cmd == IOC_MDC_GETFILESTRIPE ||
1806 cmd == LL_IOC_LOV_GETSTRIPE ||
1807 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1808 lump = (struct lov_user_md __user *)arg;
1809 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1810 cmd == LL_IOC_MDC_GETINFO_OLD){
1811 struct lov_user_mds_data_v1 __user *lmdp;
1813 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1814 statp = &lmdp->lmd_st;
1815 lump = &lmdp->lmd_lmm;
1817 struct lov_user_mds_data __user *lmdp;
1819 lmdp = (struct lov_user_mds_data __user *)arg;
1820 fidp = &lmdp->lmd_fid;
1821 stxp = &lmdp->lmd_stx;
1822 flagsp = &lmdp->lmd_flags;
1823 lmmsizep = &lmdp->lmd_lmmsize;
1824 lump = &lmdp->lmd_lmm;
1828 /* If the file has no striping then zero out *lump so
1829 * that the caller isn't confused by garbage. */
1830 if (clear_user(lump, sizeof(*lump)))
1831 GOTO(out_req, rc = -EFAULT);
1832 } else if (copy_to_user(lump, lmm, lmmsize)) {
1833 if (copy_to_user(lump, lmm, sizeof(*lump)))
1834 GOTO(out_req, rc = -EFAULT);
1838 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1839 cmd == LL_IOC_MDC_GETINFO_OLD) {
1842 st.st_dev = inode->i_sb->s_dev;
1843 st.st_mode = body->mbo_mode;
1844 st.st_nlink = body->mbo_nlink;
1845 st.st_uid = body->mbo_uid;
1846 st.st_gid = body->mbo_gid;
1847 st.st_rdev = body->mbo_rdev;
1848 st.st_size = body->mbo_size;
1849 st.st_blksize = PAGE_SIZE;
1850 st.st_blocks = body->mbo_blocks;
1851 st.st_atime = body->mbo_atime;
1852 st.st_mtime = body->mbo_mtime;
1853 st.st_ctime = body->mbo_ctime;
1854 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1858 if (copy_to_user(statp, &st, sizeof(st)))
1859 GOTO(out_req, rc = -EFAULT);
1860 } else if (cmd == IOC_MDC_GETFILEINFO ||
1861 cmd == LL_IOC_MDC_GETINFO) {
1862 lstatx_t stx = { 0 };
1863 __u64 valid = body->mbo_valid;
1865 stx.stx_blksize = PAGE_SIZE;
1866 stx.stx_nlink = body->mbo_nlink;
1867 stx.stx_uid = body->mbo_uid;
1868 stx.stx_gid = body->mbo_gid;
1869 stx.stx_mode = body->mbo_mode;
1870 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1873 stx.stx_size = body->mbo_size;
1874 stx.stx_blocks = body->mbo_blocks;
1875 stx.stx_atime.tv_sec = body->mbo_atime;
1876 stx.stx_ctime.tv_sec = body->mbo_ctime;
1877 stx.stx_mtime.tv_sec = body->mbo_mtime;
1878 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1879 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1880 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1881 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1882 stx.stx_mask |= STATX_BASIC_STATS;
1885 * For a striped directory, the size and blocks returned
1886 * from MDT is not correct.
1887 * The size and blocks are aggregated by client across
1889 * Thus for a striped directory, do not return the valid
1890 * FLSIZE and FLBLOCKS flags to the caller.
1891 * However, this whould be better decided by the MDS
1892 * instead of the client.
1894 if (cmd == LL_IOC_MDC_GETINFO &&
1895 ll_i2info(inode)->lli_lsm_md != NULL)
1896 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1898 if (flagsp && copy_to_user(flagsp, &valid,
1900 GOTO(out_req, rc = -EFAULT);
1902 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1904 GOTO(out_req, rc = -EFAULT);
1906 if (!(valid & OBD_MD_FLSIZE))
1907 stx.stx_mask &= ~STATX_SIZE;
1908 if (!(valid & OBD_MD_FLBLOCKS))
1909 stx.stx_mask &= ~STATX_BLOCKS;
1911 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1912 GOTO(out_req, rc = -EFAULT);
1914 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1916 GOTO(out_req, rc = -EFAULT);
1921 ptlrpc_req_finished(request);
1922 ptlrpc_req_finished(root_request);
1924 ll_putname(filename);
1927 case OBD_IOC_QUOTACTL: {
1928 struct if_quotactl *qctl;
1930 OBD_ALLOC_PTR(qctl);
1934 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1935 GOTO(out_quotactl, rc = -EFAULT);
1937 rc = quotactl_ioctl(sbi, qctl);
1940 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1947 case OBD_IOC_GETDTNAME:
1948 case OBD_IOC_GETMDNAME:
1949 RETURN(ll_get_obd_name(inode, cmd, arg));
1950 case LL_IOC_FLUSHCTX:
1951 RETURN(ll_flush_ctx(inode));
1952 case LL_IOC_GETOBDCOUNT: {
1954 struct obd_export *exp;
1956 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1959 /* get ost count when count is zero, get mdt count otherwise */
1960 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1961 vallen = sizeof(count);
1962 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1963 KEY_TGT_COUNT, &vallen, &count);
1965 CERROR("get target count failed: %d\n", rc);
1969 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1974 case LL_IOC_PATH2FID:
1975 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1976 sizeof(struct lu_fid)))
1979 case LL_IOC_GET_CONNECT_FLAGS: {
1980 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1981 (void __user *)arg));
1983 case OBD_IOC_FID2PATH:
1984 RETURN(ll_fid2path(inode, (void __user *)arg));
1985 case LL_IOC_GETPARENT:
1986 RETURN(ll_getparent(file, (void __user *)arg));
1987 case LL_IOC_FID2MDTIDX: {
1988 struct obd_export *exp = ll_i2mdexp(inode);
1992 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1996 /* Call mdc_iocontrol */
1997 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1998 (__u32 __user *)&index);
2004 case LL_IOC_HSM_REQUEST: {
2005 struct hsm_user_request *hur;
2012 /* We don't know the true size yet; copy the fixed-size part */
2013 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
2018 /* Compute the whole struct size */
2019 totalsize = hur_len(hur);
2024 /* Final size will be more than double totalsize */
2025 if (totalsize >= MDS_MAXREQSIZE / 3)
2028 OBD_ALLOC_LARGE(hur, totalsize);
2032 /* Copy the whole struct */
2033 if (copy_from_user(hur, (void __user *)arg, totalsize))
2034 GOTO(out_hur, rc = -EFAULT);
2036 if (hur->hur_request.hr_action == HUA_RELEASE) {
2037 const struct lu_fid *fid;
2041 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2042 fid = &hur->hur_user_item[i].hui_fid;
2043 f = search_inode_for_lustre(inode->i_sb, fid);
2049 rc = ll_hsm_release(f);
2055 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2060 OBD_FREE_LARGE(hur, totalsize);
2064 case LL_IOC_HSM_PROGRESS: {
2065 struct hsm_progress_kernel hpk;
2066 struct hsm_progress hp;
2068 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2071 hpk.hpk_fid = hp.hp_fid;
2072 hpk.hpk_cookie = hp.hp_cookie;
2073 hpk.hpk_extent = hp.hp_extent;
2074 hpk.hpk_flags = hp.hp_flags;
2075 hpk.hpk_errval = hp.hp_errval;
2076 hpk.hpk_data_version = 0;
2078 /* File may not exist in Lustre; all progress
2079 * reported to Lustre root */
2080 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2084 case LL_IOC_HSM_CT_START:
2085 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2088 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2089 (struct lustre_kernelcomm __user *)arg);
2092 case LL_IOC_HSM_COPY_START: {
2093 struct hsm_copy *copy;
2096 OBD_ALLOC_PTR(copy);
2099 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2104 rc = ll_ioc_copy_start(inode->i_sb, copy);
2105 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2111 case LL_IOC_HSM_COPY_END: {
2112 struct hsm_copy *copy;
2115 OBD_ALLOC_PTR(copy);
2118 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2123 rc = ll_ioc_copy_end(inode->i_sb, copy);
2124 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2130 case LL_IOC_MIGRATE: {
2131 struct lmv_user_md *lum;
2138 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2142 data = (struct obd_ioctl_data *)buf;
2143 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2144 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2145 GOTO(migrate_free, rc = -EINVAL);
2147 filename = data->ioc_inlbuf1;
2148 namelen = data->ioc_inllen1;
2150 if (namelen < 1 || namelen != strlen(filename) + 1) {
2151 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2152 GOTO(migrate_free, rc = -EINVAL);
2155 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2156 if (lum->lum_magic != LMV_USER_MAGIC &&
2157 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2159 CERROR("%s: wrong lum magic %x: rc = %d\n",
2160 filename, lum->lum_magic, rc);
2161 GOTO(migrate_free, rc);
2164 rc = ll_migrate(inode, file, lum, filename);
2166 OBD_FREE_LARGE(buf, len);
2170 case LL_IOC_FSGETXATTR:
2171 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2172 case LL_IOC_FSSETXATTR:
2173 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2174 case LL_IOC_PCC_DETACH_BY_FID: {
2175 struct lu_pcc_detach_fid *detach;
2177 struct inode *inode2;
2180 OBD_ALLOC_PTR(detach);
2184 if (copy_from_user(detach,
2185 (const struct lu_pcc_detach_fid __user *)arg,
2187 GOTO(out_detach, rc = -EFAULT);
2189 fid = &detach->pccd_fid;
2190 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2191 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2193 /* Target inode is not in inode cache, and PCC file
2194 * has aleady released, return immdiately.
2196 GOTO(out_detach, rc = 0);
2198 if (!S_ISREG(inode2->i_mode))
2199 GOTO(out_iput, rc = -EINVAL);
2201 if (!inode_owner_or_capable(inode2))
2202 GOTO(out_iput, rc = -EPERM);
2204 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2208 OBD_FREE_PTR(detach);
2212 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2213 (void __user *)arg));
2217 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2219 struct inode *inode = file->f_mapping->host;
2220 struct ll_file_data *fd = file->private_data;
2221 struct ll_sb_info *sbi = ll_i2sbi(inode);
2222 int api32 = ll_need_32bit_api(sbi);
2223 loff_t ret = -EINVAL;
2231 offset += file->f_pos;
2237 offset += LL_DIR_END_OFF_32BIT;
2239 offset += LL_DIR_END_OFF;
2246 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2247 (!api32 && offset <= LL_DIR_END_OFF))) {
2248 if (offset != file->f_pos) {
2249 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2250 (!api32 && offset == LL_DIR_END_OFF))
2251 fd->lfd_pos = MDS_DIR_END_OFF;
2252 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2253 fd->lfd_pos = offset << 32;
2255 fd->lfd_pos = offset;
2256 file->f_pos = offset;
2257 file->f_version = 0;
2264 inode_unlock(inode);
2268 static int ll_dir_open(struct inode *inode, struct file *file)
2271 RETURN(ll_file_open(inode, file));
2274 static int ll_dir_release(struct inode *inode, struct file *file)
2277 RETURN(ll_file_release(inode, file));
2280 const struct file_operations ll_dir_operations = {
2281 .llseek = ll_dir_seek,
2282 .open = ll_dir_open,
2283 .release = ll_dir_release,
2284 .read = generic_read_dir,
2285 #ifdef HAVE_DIR_CONTEXT
2286 .iterate_shared = ll_iterate,
2288 .readdir = ll_readdir,
2290 .unlocked_ioctl = ll_dir_ioctl,