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 && ll_dir_striped(inode)) {
173 if (likely(page->mapping != NULL))
174 delete_from_page_cache(page);
181 * return IF_* type for given lu_dirent entry.
182 * IF_* flag shld be converted to particular OS file type in
183 * platform llite module.
185 static u16 ll_dirent_type_get(struct lu_dirent *ent)
188 struct luda_type *lt;
191 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
192 const unsigned align = sizeof(struct luda_type) - 1;
194 len = le16_to_cpu(ent->lde_namelen);
195 len = (len + align) & ~align;
196 lt = (void *)ent->lde_name + len;
197 type = IFTODT(le16_to_cpu(lt->lt_type));
203 #ifdef HAVE_DIR_CONTEXT
204 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
205 struct dir_context *ctx)
208 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
209 void *cookie, filldir_t filldir)
212 struct ll_sb_info *sbi = ll_i2sbi(inode);
214 bool is_api32 = ll_need_32bit_api(sbi);
215 bool is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
217 struct ll_dir_chain chain;
222 ll_dir_chain_init(&chain);
224 page = ll_get_dir_page(inode, op_data, pos, &chain);
226 while (rc == 0 && !done) {
227 struct lu_dirpage *dp;
228 struct lu_dirent *ent;
237 hash = MDS_DIR_END_OFF;
238 dp = page_address(page);
239 for (ent = lu_dirent_start(dp); ent != NULL && !done;
240 ent = lu_dirent_next(ent)) {
247 hash = le64_to_cpu(ent->lde_hash);
248 if (hash < pos) /* Skip until we find target hash */
251 namelen = le16_to_cpu(ent->lde_namelen);
252 if (namelen == 0) /* Skip dummy record */
255 if (is_api32 && is_hash64)
259 fid_le_to_cpu(&fid, &ent->lde_fid);
260 ino = cl_fid_build_ino(&fid, is_api32);
261 type = ll_dirent_type_get(ent);
262 /* For ll_nfs_get_name_filldir(), it will try to access
263 * 'ent' through 'lde_name', so the parameter 'name'
264 * for 'filldir()' must be part of the 'ent'. */
265 #ifdef HAVE_DIR_CONTEXT
267 done = !dir_emit(ctx, ent->lde_name, namelen, ino,
270 done = filldir(cookie, ent->lde_name, namelen, lhash,
277 ll_release_page(inode, page, false);
281 next = le64_to_cpu(dp->ldp_hash_end);
283 if (pos == MDS_DIR_END_OFF) {
285 * End of directory reached.
288 ll_release_page(inode, page, false);
291 * Normal case: continue to the next
294 ll_release_page(inode, page,
295 le32_to_cpu(dp->ldp_flags) &
298 page = ll_get_dir_page(inode, op_data, pos,
302 #ifdef HAVE_DIR_CONTEXT
307 ll_dir_chain_fini(&chain);
311 #ifdef HAVE_DIR_CONTEXT
312 static int ll_iterate(struct file *filp, struct dir_context *ctx)
314 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
317 struct inode *inode = file_inode(filp);
318 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
319 struct ll_sb_info *sbi = ll_i2sbi(inode);
320 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
321 int api32 = ll_need_32bit_api(sbi);
322 struct md_op_data *op_data;
323 struct lu_fid pfid = { 0 };
324 ktime_t kstart = ktime_get();
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_dir_striped(inode))) {
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));
373 /* foreign dirs are browsed out of Lustre */
374 if (unlikely(op_data->op_mea1 != NULL &&
375 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
376 ll_finish_md_op_data(op_data);
380 op_data->op_fid3 = pfid;
382 #ifdef HAVE_DIR_CONTEXT
384 rc = ll_dir_read(inode, &pos, op_data, ctx);
387 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
392 if (pos == MDS_DIR_END_OFF) {
394 pos = LL_DIR_END_OFF_32BIT;
396 pos = LL_DIR_END_OFF;
401 #ifdef HAVE_DIR_CONTEXT
406 ll_finish_md_op_data(op_data);
410 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
411 ktime_us_delta(ktime_get(), kstart));
416 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
417 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
419 struct mgs_send_param *msp;
426 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
427 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
428 sizeof(struct mgs_send_param), msp, NULL);
430 CERROR("Failed to set parameter: %d\n", rc);
438 * Create striped directory with specified stripe(@lump)
440 * \param[in] dparent the parent of the directory.
441 * \param[in] lump the specified stripes.
442 * \param[in] dirname the name of the directory.
443 * \param[in] mode the specified mode of the directory.
445 * \retval =0 if striped directory is being created successfully.
446 * <0 if the creation is failed.
448 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
449 size_t len, const char *dirname, umode_t mode)
451 struct inode *parent = dparent->d_inode;
452 struct ptlrpc_request *request = NULL;
453 struct md_op_data *op_data;
454 struct ll_sb_info *sbi = ll_i2sbi(parent);
455 struct inode *inode = NULL;
456 struct dentry dentry = {
460 .len = strlen(dirname),
461 .hash = ll_full_name_hash(dparent, dirname,
468 if (unlikely(!lmv_magic_supported(lump->lum_magic)))
471 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
473 "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
474 PFID(ll_inode2fid(parent)), parent, dirname,
475 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
477 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
480 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
481 PFID(ll_inode2fid(parent)), parent, dirname,
482 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
485 if (lump->lum_stripe_count > 1 &&
486 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
489 if (IS_DEADDIR(parent) &&
490 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
493 if (unlikely(!lmv_magic_supported(cpu_to_le32(lump->lum_magic))))
494 lustre_swab_lmv_user_md(lump);
496 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
497 mode &= ~current_umask();
498 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
499 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
500 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
503 RETURN(PTR_ERR(op_data));
505 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
506 /* selinux_dentry_init_security() uses dentry->d_parent and name
507 * to determine the security context for the file. So our fake
508 * dentry should be real enough for this purpose. */
509 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
510 &op_data->op_file_secctx_name,
511 &op_data->op_file_secctx,
512 &op_data->op_file_secctx_size);
514 GOTO(out_op_data, err);
517 op_data->op_cli_flags |= CLI_SET_MEA;
518 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
519 from_kuid(&init_user_ns, current_fsuid()),
520 from_kgid(&init_user_ns, current_fsgid()),
521 cfs_curproc_cap_pack(), 0, &request);
523 GOTO(out_request, err);
525 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
527 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
529 GOTO(out_inode, err);
531 dentry.d_inode = inode;
533 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
535 err = security_inode_notifysecctx(inode,
536 op_data->op_file_secctx,
537 op_data->op_file_secctx_size);
540 err = ll_inode_init_security(&dentry, inode, parent);
543 GOTO(out_inode, err);
549 ptlrpc_req_finished(request);
551 ll_finish_md_op_data(op_data);
556 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
559 struct ll_sb_info *sbi = ll_i2sbi(inode);
560 struct md_op_data *op_data;
561 struct ptlrpc_request *req = NULL;
563 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
564 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
565 struct obd_device *mgc = lsi->lsi_mgc;
571 switch (lump->lmm_magic) {
572 case LOV_USER_MAGIC_V1:
573 lum_size = sizeof(struct lov_user_md_v1);
575 case LOV_USER_MAGIC_V3:
576 lum_size = sizeof(struct lov_user_md_v3);
578 case LOV_USER_MAGIC_COMP_V1:
579 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
582 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
583 lustre_swab_lmv_user_md(
584 (struct lmv_user_md *)lump);
585 lum_size = sizeof(struct lmv_user_md);
587 case LOV_USER_MAGIC_SPECIFIC: {
588 struct lov_user_md_v3 *v3 =
589 (struct lov_user_md_v3 *)lump;
590 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
592 lum_size = lov_user_md_size(v3->lmm_stripe_count,
593 LOV_USER_MAGIC_SPECIFIC);
597 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
598 " %#08x != %#08x nor %#08x\n",
599 lump->lmm_magic, LOV_USER_MAGIC_V1,
605 * This is coming from userspace, so should be in
606 * local endian. But the MDS would like it in little
607 * endian, so we swab it before we send it.
609 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
610 le32_to_cpu(LOV_MAGIC_MAGIC))
611 lustre_swab_lov_user_md(lump, 0);
613 lum_size = sizeof(struct lov_user_md_v1);
616 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
617 LUSTRE_OPC_ANY, NULL);
619 RETURN(PTR_ERR(op_data));
621 /* swabbing is done in lov_setstripe() on server side */
622 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
623 ll_finish_md_op_data(op_data);
624 ptlrpc_req_finished(req);
628 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
630 * 2.9 server has stored filesystem default stripe in ROOT xattr,
631 * and it's stored into system config for backward compatibility.
633 * In the following we use the fact that LOV_USER_MAGIC_V1 and
634 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
635 * need the make the distiction between the 2 versions
637 if (set_default && mgc->u.cli.cl_mgc_mgsexp &&
639 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V1 ||
640 le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V3)) {
644 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
646 GOTO(end, rc = -ENOMEM);
649 /* Get fsname and assume devname to be -MDT0000. */
650 snprintf(buf, MGS_PARAM_MAXLEN, "%s-MDT0000.lov",
654 /* Set root stripesize */
655 snprintf(buf, MGS_PARAM_MAXLEN, ".stripesize=%u",
656 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
657 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
661 /* Set root stripecount */
662 snprintf(buf, MGS_PARAM_MAXLEN, ".stripecount=%hd",
663 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
664 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
668 /* Set root stripeoffset */
669 snprintf(buf, MGS_PARAM_MAXLEN, ".stripeoffset=%hd",
670 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
671 (typeof(lump->lmm_stripe_offset))(-1));
672 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
676 OBD_FREE(param, MGS_PARAM_MAXLEN);
682 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
684 struct ptlrpc_request **request, u64 valid,
685 enum get_default_layout_type type)
687 struct ll_sb_info *sbi = ll_i2sbi(inode);
688 struct mdt_body *body;
689 struct lov_mds_md *lmm = NULL;
690 struct ptlrpc_request *req = NULL;
692 struct md_op_data *op_data;
696 rc = ll_get_default_mdsize(sbi, &lmm_size);
700 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
701 0, lmm_size, LUSTRE_OPC_ANY,
704 RETURN(PTR_ERR(op_data));
706 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
708 if (type == GET_DEFAULT_LAYOUT_ROOT) {
709 lu_root_fid(&op_data->op_fid1);
710 fid = op_data->op_fid1;
712 fid = *ll_inode2fid(inode);
715 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
716 ll_finish_md_op_data(op_data);
718 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
723 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
724 LASSERT(body != NULL);
726 lmm_size = body->mbo_eadatasize;
728 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
730 GOTO(out, rc = -ENODATA);
733 lmm = req_capsule_server_sized_get(&req->rq_pill,
734 &RMF_MDT_MD, lmm_size);
735 LASSERT(lmm != NULL);
738 * This is coming from the MDS, so is probably in
739 * little endian. We convert it to host endian before
740 * passing it to userspace.
742 /* We don't swab objects for directories */
743 switch (le32_to_cpu(lmm->lmm_magic)) {
746 case LOV_MAGIC_COMP_V1:
747 case LOV_USER_MAGIC_SPECIFIC:
748 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
749 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
752 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
753 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
756 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
757 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
759 case LMV_MAGIC_FOREIGN: {
760 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
762 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
763 __swab32s(&lfm->lfm_magic);
764 __swab32s(&lfm->lfm_length);
765 __swab32s(&lfm->lfm_type);
766 __swab32s(&lfm->lfm_flags);
771 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
776 *plmm_size = lmm_size;
782 * This function will be used to get default LOV/LMV/Default LMV
783 * @valid will be used to indicate which stripe it will retrieve.
784 * If the directory does not have its own default layout, then the
785 * function will request the default layout from root FID.
786 * OBD_MD_MEA LMV stripe EA
787 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
788 * otherwise Default LOV EA.
789 * Each time, it can only retrieve 1 stripe EA
791 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
792 struct ptlrpc_request **request,
793 struct ptlrpc_request **root_request,
796 struct ptlrpc_request *req = NULL;
797 struct ptlrpc_request *root_req = NULL;
798 struct lov_mds_md *lmm = NULL;
803 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
805 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
806 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
807 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
809 GET_DEFAULT_LAYOUT_ROOT);
812 *plmm_size = lmm_size;
814 if (root_request != NULL)
815 *root_request = root_req;
821 * This function will be used to get default LOV/LMV/Default LMV
822 * @valid will be used to indicate which stripe it will retrieve
823 * OBD_MD_MEA LMV stripe EA
824 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
825 * otherwise Default LOV EA.
826 * Each time, it can only retrieve 1 stripe EA
828 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
829 struct ptlrpc_request **request, u64 valid)
831 struct ptlrpc_request *req = NULL;
832 struct lov_mds_md *lmm = NULL;
837 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
841 *plmm_size = lmm_size;
847 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
849 struct md_op_data *op_data;
854 OBD_ALLOC_PTR(op_data);
858 op_data->op_flags |= MF_GET_MDT_IDX;
859 op_data->op_fid1 = *fid;
860 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
861 mdt_index = op_data->op_mds;
862 OBD_FREE_PTR(op_data);
870 * Get MDT index for the inode.
872 int ll_get_mdt_idx(struct inode *inode)
874 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
878 * Generic handler to do any pre-copy work.
880 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
881 * first information for it that real work has started.
883 * Moreover, for a ARCHIVE request, it will sample the file data version and
884 * store it in \a copy.
886 * \return 0 on success.
888 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
890 struct ll_sb_info *sbi = ll_s2sbi(sb);
891 struct hsm_progress_kernel hpk;
896 /* Forge a hsm_progress based on data from copy. */
897 hpk.hpk_fid = copy->hc_hai.hai_fid;
898 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
899 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
900 hpk.hpk_extent.length = 0;
903 hpk.hpk_data_version = 0;
906 /* For archive request, we need to read the current file version. */
907 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
909 __u64 data_version = 0;
911 /* Get inode for this fid */
912 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
914 hpk.hpk_flags |= HP_FLAG_RETRY;
915 /* hpk_errval is >= 0 */
916 hpk.hpk_errval = -PTR_ERR(inode);
917 GOTO(progress, rc = PTR_ERR(inode));
920 /* Read current file data version */
921 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
924 CDEBUG(D_HSM, "Could not read file data version of "
925 DFID" (rc = %d). Archive request ("
926 "%#llx) could not be done.\n",
927 PFID(©->hc_hai.hai_fid), rc,
928 copy->hc_hai.hai_cookie);
929 hpk.hpk_flags |= HP_FLAG_RETRY;
930 /* hpk_errval must be >= 0 */
931 hpk.hpk_errval = -rc;
935 /* Store in the hsm_copy for later copytool use.
936 * Always modified even if no lsm. */
937 copy->hc_data_version = data_version;
941 /* On error, the request should be considered as completed */
942 if (hpk.hpk_errval > 0)
943 hpk.hpk_flags |= HP_FLAG_COMPLETED;
945 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
948 /* Return first error */
949 RETURN(rc != 0 ? rc : rc2);
953 * Generic handler to do any post-copy work.
955 * It will send the last hsm_progress update to coordinator to inform it
956 * that copy is finished and whether it was successful or not.
959 * - for ARCHIVE request, it will sample the file data version and compare it
960 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
961 * will be considered as failed.
962 * - for RESTORE request, it will sample the file data version and send it to
963 * coordinator which is useful if the file was imported as 'released'.
965 * \return 0 on success.
967 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
969 struct ll_sb_info *sbi = ll_s2sbi(sb);
970 struct hsm_progress_kernel hpk;
975 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
976 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
977 * initialized if copy_end was called with copy == NULL.
980 /* Forge a hsm_progress based on data from copy. */
981 hpk.hpk_fid = copy->hc_hai.hai_fid;
982 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
983 hpk.hpk_extent = copy->hc_hai.hai_extent;
984 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
985 hpk.hpk_errval = copy->hc_errval;
986 hpk.hpk_data_version = 0;
988 /* For archive request, we need to check the file data was not changed.
990 * For restore request, we need to send the file data version, this is
991 * useful when the file was created using hsm_import.
993 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
994 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
995 (copy->hc_errval == 0)) {
997 __u64 data_version = 0;
999 /* Get lsm for this fid */
1000 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1001 if (IS_ERR(inode)) {
1002 hpk.hpk_flags |= HP_FLAG_RETRY;
1003 /* hpk_errval must be >= 0 */
1004 hpk.hpk_errval = -PTR_ERR(inode);
1005 GOTO(progress, rc = PTR_ERR(inode));
1008 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1011 CDEBUG(D_HSM, "Could not read file data version. "
1012 "Request could not be confirmed.\n");
1013 if (hpk.hpk_errval == 0)
1014 hpk.hpk_errval = -rc;
1018 /* Store in the hsm_copy for later copytool use.
1019 * Always modified even if no lsm. */
1020 hpk.hpk_data_version = data_version;
1022 /* File could have been stripped during archiving, so we need
1023 * to check anyway. */
1024 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1025 (copy->hc_data_version != data_version)) {
1026 CDEBUG(D_HSM, "File data version mismatched. "
1027 "File content was changed during archiving. "
1028 DFID", start:%#llx current:%#llx\n",
1029 PFID(©->hc_hai.hai_fid),
1030 copy->hc_data_version, data_version);
1031 /* File was changed, send error to cdt. Do not ask for
1032 * retry because if a file is modified frequently,
1033 * the cdt will loop on retried archive requests.
1034 * The policy engine will ask for a new archive later
1035 * when the file will not be modified for some tunable
1037 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1039 /* hpk_errval must be >= 0 */
1040 hpk.hpk_errval = -rc;
1047 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1050 /* Return first error */
1051 RETURN(rc != 0 ? rc : rc2);
1055 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1056 const struct lustre_kernelcomm __user *data)
1058 struct lustre_kernelcomm *lk;
1059 struct lustre_kernelcomm *tmp;
1060 size_t size = sizeof(*lk);
1065 /* copy data from userspace to get numbers of archive_id */
1066 OBD_ALLOC(lk, size);
1070 if (copy_from_user(lk, data, size))
1071 GOTO(out_lk, rc = -EFAULT);
1073 if (lk->lk_flags & LK_FLG_STOP)
1076 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1077 __u32 archive_mask = lk->lk_data_count;
1080 /* old hsm agent to old MDS */
1081 if (!exp_connect_archive_id_array(exp))
1084 /* old hsm agent to new MDS */
1085 lk->lk_flags |= LK_FLG_DATANR;
1087 if (archive_mask == 0)
1090 count = hweight32(archive_mask);
1091 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1092 OBD_ALLOC(tmp, new_size);
1094 GOTO(out_lk, rc = -ENOMEM);
1096 memcpy(tmp, lk, size);
1097 tmp->lk_data_count = count;
1103 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1104 if ((1 << i) & archive_mask) {
1105 lk->lk_data[count] = i + 1;
1112 /* new hsm agent to new mds */
1113 if (lk->lk_data_count > 0) {
1114 new_size = offsetof(struct lustre_kernelcomm,
1115 lk_data[lk->lk_data_count]);
1116 OBD_ALLOC(tmp, new_size);
1118 GOTO(out_lk, rc = -ENOMEM);
1124 if (copy_from_user(lk, data, size))
1125 GOTO(out_lk, rc = -EFAULT);
1128 /* new hsm agent to old MDS */
1129 if (!exp_connect_archive_id_array(exp)) {
1132 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1133 GOTO(out_lk, rc = -EINVAL);
1135 for (i = 0; i < lk->lk_data_count; i++) {
1136 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1138 CERROR("%s: archive id %d requested but only "
1139 "[0 - %zu] supported: rc = %d\n",
1140 exp->exp_obd->obd_name, lk->lk_data[i],
1141 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1145 if (lk->lk_data[i] == 0) {
1150 archives |= (1 << (lk->lk_data[i] - 1));
1152 lk->lk_flags &= ~LK_FLG_DATANR;
1153 lk->lk_data_count = archives;
1156 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1162 static int check_owner(int type, int id)
1166 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1170 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1179 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1181 int cmd = qctl->qc_cmd;
1182 int type = qctl->qc_type;
1183 int id = qctl->qc_id;
1184 int valid = qctl->qc_valid;
1191 case LUSTRE_Q_SETDEFAULT:
1192 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1196 case LUSTRE_Q_GETDEFAULT:
1197 if (check_owner(type, id) &&
1198 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1204 CERROR("unsupported quotactl op: %#x\n", cmd);
1208 if (valid != QC_GENERAL) {
1209 if (cmd == Q_GETINFO)
1210 qctl->qc_cmd = Q_GETOINFO;
1211 else if (cmd == Q_GETQUOTA)
1212 qctl->qc_cmd = Q_GETOQUOTA;
1218 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1219 sizeof(*qctl), qctl, NULL);
1222 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1223 sizeof(*qctl), qctl, NULL);
1226 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1227 sizeof(*qctl), qctl, NULL);
1229 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1231 sizeof(*qctl), qctl, NULL);
1243 struct obd_quotactl *oqctl;
1245 OBD_ALLOC_PTR(oqctl);
1249 QCTL_COPY(oqctl, qctl);
1250 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1252 OBD_FREE_PTR(oqctl);
1255 /* If QIF_SPACE is not set, client should collect the
1256 * space usage from OSSs by itself */
1257 if (cmd == Q_GETQUOTA &&
1258 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1259 !oqctl->qc_dqblk.dqb_curspace) {
1260 struct obd_quotactl *oqctl_tmp;
1262 OBD_ALLOC_PTR(oqctl_tmp);
1263 if (oqctl_tmp == NULL)
1264 GOTO(out, rc = -ENOMEM);
1266 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1267 oqctl_tmp->qc_id = oqctl->qc_id;
1268 oqctl_tmp->qc_type = oqctl->qc_type;
1270 /* collect space usage from OSTs */
1271 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1272 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1273 if (!rc || rc == -EREMOTEIO) {
1274 oqctl->qc_dqblk.dqb_curspace =
1275 oqctl_tmp->qc_dqblk.dqb_curspace;
1276 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1279 /* collect space & inode usage from MDTs */
1280 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1281 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1282 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1283 if (!rc || rc == -EREMOTEIO) {
1284 oqctl->qc_dqblk.dqb_curspace +=
1285 oqctl_tmp->qc_dqblk.dqb_curspace;
1286 oqctl->qc_dqblk.dqb_curinodes =
1287 oqctl_tmp->qc_dqblk.dqb_curinodes;
1288 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1290 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1293 OBD_FREE_PTR(oqctl_tmp);
1296 QCTL_COPY(qctl, oqctl);
1297 OBD_FREE_PTR(oqctl);
1303 int ll_rmfid(struct file *file, void __user *arg)
1305 const struct fid_array __user *ufa = arg;
1306 struct fid_array *lfa = NULL;
1309 int i, rc, *rcs = NULL;
1312 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1313 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1315 /* Only need to get the buflen */
1316 if (get_user(nr, &ufa->fa_nr))
1318 /* DoS protection */
1319 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1322 size = offsetof(struct fid_array, fa_fids[nr]);
1323 OBD_ALLOC(lfa, size);
1326 OBD_ALLOC(rcs, sizeof(int) * nr);
1328 GOTO(free_lfa, rc = -ENOMEM);
1330 if (copy_from_user(lfa, arg, size))
1331 GOTO(free_rcs, rc = -EFAULT);
1333 /* Call mdc_iocontrol */
1334 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1336 for (i = 0; i < nr; i++)
1338 lfa->fa_fids[i].f_ver = rcs[i];
1339 if (copy_to_user(arg, lfa, size))
1344 OBD_FREE(rcs, sizeof(int) * nr);
1346 OBD_FREE(lfa, size);
1351 /* This function tries to get a single name component,
1352 * to send to the server. No actual path traversal involved,
1353 * so we limit to NAME_MAX */
1354 static char *ll_getname(const char __user *filename)
1359 OBD_ALLOC(tmp, NAME_MAX + 1);
1362 return ERR_PTR(-ENOMEM);
1364 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1367 else if (len > NAME_MAX)
1368 ret = -ENAMETOOLONG;
1371 OBD_FREE(tmp, NAME_MAX + 1);
1377 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1379 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1381 struct dentry *dentry = file_dentry(file);
1382 struct inode *inode = file_inode(file);
1383 struct ll_sb_info *sbi = ll_i2sbi(inode);
1384 struct obd_ioctl_data *data;
1388 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1389 PFID(ll_inode2fid(inode)), inode, cmd);
1391 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1392 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1395 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1397 case FS_IOC_GETFLAGS:
1398 case FS_IOC_SETFLAGS:
1399 RETURN(ll_iocontrol(inode, file, cmd, arg));
1400 case FSFILT_IOC_GETVERSION:
1401 case FS_IOC_GETVERSION:
1402 RETURN(put_user(inode->i_generation, (int __user *)arg));
1403 /* We need to special case any other ioctls we want to handle,
1404 * to send them to the MDS/OST as appropriate and to properly
1405 * network encode the arg field. */
1406 case FS_IOC_SETVERSION:
1409 case LL_IOC_GET_MDTIDX: {
1412 mdtidx = ll_get_mdt_idx(inode);
1416 if (put_user((int)mdtidx, (int __user *)arg))
1421 case IOC_MDC_LOOKUP: {
1422 int namelen, len = 0;
1426 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1431 filename = data->ioc_inlbuf1;
1432 namelen = strlen(filename);
1434 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1435 GOTO(out_free, rc = -EINVAL);
1438 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1440 CERROR("%s: lookup %.*s failed: rc = %d\n",
1441 sbi->ll_fsname, namelen, filename, rc);
1445 OBD_FREE_LARGE(buf, len);
1448 case LL_IOC_LMV_SETSTRIPE: {
1449 struct lmv_user_md *lum;
1458 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1463 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1464 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1465 GOTO(lmv_out_free, rc = -EINVAL);
1467 filename = data->ioc_inlbuf1;
1468 namelen = data->ioc_inllen1;
1471 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1472 GOTO(lmv_out_free, rc = -EINVAL);
1474 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1475 lumlen = data->ioc_inllen2;
1477 if (!lmv_magic_supported(lum->lum_magic)) {
1478 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1479 lum->lum_magic, -EINVAL);
1480 GOTO(lmv_out_free, rc = -EINVAL);
1483 if ((lum->lum_magic == LMV_USER_MAGIC ||
1484 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1485 lumlen < sizeof(*lum)) {
1486 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1487 filename, lumlen, lum->lum_magic, -EINVAL);
1488 GOTO(lmv_out_free, rc = -EINVAL);
1491 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1492 lumlen < sizeof(struct lmv_foreign_md)) {
1493 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1494 filename, lum->lum_magic, lumlen, -EFAULT);
1495 GOTO(lmv_out_free, rc = -EINVAL);
1498 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1499 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1501 mode = data->ioc_type;
1503 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1505 OBD_FREE_LARGE(buf, len);
1509 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1510 struct lmv_user_md lum;
1511 struct lmv_user_md __user *ulump =
1512 (struct lmv_user_md __user *)arg;
1515 if (copy_from_user(&lum, ulump, sizeof(lum)))
1518 if (lum.lum_magic != LMV_USER_MAGIC)
1521 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1525 case LL_IOC_LOV_SETSTRIPE_NEW:
1526 case LL_IOC_LOV_SETSTRIPE: {
1527 struct lov_user_md_v3 *lumv3 = NULL;
1528 struct lov_user_md_v1 lumv1;
1529 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1530 struct lov_user_md_v1 __user *lumv1p =
1531 (struct lov_user_md_v1 __user *)arg;
1532 struct lov_user_md_v3 __user *lumv3p =
1533 (struct lov_user_md_v3 __user *)arg;
1536 int set_default = 0;
1538 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1539 sizeof(struct lov_comp_md_v1));
1540 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1541 /* first try with v1 which is smaller than v3 */
1542 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1545 if (inode->i_sb->s_root == file_dentry(file))
1548 switch (lumv1.lmm_magic) {
1549 case LOV_USER_MAGIC_V3:
1550 case LOV_USER_MAGIC_SPECIFIC:
1551 lum_size = ll_lov_user_md_size(&lumv1);
1554 OBD_ALLOC(lumv3, lum_size);
1557 if (copy_from_user(lumv3, lumv3p, lum_size))
1558 GOTO(out, rc = -EFAULT);
1559 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1561 case LOV_USER_MAGIC_V1:
1564 GOTO(out, rc = -ENOTSUPP);
1567 /* in v1 and v3 cases lumv1 points to data */
1568 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1571 OBD_FREE(lumv3, lum_size);
1574 case LL_IOC_LMV_GETSTRIPE: {
1575 struct lmv_user_md __user *ulmv =
1576 (struct lmv_user_md __user *)arg;
1577 struct lmv_user_md lum;
1578 struct ptlrpc_request *request = NULL;
1579 struct ptlrpc_request *root_request = NULL;
1580 union lmv_mds_md *lmm = NULL;
1583 struct lmv_user_md *tmp = NULL;
1587 int max_stripe_count;
1591 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1594 max_stripe_count = lum.lum_stripe_count;
1595 /* lum_magic will indicate which stripe the ioctl will like
1596 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1597 * is for default LMV stripe */
1598 if (lum.lum_magic == LMV_MAGIC_V1)
1599 valid |= OBD_MD_MEA;
1600 else if (lum.lum_magic == LMV_USER_MAGIC)
1601 valid |= OBD_MD_DEFAULT_MEA;
1605 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1606 &request, &root_request, valid);
1608 GOTO(finish_req, rc);
1610 /* Get default LMV EA */
1611 if (lum.lum_magic == LMV_USER_MAGIC) {
1612 if (lmmsize > sizeof(*ulmv))
1613 GOTO(finish_req, rc = -EINVAL);
1615 if (copy_to_user(ulmv, lmm, lmmsize))
1616 GOTO(finish_req, rc = -EFAULT);
1618 GOTO(finish_req, rc);
1621 /* if foreign LMV case, fake stripes number */
1622 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1623 struct lmv_foreign_md *lfm;
1625 lfm = (struct lmv_foreign_md *)lmm;
1626 if (lfm->lfm_length < XATTR_SIZE_MAX -
1627 offsetof(typeof(*lfm), lfm_value)) {
1628 __u32 size = lfm->lfm_length +
1629 offsetof(typeof(*lfm), lfm_value);
1631 stripe_count = lmv_foreign_to_md_stripes(size);
1633 CERROR("invalid %d foreign size returned\n",
1638 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1640 if (max_stripe_count < stripe_count) {
1641 lum.lum_stripe_count = stripe_count;
1642 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1643 GOTO(finish_req, rc = -EFAULT);
1644 GOTO(finish_req, rc = -E2BIG);
1647 /* enough room on user side and foreign case */
1648 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1649 struct lmv_foreign_md *lfm;
1652 lfm = (struct lmv_foreign_md *)lmm;
1653 size = lfm->lfm_length +
1654 offsetof(struct lmv_foreign_md, lfm_value);
1655 if (copy_to_user(ulmv, lfm, size))
1656 GOTO(finish_req, rc = -EFAULT);
1657 GOTO(finish_req, rc);
1660 lum_size = lmv_user_md_size(stripe_count,
1661 LMV_USER_MAGIC_SPECIFIC);
1662 OBD_ALLOC(tmp, lum_size);
1664 GOTO(finish_req, rc = -ENOMEM);
1666 mdt_index = ll_get_mdt_idx(inode);
1668 GOTO(out_tmp, rc = -ENOMEM);
1670 tmp->lum_magic = LMV_MAGIC_V1;
1671 tmp->lum_stripe_count = 0;
1672 tmp->lum_stripe_offset = mdt_index;
1673 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1674 for (i = 0; i < stripe_count; i++) {
1677 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1678 if (fid_is_sane(&fid)) {
1679 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1681 GOTO(out_tmp, rc = mdt_index);
1683 tmp->lum_objects[i].lum_mds = mdt_index;
1684 tmp->lum_objects[i].lum_fid = fid;
1687 tmp->lum_stripe_count++;
1690 if (copy_to_user(ulmv, tmp, lum_size))
1691 GOTO(out_tmp, rc = -EFAULT);
1693 OBD_FREE(tmp, lum_size);
1695 ptlrpc_req_finished(request);
1696 ptlrpc_req_finished(root_request);
1700 case LL_IOC_REMOVE_ENTRY: {
1701 char *filename = NULL;
1705 /* Here is a little hack to avoid sending REINT_RMENTRY to
1706 * unsupported server, which might crash the server(LU-2730),
1707 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1708 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1709 * server will support REINT_RMENTRY XXX*/
1710 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1713 filename = ll_getname((const char __user *)arg);
1714 if (IS_ERR(filename))
1715 RETURN(PTR_ERR(filename));
1717 namelen = strlen(filename);
1719 GOTO(out_rmdir, rc = -EINVAL);
1721 rc = ll_rmdir_entry(inode, filename, namelen);
1724 ll_putname(filename);
1728 RETURN(ll_rmfid(file, (void __user *)arg));
1729 case LL_IOC_LOV_SWAP_LAYOUTS:
1731 case IOC_OBD_STATFS:
1732 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1733 case LL_IOC_LOV_GETSTRIPE:
1734 case LL_IOC_LOV_GETSTRIPE_NEW:
1735 case LL_IOC_MDC_GETINFO:
1736 case LL_IOC_MDC_GETINFO_OLD:
1737 case IOC_MDC_GETFILEINFO:
1738 case IOC_MDC_GETFILEINFO_OLD:
1739 case IOC_MDC_GETFILESTRIPE: {
1740 struct ptlrpc_request *request = NULL;
1741 struct ptlrpc_request *root_request = NULL;
1742 struct lov_user_md __user *lump;
1743 struct lov_mds_md *lmm = NULL;
1744 struct mdt_body *body;
1745 char *filename = NULL;
1746 lstat_t __user *statp = NULL;
1747 lstatx_t __user *stxp = NULL;
1748 __u64 __user *flagsp = NULL;
1749 __u32 __user *lmmsizep = NULL;
1750 struct lu_fid __user *fidp = NULL;
1753 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1754 cmd == IOC_MDC_GETFILEINFO ||
1755 cmd == IOC_MDC_GETFILESTRIPE) {
1756 filename = ll_getname((const char __user *)arg);
1757 if (IS_ERR(filename))
1758 RETURN(PTR_ERR(filename));
1760 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1761 &lmmsize, &request);
1763 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1769 body = req_capsule_server_get(&request->rq_pill,
1771 LASSERT(body != NULL);
1776 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1777 cmd == LL_IOC_MDC_GETINFO ||
1778 cmd == IOC_MDC_GETFILEINFO_OLD ||
1779 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1787 if (cmd == IOC_MDC_GETFILESTRIPE ||
1788 cmd == LL_IOC_LOV_GETSTRIPE ||
1789 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1790 lump = (struct lov_user_md __user *)arg;
1791 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1792 cmd == LL_IOC_MDC_GETINFO_OLD){
1793 struct lov_user_mds_data_v1 __user *lmdp;
1795 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1796 statp = &lmdp->lmd_st;
1797 lump = &lmdp->lmd_lmm;
1799 struct lov_user_mds_data __user *lmdp;
1801 lmdp = (struct lov_user_mds_data __user *)arg;
1802 fidp = &lmdp->lmd_fid;
1803 stxp = &lmdp->lmd_stx;
1804 flagsp = &lmdp->lmd_flags;
1805 lmmsizep = &lmdp->lmd_lmmsize;
1806 lump = &lmdp->lmd_lmm;
1810 /* If the file has no striping then zero out *lump so
1811 * that the caller isn't confused by garbage. */
1812 if (clear_user(lump, sizeof(*lump)))
1813 GOTO(out_req, rc = -EFAULT);
1814 } else if (copy_to_user(lump, lmm, lmmsize)) {
1815 if (copy_to_user(lump, lmm, sizeof(*lump)))
1816 GOTO(out_req, rc = -EFAULT);
1820 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1821 cmd == LL_IOC_MDC_GETINFO_OLD) {
1824 st.st_dev = inode->i_sb->s_dev;
1825 st.st_mode = body->mbo_mode;
1826 st.st_nlink = body->mbo_nlink;
1827 st.st_uid = body->mbo_uid;
1828 st.st_gid = body->mbo_gid;
1829 st.st_rdev = body->mbo_rdev;
1830 st.st_size = body->mbo_size;
1831 st.st_blksize = PAGE_SIZE;
1832 st.st_blocks = body->mbo_blocks;
1833 st.st_atime = body->mbo_atime;
1834 st.st_mtime = body->mbo_mtime;
1835 st.st_ctime = body->mbo_ctime;
1836 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1840 if (copy_to_user(statp, &st, sizeof(st)))
1841 GOTO(out_req, rc = -EFAULT);
1842 } else if (cmd == IOC_MDC_GETFILEINFO ||
1843 cmd == LL_IOC_MDC_GETINFO) {
1844 lstatx_t stx = { 0 };
1845 __u64 valid = body->mbo_valid;
1847 stx.stx_blksize = PAGE_SIZE;
1848 stx.stx_nlink = body->mbo_nlink;
1849 stx.stx_uid = body->mbo_uid;
1850 stx.stx_gid = body->mbo_gid;
1851 stx.stx_mode = body->mbo_mode;
1852 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1855 stx.stx_size = body->mbo_size;
1856 stx.stx_blocks = body->mbo_blocks;
1857 stx.stx_atime.tv_sec = body->mbo_atime;
1858 stx.stx_ctime.tv_sec = body->mbo_ctime;
1859 stx.stx_mtime.tv_sec = body->mbo_mtime;
1860 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1861 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1862 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1863 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1864 stx.stx_mask |= STATX_BASIC_STATS;
1867 * For a striped directory, the size and blocks returned
1868 * from MDT is not correct.
1869 * The size and blocks are aggregated by client across
1871 * Thus for a striped directory, do not return the valid
1872 * FLSIZE and FLBLOCKS flags to the caller.
1873 * However, this whould be better decided by the MDS
1874 * instead of the client.
1876 if (cmd == LL_IOC_MDC_GETINFO &&
1877 ll_i2info(inode)->lli_lsm_md != NULL)
1878 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1880 if (flagsp && copy_to_user(flagsp, &valid,
1882 GOTO(out_req, rc = -EFAULT);
1884 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1886 GOTO(out_req, rc = -EFAULT);
1888 if (!(valid & OBD_MD_FLSIZE))
1889 stx.stx_mask &= ~STATX_SIZE;
1890 if (!(valid & OBD_MD_FLBLOCKS))
1891 stx.stx_mask &= ~STATX_BLOCKS;
1893 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1894 GOTO(out_req, rc = -EFAULT);
1896 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1898 GOTO(out_req, rc = -EFAULT);
1903 ptlrpc_req_finished(request);
1904 ptlrpc_req_finished(root_request);
1906 ll_putname(filename);
1909 case OBD_IOC_QUOTACTL: {
1910 struct if_quotactl *qctl;
1912 OBD_ALLOC_PTR(qctl);
1916 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1917 GOTO(out_quotactl, rc = -EFAULT);
1919 rc = quotactl_ioctl(sbi, qctl);
1922 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1929 case OBD_IOC_GETDTNAME:
1930 case OBD_IOC_GETMDNAME:
1931 RETURN(ll_get_obd_name(inode, cmd, arg));
1932 case LL_IOC_FLUSHCTX:
1933 RETURN(ll_flush_ctx(inode));
1934 case LL_IOC_GETOBDCOUNT: {
1936 struct obd_export *exp;
1938 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1941 /* get ost count when count is zero, get mdt count otherwise */
1942 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1943 vallen = sizeof(count);
1944 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1945 KEY_TGT_COUNT, &vallen, &count);
1947 CERROR("get target count failed: %d\n", rc);
1951 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1956 case LL_IOC_PATH2FID:
1957 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1958 sizeof(struct lu_fid)))
1961 case LL_IOC_GET_CONNECT_FLAGS: {
1962 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1963 (void __user *)arg));
1965 case OBD_IOC_FID2PATH:
1966 RETURN(ll_fid2path(inode, (void __user *)arg));
1967 case LL_IOC_GETPARENT:
1968 RETURN(ll_getparent(file, (void __user *)arg));
1969 case LL_IOC_FID2MDTIDX: {
1970 struct obd_export *exp = ll_i2mdexp(inode);
1974 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1978 /* Call mdc_iocontrol */
1979 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1980 (__u32 __user *)&index);
1986 case LL_IOC_HSM_REQUEST: {
1987 struct hsm_user_request *hur;
1994 /* We don't know the true size yet; copy the fixed-size part */
1995 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
2000 /* Compute the whole struct size */
2001 totalsize = hur_len(hur);
2006 /* Final size will be more than double totalsize */
2007 if (totalsize >= MDS_MAXREQSIZE / 3)
2010 OBD_ALLOC_LARGE(hur, totalsize);
2014 /* Copy the whole struct */
2015 if (copy_from_user(hur, (void __user *)arg, totalsize))
2016 GOTO(out_hur, rc = -EFAULT);
2018 if (hur->hur_request.hr_action == HUA_RELEASE) {
2019 const struct lu_fid *fid;
2023 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2024 fid = &hur->hur_user_item[i].hui_fid;
2025 f = search_inode_for_lustre(inode->i_sb, fid);
2031 rc = ll_hsm_release(f);
2037 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2042 OBD_FREE_LARGE(hur, totalsize);
2046 case LL_IOC_HSM_PROGRESS: {
2047 struct hsm_progress_kernel hpk;
2048 struct hsm_progress hp;
2050 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2053 hpk.hpk_fid = hp.hp_fid;
2054 hpk.hpk_cookie = hp.hp_cookie;
2055 hpk.hpk_extent = hp.hp_extent;
2056 hpk.hpk_flags = hp.hp_flags;
2057 hpk.hpk_errval = hp.hp_errval;
2058 hpk.hpk_data_version = 0;
2060 /* File may not exist in Lustre; all progress
2061 * reported to Lustre root */
2062 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2066 case LL_IOC_HSM_CT_START:
2067 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2070 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2071 (struct lustre_kernelcomm __user *)arg);
2074 case LL_IOC_HSM_COPY_START: {
2075 struct hsm_copy *copy;
2078 OBD_ALLOC_PTR(copy);
2081 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2086 rc = ll_ioc_copy_start(inode->i_sb, copy);
2087 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2093 case LL_IOC_HSM_COPY_END: {
2094 struct hsm_copy *copy;
2097 OBD_ALLOC_PTR(copy);
2100 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2105 rc = ll_ioc_copy_end(inode->i_sb, copy);
2106 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2112 case LL_IOC_MIGRATE: {
2113 struct lmv_user_md *lum;
2120 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2124 data = (struct obd_ioctl_data *)buf;
2125 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2126 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2127 GOTO(migrate_free, rc = -EINVAL);
2129 filename = data->ioc_inlbuf1;
2130 namelen = data->ioc_inllen1;
2132 if (namelen < 1 || namelen != strlen(filename) + 1) {
2133 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2134 GOTO(migrate_free, rc = -EINVAL);
2137 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2138 if (lum->lum_magic != LMV_USER_MAGIC &&
2139 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2141 CERROR("%s: wrong lum magic %x: rc = %d\n",
2142 filename, lum->lum_magic, rc);
2143 GOTO(migrate_free, rc);
2146 rc = ll_migrate(inode, file, lum, filename);
2148 OBD_FREE_LARGE(buf, len);
2152 case LL_IOC_FSGETXATTR:
2153 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2154 case LL_IOC_FSSETXATTR:
2155 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2156 case LL_IOC_PCC_DETACH_BY_FID: {
2157 struct lu_pcc_detach_fid *detach;
2159 struct inode *inode2;
2162 OBD_ALLOC_PTR(detach);
2166 if (copy_from_user(detach,
2167 (const struct lu_pcc_detach_fid __user *)arg,
2169 GOTO(out_detach, rc = -EFAULT);
2171 fid = &detach->pccd_fid;
2172 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2173 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2175 /* Target inode is not in inode cache, and PCC file
2176 * has aleady released, return immdiately.
2178 GOTO(out_detach, rc = 0);
2180 if (!S_ISREG(inode2->i_mode))
2181 GOTO(out_iput, rc = -EINVAL);
2183 if (!inode_owner_or_capable(inode2))
2184 GOTO(out_iput, rc = -EPERM);
2186 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2190 OBD_FREE_PTR(detach);
2194 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2195 (void __user *)arg));
2199 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2201 struct inode *inode = file->f_mapping->host;
2202 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2203 struct ll_sb_info *sbi = ll_i2sbi(inode);
2204 int api32 = ll_need_32bit_api(sbi);
2205 loff_t ret = -EINVAL;
2213 offset += file->f_pos;
2219 offset += LL_DIR_END_OFF_32BIT;
2221 offset += LL_DIR_END_OFF;
2228 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2229 (!api32 && offset <= LL_DIR_END_OFF))) {
2230 if (offset != file->f_pos) {
2231 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2232 (!api32 && offset == LL_DIR_END_OFF))
2233 fd->lfd_pos = MDS_DIR_END_OFF;
2234 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2235 fd->lfd_pos = offset << 32;
2237 fd->lfd_pos = offset;
2238 file->f_pos = offset;
2239 file->f_version = 0;
2246 inode_unlock(inode);
2250 static int ll_dir_open(struct inode *inode, struct file *file)
2253 RETURN(ll_file_open(inode, file));
2256 static int ll_dir_release(struct inode *inode, struct file *file)
2259 RETURN(ll_file_release(inode, file));
2262 const struct file_operations ll_dir_operations = {
2263 .llseek = ll_dir_seek,
2264 .open = ll_dir_open,
2265 .release = ll_dir_release,
2266 .read = generic_read_dir,
2267 #ifdef HAVE_DIR_CONTEXT
2268 .iterate_shared = ll_iterate,
2270 .readdir = ll_readdir,
2272 .unlocked_ioctl = ll_dir_ioctl,