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);
683 * This function will be used to get default LOV/LMV/Default LMV
684 * @valid will be used to indicate which stripe it will retrieve
685 * OBD_MD_MEA LMV stripe EA
686 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
687 * otherwise Default LOV EA.
688 * Each time, it can only retrieve 1 stripe EA
690 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
691 struct ptlrpc_request **request, u64 valid)
693 struct ll_sb_info *sbi = ll_i2sbi(inode);
694 struct mdt_body *body;
695 struct lov_mds_md *lmm = NULL;
696 struct ptlrpc_request *req = NULL;
698 struct md_op_data *op_data;
701 rc = ll_get_default_mdsize(sbi, &lmm_size);
705 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
706 0, lmm_size, LUSTRE_OPC_ANY,
709 RETURN(PTR_ERR(op_data));
711 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
712 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
713 ll_finish_md_op_data(op_data);
715 CDEBUG(D_INFO, "md_getattr failed on inode "
716 DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
720 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
721 LASSERT(body != NULL);
723 lmm_size = body->mbo_eadatasize;
725 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
727 GOTO(out, rc = -ENODATA);
730 lmm = req_capsule_server_sized_get(&req->rq_pill,
731 &RMF_MDT_MD, lmm_size);
732 LASSERT(lmm != NULL);
735 * This is coming from the MDS, so is probably in
736 * little endian. We convert it to host endian before
737 * passing it to userspace.
739 /* We don't swab objects for directories */
740 switch (le32_to_cpu(lmm->lmm_magic)) {
743 case LOV_MAGIC_COMP_V1:
744 case LOV_USER_MAGIC_SPECIFIC:
745 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
746 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
749 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
750 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
753 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
754 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
756 case LMV_MAGIC_FOREIGN: {
757 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
759 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
760 __swab32s(&lfm->lfm_magic);
761 __swab32s(&lfm->lfm_length);
762 __swab32s(&lfm->lfm_type);
763 __swab32s(&lfm->lfm_flags);
768 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
773 *plmm_size = lmm_size;
778 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
780 struct md_op_data *op_data;
785 OBD_ALLOC_PTR(op_data);
789 op_data->op_flags |= MF_GET_MDT_IDX;
790 op_data->op_fid1 = *fid;
791 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
792 mdt_index = op_data->op_mds;
793 OBD_FREE_PTR(op_data);
801 * Get MDT index for the inode.
803 int ll_get_mdt_idx(struct inode *inode)
805 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
809 * Generic handler to do any pre-copy work.
811 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
812 * first information for it that real work has started.
814 * Moreover, for a ARCHIVE request, it will sample the file data version and
815 * store it in \a copy.
817 * \return 0 on success.
819 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
821 struct ll_sb_info *sbi = ll_s2sbi(sb);
822 struct hsm_progress_kernel hpk;
827 /* Forge a hsm_progress based on data from copy. */
828 hpk.hpk_fid = copy->hc_hai.hai_fid;
829 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
830 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
831 hpk.hpk_extent.length = 0;
834 hpk.hpk_data_version = 0;
837 /* For archive request, we need to read the current file version. */
838 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
840 __u64 data_version = 0;
842 /* Get inode for this fid */
843 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
845 hpk.hpk_flags |= HP_FLAG_RETRY;
846 /* hpk_errval is >= 0 */
847 hpk.hpk_errval = -PTR_ERR(inode);
848 GOTO(progress, rc = PTR_ERR(inode));
851 /* Read current file data version */
852 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
855 CDEBUG(D_HSM, "Could not read file data version of "
856 DFID" (rc = %d). Archive request ("
857 "%#llx) could not be done.\n",
858 PFID(©->hc_hai.hai_fid), rc,
859 copy->hc_hai.hai_cookie);
860 hpk.hpk_flags |= HP_FLAG_RETRY;
861 /* hpk_errval must be >= 0 */
862 hpk.hpk_errval = -rc;
866 /* Store in the hsm_copy for later copytool use.
867 * Always modified even if no lsm. */
868 copy->hc_data_version = data_version;
872 /* On error, the request should be considered as completed */
873 if (hpk.hpk_errval > 0)
874 hpk.hpk_flags |= HP_FLAG_COMPLETED;
876 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
879 /* Return first error */
880 RETURN(rc != 0 ? rc : rc2);
884 * Generic handler to do any post-copy work.
886 * It will send the last hsm_progress update to coordinator to inform it
887 * that copy is finished and whether it was successful or not.
890 * - for ARCHIVE request, it will sample the file data version and compare it
891 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
892 * will be considered as failed.
893 * - for RESTORE request, it will sample the file data version and send it to
894 * coordinator which is useful if the file was imported as 'released'.
896 * \return 0 on success.
898 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
900 struct ll_sb_info *sbi = ll_s2sbi(sb);
901 struct hsm_progress_kernel hpk;
906 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
907 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
908 * initialized if copy_end was called with copy == NULL.
911 /* Forge a hsm_progress based on data from copy. */
912 hpk.hpk_fid = copy->hc_hai.hai_fid;
913 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
914 hpk.hpk_extent = copy->hc_hai.hai_extent;
915 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
916 hpk.hpk_errval = copy->hc_errval;
917 hpk.hpk_data_version = 0;
919 /* For archive request, we need to check the file data was not changed.
921 * For restore request, we need to send the file data version, this is
922 * useful when the file was created using hsm_import.
924 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
925 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
926 (copy->hc_errval == 0)) {
928 __u64 data_version = 0;
930 /* Get lsm for this fid */
931 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
933 hpk.hpk_flags |= HP_FLAG_RETRY;
934 /* hpk_errval must be >= 0 */
935 hpk.hpk_errval = -PTR_ERR(inode);
936 GOTO(progress, rc = PTR_ERR(inode));
939 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
942 CDEBUG(D_HSM, "Could not read file data version. "
943 "Request could not be confirmed.\n");
944 if (hpk.hpk_errval == 0)
945 hpk.hpk_errval = -rc;
949 /* Store in the hsm_copy for later copytool use.
950 * Always modified even if no lsm. */
951 hpk.hpk_data_version = data_version;
953 /* File could have been stripped during archiving, so we need
954 * to check anyway. */
955 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
956 (copy->hc_data_version != data_version)) {
957 CDEBUG(D_HSM, "File data version mismatched. "
958 "File content was changed during archiving. "
959 DFID", start:%#llx current:%#llx\n",
960 PFID(©->hc_hai.hai_fid),
961 copy->hc_data_version, data_version);
962 /* File was changed, send error to cdt. Do not ask for
963 * retry because if a file is modified frequently,
964 * the cdt will loop on retried archive requests.
965 * The policy engine will ask for a new archive later
966 * when the file will not be modified for some tunable
968 hpk.hpk_flags &= ~HP_FLAG_RETRY;
970 /* hpk_errval must be >= 0 */
971 hpk.hpk_errval = -rc;
978 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
981 /* Return first error */
982 RETURN(rc != 0 ? rc : rc2);
986 static int copy_and_ct_start(int cmd, struct obd_export *exp,
987 const struct lustre_kernelcomm __user *data)
989 struct lustre_kernelcomm *lk;
990 struct lustre_kernelcomm *tmp;
991 size_t size = sizeof(*lk);
996 /* copy data from userspace to get numbers of archive_id */
1001 if (copy_from_user(lk, data, size))
1002 GOTO(out_lk, rc = -EFAULT);
1004 if (lk->lk_flags & LK_FLG_STOP)
1007 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1008 __u32 archive_mask = lk->lk_data_count;
1011 /* old hsm agent to old MDS */
1012 if (!exp_connect_archive_id_array(exp))
1015 /* old hsm agent to new MDS */
1016 lk->lk_flags |= LK_FLG_DATANR;
1018 if (archive_mask == 0)
1021 count = hweight32(archive_mask);
1022 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1023 OBD_ALLOC(tmp, new_size);
1025 GOTO(out_lk, rc = -ENOMEM);
1027 memcpy(tmp, lk, size);
1028 tmp->lk_data_count = count;
1034 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1035 if ((1 << i) & archive_mask) {
1036 lk->lk_data[count] = i + 1;
1043 /* new hsm agent to new mds */
1044 if (lk->lk_data_count > 0) {
1045 new_size = offsetof(struct lustre_kernelcomm,
1046 lk_data[lk->lk_data_count]);
1047 OBD_ALLOC(tmp, new_size);
1049 GOTO(out_lk, rc = -ENOMEM);
1055 if (copy_from_user(lk, data, size))
1056 GOTO(out_lk, rc = -EFAULT);
1059 /* new hsm agent to old MDS */
1060 if (!exp_connect_archive_id_array(exp)) {
1063 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1064 GOTO(out_lk, rc = -EINVAL);
1066 for (i = 0; i < lk->lk_data_count; i++) {
1067 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1069 CERROR("%s: archive id %d requested but only "
1070 "[0 - %zu] supported: rc = %d\n",
1071 exp->exp_obd->obd_name, lk->lk_data[i],
1072 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1076 if (lk->lk_data[i] == 0) {
1081 archives |= (1 << (lk->lk_data[i] - 1));
1083 lk->lk_flags &= ~LK_FLG_DATANR;
1084 lk->lk_data_count = archives;
1087 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1093 static int check_owner(int type, int id)
1097 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1101 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1110 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1112 int cmd = qctl->qc_cmd;
1113 int type = qctl->qc_type;
1114 int id = qctl->qc_id;
1115 int valid = qctl->qc_valid;
1122 case LUSTRE_Q_SETDEFAULT:
1123 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1127 case LUSTRE_Q_GETDEFAULT:
1128 if (check_owner(type, id) &&
1129 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1135 CERROR("unsupported quotactl op: %#x\n", cmd);
1139 if (valid != QC_GENERAL) {
1140 if (cmd == Q_GETINFO)
1141 qctl->qc_cmd = Q_GETOINFO;
1142 else if (cmd == Q_GETQUOTA)
1143 qctl->qc_cmd = Q_GETOQUOTA;
1149 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1150 sizeof(*qctl), qctl, NULL);
1153 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1154 sizeof(*qctl), qctl, NULL);
1157 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1158 sizeof(*qctl), qctl, NULL);
1160 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1162 sizeof(*qctl), qctl, NULL);
1174 struct obd_quotactl *oqctl;
1176 OBD_ALLOC_PTR(oqctl);
1180 QCTL_COPY(oqctl, qctl);
1181 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1183 OBD_FREE_PTR(oqctl);
1186 /* If QIF_SPACE is not set, client should collect the
1187 * space usage from OSSs by itself */
1188 if (cmd == Q_GETQUOTA &&
1189 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1190 !oqctl->qc_dqblk.dqb_curspace) {
1191 struct obd_quotactl *oqctl_tmp;
1193 OBD_ALLOC_PTR(oqctl_tmp);
1194 if (oqctl_tmp == NULL)
1195 GOTO(out, rc = -ENOMEM);
1197 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1198 oqctl_tmp->qc_id = oqctl->qc_id;
1199 oqctl_tmp->qc_type = oqctl->qc_type;
1201 /* collect space usage from OSTs */
1202 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1203 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1204 if (!rc || rc == -EREMOTEIO) {
1205 oqctl->qc_dqblk.dqb_curspace =
1206 oqctl_tmp->qc_dqblk.dqb_curspace;
1207 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1210 /* collect space & inode usage from MDTs */
1211 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1212 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1213 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1214 if (!rc || rc == -EREMOTEIO) {
1215 oqctl->qc_dqblk.dqb_curspace +=
1216 oqctl_tmp->qc_dqblk.dqb_curspace;
1217 oqctl->qc_dqblk.dqb_curinodes =
1218 oqctl_tmp->qc_dqblk.dqb_curinodes;
1219 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1221 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1224 OBD_FREE_PTR(oqctl_tmp);
1227 QCTL_COPY(qctl, oqctl);
1228 OBD_FREE_PTR(oqctl);
1234 int ll_rmfid(struct file *file, void __user *arg)
1236 const struct fid_array __user *ufa = arg;
1237 struct fid_array *lfa = NULL;
1240 int i, rc, *rcs = NULL;
1243 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1244 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1246 /* Only need to get the buflen */
1247 if (get_user(nr, &ufa->fa_nr))
1249 /* DoS protection */
1250 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1253 size = offsetof(struct fid_array, fa_fids[nr]);
1254 OBD_ALLOC(lfa, size);
1257 OBD_ALLOC(rcs, sizeof(int) * nr);
1259 GOTO(free_lfa, rc = -ENOMEM);
1261 if (copy_from_user(lfa, arg, size))
1262 GOTO(free_rcs, rc = -EFAULT);
1264 /* Call mdc_iocontrol */
1265 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1267 for (i = 0; i < nr; i++)
1269 lfa->fa_fids[i].f_ver = rcs[i];
1270 if (copy_to_user(arg, lfa, size))
1275 OBD_FREE(rcs, sizeof(int) * nr);
1277 OBD_FREE(lfa, size);
1282 /* This function tries to get a single name component,
1283 * to send to the server. No actual path traversal involved,
1284 * so we limit to NAME_MAX */
1285 static char *ll_getname(const char __user *filename)
1290 OBD_ALLOC(tmp, NAME_MAX + 1);
1293 return ERR_PTR(-ENOMEM);
1295 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1298 else if (len > NAME_MAX)
1299 ret = -ENAMETOOLONG;
1302 OBD_FREE(tmp, NAME_MAX + 1);
1308 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1310 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1312 struct dentry *dentry = file_dentry(file);
1313 struct inode *inode = file_inode(file);
1314 struct ll_sb_info *sbi = ll_i2sbi(inode);
1315 struct obd_ioctl_data *data;
1319 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1320 PFID(ll_inode2fid(inode)), inode, cmd);
1322 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1323 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1326 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1328 case FS_IOC_GETFLAGS:
1329 case FS_IOC_SETFLAGS:
1330 RETURN(ll_iocontrol(inode, file, cmd, arg));
1331 case FSFILT_IOC_GETVERSION:
1332 case FS_IOC_GETVERSION:
1333 RETURN(put_user(inode->i_generation, (int __user *)arg));
1334 /* We need to special case any other ioctls we want to handle,
1335 * to send them to the MDS/OST as appropriate and to properly
1336 * network encode the arg field. */
1337 case FS_IOC_SETVERSION:
1340 case LL_IOC_GET_MDTIDX: {
1343 mdtidx = ll_get_mdt_idx(inode);
1347 if (put_user((int)mdtidx, (int __user *)arg))
1352 case IOC_MDC_LOOKUP: {
1353 int namelen, len = 0;
1357 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1362 filename = data->ioc_inlbuf1;
1363 namelen = strlen(filename);
1365 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1366 GOTO(out_free, rc = -EINVAL);
1369 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1371 CERROR("%s: lookup %.*s failed: rc = %d\n",
1372 sbi->ll_fsname, namelen, filename, rc);
1376 OBD_FREE_LARGE(buf, len);
1379 case LL_IOC_LMV_SETSTRIPE: {
1380 struct lmv_user_md *lum;
1389 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1394 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1395 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1396 GOTO(lmv_out_free, rc = -EINVAL);
1398 filename = data->ioc_inlbuf1;
1399 namelen = data->ioc_inllen1;
1402 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1403 GOTO(lmv_out_free, rc = -EINVAL);
1405 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1406 lumlen = data->ioc_inllen2;
1408 if (!lmv_magic_supported(lum->lum_magic)) {
1409 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1410 lum->lum_magic, -EINVAL);
1411 GOTO(lmv_out_free, rc = -EINVAL);
1414 if ((lum->lum_magic == LMV_USER_MAGIC ||
1415 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1416 lumlen < sizeof(*lum)) {
1417 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1418 filename, lumlen, lum->lum_magic, -EINVAL);
1419 GOTO(lmv_out_free, rc = -EINVAL);
1422 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1423 lumlen < sizeof(struct lmv_foreign_md)) {
1424 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1425 filename, lum->lum_magic, lumlen, -EFAULT);
1426 GOTO(lmv_out_free, rc = -EINVAL);
1429 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1430 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1432 mode = data->ioc_type;
1434 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1436 OBD_FREE_LARGE(buf, len);
1440 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1441 struct lmv_user_md lum;
1442 struct lmv_user_md __user *ulump =
1443 (struct lmv_user_md __user *)arg;
1446 if (copy_from_user(&lum, ulump, sizeof(lum)))
1449 if (lum.lum_magic != LMV_USER_MAGIC)
1452 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1456 case LL_IOC_LOV_SETSTRIPE_NEW:
1457 case LL_IOC_LOV_SETSTRIPE: {
1458 struct lov_user_md_v3 *lumv3 = NULL;
1459 struct lov_user_md_v1 lumv1;
1460 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1461 struct lov_user_md_v1 __user *lumv1p =
1462 (struct lov_user_md_v1 __user *)arg;
1463 struct lov_user_md_v3 __user *lumv3p =
1464 (struct lov_user_md_v3 __user *)arg;
1467 int set_default = 0;
1469 CLASSERT(sizeof(struct lov_user_md_v3) >
1470 sizeof(struct lov_comp_md_v1));
1471 CLASSERT(sizeof(*lumv3) == sizeof(*lumv3p));
1472 /* first try with v1 which is smaller than v3 */
1473 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1476 if (inode->i_sb->s_root == file_dentry(file))
1479 switch (lumv1.lmm_magic) {
1480 case LOV_USER_MAGIC_V3:
1481 case LOV_USER_MAGIC_SPECIFIC:
1482 lum_size = ll_lov_user_md_size(&lumv1);
1485 OBD_ALLOC(lumv3, lum_size);
1488 if (copy_from_user(lumv3, lumv3p, lum_size))
1489 GOTO(out, rc = -EFAULT);
1490 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1492 case LOV_USER_MAGIC_V1:
1495 GOTO(out, rc = -ENOTSUPP);
1498 /* in v1 and v3 cases lumv1 points to data */
1499 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1502 OBD_FREE(lumv3, lum_size);
1505 case LL_IOC_LMV_GETSTRIPE: {
1506 struct lmv_user_md __user *ulmv =
1507 (struct lmv_user_md __user *)arg;
1508 struct lmv_user_md lum;
1509 struct ptlrpc_request *request = NULL;
1510 union lmv_mds_md *lmm = NULL;
1513 struct lmv_user_md *tmp = NULL;
1517 int max_stripe_count;
1521 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1524 max_stripe_count = lum.lum_stripe_count;
1525 /* lum_magic will indicate which stripe the ioctl will like
1526 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1527 * is for default LMV stripe */
1528 if (lum.lum_magic == LMV_MAGIC_V1)
1529 valid |= OBD_MD_MEA;
1530 else if (lum.lum_magic == LMV_USER_MAGIC)
1531 valid |= OBD_MD_DEFAULT_MEA;
1535 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1538 GOTO(finish_req, rc);
1540 /* Get default LMV EA */
1541 if (lum.lum_magic == LMV_USER_MAGIC) {
1542 if (lmmsize > sizeof(*ulmv))
1543 GOTO(finish_req, rc = -EINVAL);
1545 if (copy_to_user(ulmv, lmm, lmmsize))
1546 GOTO(finish_req, rc = -EFAULT);
1548 GOTO(finish_req, rc);
1551 /* if foreign LMV case, fake stripes number */
1552 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1553 struct lmv_foreign_md *lfm;
1555 lfm = (struct lmv_foreign_md *)lmm;
1556 if (lfm->lfm_length < XATTR_SIZE_MAX -
1557 offsetof(typeof(*lfm), lfm_value)) {
1558 __u32 size = lfm->lfm_length +
1559 offsetof(typeof(*lfm), lfm_value);
1561 stripe_count = lmv_foreign_to_md_stripes(size);
1563 CERROR("invalid %d foreign size returned\n",
1568 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1570 if (max_stripe_count < stripe_count) {
1571 lum.lum_stripe_count = stripe_count;
1572 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1573 GOTO(finish_req, rc = -EFAULT);
1574 GOTO(finish_req, rc = -E2BIG);
1577 /* enough room on user side and foreign case */
1578 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1579 struct lmv_foreign_md *lfm;
1582 lfm = (struct lmv_foreign_md *)lmm;
1583 size = lfm->lfm_length +
1584 offsetof(struct lmv_foreign_md, lfm_value);
1585 if (copy_to_user(ulmv, lfm, size))
1586 GOTO(finish_req, rc = -EFAULT);
1587 GOTO(finish_req, rc);
1590 lum_size = lmv_user_md_size(stripe_count,
1591 LMV_USER_MAGIC_SPECIFIC);
1592 OBD_ALLOC(tmp, lum_size);
1594 GOTO(finish_req, rc = -ENOMEM);
1596 mdt_index = ll_get_mdt_idx(inode);
1598 GOTO(out_tmp, rc = -ENOMEM);
1600 tmp->lum_magic = LMV_MAGIC_V1;
1601 tmp->lum_stripe_count = 0;
1602 tmp->lum_stripe_offset = mdt_index;
1603 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1604 for (i = 0; i < stripe_count; i++) {
1607 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1608 if (fid_is_sane(&fid)) {
1609 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1611 GOTO(out_tmp, rc = mdt_index);
1613 tmp->lum_objects[i].lum_mds = mdt_index;
1614 tmp->lum_objects[i].lum_fid = fid;
1617 tmp->lum_stripe_count++;
1620 if (copy_to_user(ulmv, tmp, lum_size))
1621 GOTO(out_tmp, rc = -EFAULT);
1623 OBD_FREE(tmp, lum_size);
1625 ptlrpc_req_finished(request);
1629 case LL_IOC_REMOVE_ENTRY: {
1630 char *filename = NULL;
1634 /* Here is a little hack to avoid sending REINT_RMENTRY to
1635 * unsupported server, which might crash the server(LU-2730),
1636 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1637 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1638 * server will support REINT_RMENTRY XXX*/
1639 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1642 filename = ll_getname((const char __user *)arg);
1643 if (IS_ERR(filename))
1644 RETURN(PTR_ERR(filename));
1646 namelen = strlen(filename);
1648 GOTO(out_rmdir, rc = -EINVAL);
1650 rc = ll_rmdir_entry(inode, filename, namelen);
1653 ll_putname(filename);
1657 RETURN(ll_rmfid(file, (void __user *)arg));
1658 case LL_IOC_LOV_SWAP_LAYOUTS:
1660 case IOC_OBD_STATFS:
1661 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1662 case LL_IOC_LOV_GETSTRIPE:
1663 case LL_IOC_LOV_GETSTRIPE_NEW:
1664 case LL_IOC_MDC_GETINFO:
1665 case LL_IOC_MDC_GETINFO_OLD:
1666 case IOC_MDC_GETFILEINFO:
1667 case IOC_MDC_GETFILEINFO_OLD:
1668 case IOC_MDC_GETFILESTRIPE: {
1669 struct ptlrpc_request *request = NULL;
1670 struct lov_user_md __user *lump;
1671 struct lov_mds_md *lmm = NULL;
1672 struct mdt_body *body;
1673 char *filename = NULL;
1674 lstat_t __user *statp = NULL;
1675 lstatx_t __user *stxp = NULL;
1676 __u64 __user *flagsp = NULL;
1677 __u32 __user *lmmsizep = NULL;
1678 struct lu_fid __user *fidp = NULL;
1681 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1682 cmd == IOC_MDC_GETFILEINFO ||
1683 cmd == IOC_MDC_GETFILESTRIPE) {
1684 filename = ll_getname((const char __user *)arg);
1685 if (IS_ERR(filename))
1686 RETURN(PTR_ERR(filename));
1688 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1689 &lmmsize, &request);
1691 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1696 body = req_capsule_server_get(&request->rq_pill,
1698 LASSERT(body != NULL);
1703 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1704 cmd == LL_IOC_MDC_GETINFO ||
1705 cmd == IOC_MDC_GETFILEINFO_OLD ||
1706 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1714 if (cmd == IOC_MDC_GETFILESTRIPE ||
1715 cmd == LL_IOC_LOV_GETSTRIPE ||
1716 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1717 lump = (struct lov_user_md __user *)arg;
1718 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1719 cmd == LL_IOC_MDC_GETINFO_OLD){
1720 struct lov_user_mds_data_v1 __user *lmdp;
1722 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1723 statp = &lmdp->lmd_st;
1724 lump = &lmdp->lmd_lmm;
1726 struct lov_user_mds_data __user *lmdp;
1728 lmdp = (struct lov_user_mds_data __user *)arg;
1729 fidp = &lmdp->lmd_fid;
1730 stxp = &lmdp->lmd_stx;
1731 flagsp = &lmdp->lmd_flags;
1732 lmmsizep = &lmdp->lmd_lmmsize;
1733 lump = &lmdp->lmd_lmm;
1737 /* If the file has no striping then zero out *lump so
1738 * that the caller isn't confused by garbage. */
1739 if (clear_user(lump, sizeof(*lump)))
1740 GOTO(out_req, rc = -EFAULT);
1741 } else if (copy_to_user(lump, lmm, lmmsize)) {
1742 if (copy_to_user(lump, lmm, sizeof(*lump)))
1743 GOTO(out_req, rc = -EFAULT);
1747 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1748 cmd == LL_IOC_MDC_GETINFO_OLD) {
1751 st.st_dev = inode->i_sb->s_dev;
1752 st.st_mode = body->mbo_mode;
1753 st.st_nlink = body->mbo_nlink;
1754 st.st_uid = body->mbo_uid;
1755 st.st_gid = body->mbo_gid;
1756 st.st_rdev = body->mbo_rdev;
1757 st.st_size = body->mbo_size;
1758 st.st_blksize = PAGE_SIZE;
1759 st.st_blocks = body->mbo_blocks;
1760 st.st_atime = body->mbo_atime;
1761 st.st_mtime = body->mbo_mtime;
1762 st.st_ctime = body->mbo_ctime;
1763 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1767 if (copy_to_user(statp, &st, sizeof(st)))
1768 GOTO(out_req, rc = -EFAULT);
1769 } else if (cmd == IOC_MDC_GETFILEINFO ||
1770 cmd == LL_IOC_MDC_GETINFO) {
1771 lstatx_t stx = { 0 };
1772 __u64 valid = body->mbo_valid;
1774 stx.stx_blksize = PAGE_SIZE;
1775 stx.stx_nlink = body->mbo_nlink;
1776 stx.stx_uid = body->mbo_uid;
1777 stx.stx_gid = body->mbo_gid;
1778 stx.stx_mode = body->mbo_mode;
1779 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1782 stx.stx_size = body->mbo_size;
1783 stx.stx_blocks = body->mbo_blocks;
1784 stx.stx_atime.tv_sec = body->mbo_atime;
1785 stx.stx_ctime.tv_sec = body->mbo_ctime;
1786 stx.stx_mtime.tv_sec = body->mbo_mtime;
1787 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1788 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1789 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1790 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1791 stx.stx_mask |= STATX_BASIC_STATS;
1794 * For a striped directory, the size and blocks returned
1795 * from MDT is not correct.
1796 * The size and blocks are aggregated by client across
1798 * Thus for a striped directory, do not return the valid
1799 * FLSIZE and FLBLOCKS flags to the caller.
1800 * However, this whould be better decided by the MDS
1801 * instead of the client.
1803 if (cmd == LL_IOC_MDC_GETINFO &&
1804 ll_i2info(inode)->lli_lsm_md != NULL)
1805 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1807 if (flagsp && copy_to_user(flagsp, &valid,
1809 GOTO(out_req, rc = -EFAULT);
1811 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1813 GOTO(out_req, rc = -EFAULT);
1815 if (!(valid & OBD_MD_FLSIZE))
1816 stx.stx_mask &= ~STATX_SIZE;
1817 if (!(valid & OBD_MD_FLBLOCKS))
1818 stx.stx_mask &= ~STATX_BLOCKS;
1820 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1821 GOTO(out_req, rc = -EFAULT);
1823 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1825 GOTO(out_req, rc = -EFAULT);
1830 ptlrpc_req_finished(request);
1832 ll_putname(filename);
1835 case OBD_IOC_QUOTACTL: {
1836 struct if_quotactl *qctl;
1838 OBD_ALLOC_PTR(qctl);
1842 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1843 GOTO(out_quotactl, rc = -EFAULT);
1845 rc = quotactl_ioctl(sbi, qctl);
1848 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1855 case OBD_IOC_GETDTNAME:
1856 case OBD_IOC_GETMDNAME:
1857 RETURN(ll_get_obd_name(inode, cmd, arg));
1858 case LL_IOC_FLUSHCTX:
1859 RETURN(ll_flush_ctx(inode));
1860 case LL_IOC_GETOBDCOUNT: {
1862 struct obd_export *exp;
1864 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1867 /* get ost count when count is zero, get mdt count otherwise */
1868 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1869 vallen = sizeof(count);
1870 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1871 KEY_TGT_COUNT, &vallen, &count);
1873 CERROR("get target count failed: %d\n", rc);
1877 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1882 case LL_IOC_PATH2FID:
1883 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1884 sizeof(struct lu_fid)))
1887 case LL_IOC_GET_CONNECT_FLAGS: {
1888 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1889 (void __user *)arg));
1891 case OBD_IOC_FID2PATH:
1892 RETURN(ll_fid2path(inode, (void __user *)arg));
1893 case LL_IOC_GETPARENT:
1894 RETURN(ll_getparent(file, (void __user *)arg));
1895 case LL_IOC_FID2MDTIDX: {
1896 struct obd_export *exp = ll_i2mdexp(inode);
1900 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1904 /* Call mdc_iocontrol */
1905 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1906 (__u32 __user *)&index);
1912 case LL_IOC_HSM_REQUEST: {
1913 struct hsm_user_request *hur;
1920 /* We don't know the true size yet; copy the fixed-size part */
1921 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1926 /* Compute the whole struct size */
1927 totalsize = hur_len(hur);
1932 /* Final size will be more than double totalsize */
1933 if (totalsize >= MDS_MAXREQSIZE / 3)
1936 OBD_ALLOC_LARGE(hur, totalsize);
1940 /* Copy the whole struct */
1941 if (copy_from_user(hur, (void __user *)arg, totalsize))
1942 GOTO(out_hur, rc = -EFAULT);
1944 if (hur->hur_request.hr_action == HUA_RELEASE) {
1945 const struct lu_fid *fid;
1949 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1950 fid = &hur->hur_user_item[i].hui_fid;
1951 f = search_inode_for_lustre(inode->i_sb, fid);
1957 rc = ll_hsm_release(f);
1963 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1968 OBD_FREE_LARGE(hur, totalsize);
1972 case LL_IOC_HSM_PROGRESS: {
1973 struct hsm_progress_kernel hpk;
1974 struct hsm_progress hp;
1976 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1979 hpk.hpk_fid = hp.hp_fid;
1980 hpk.hpk_cookie = hp.hp_cookie;
1981 hpk.hpk_extent = hp.hp_extent;
1982 hpk.hpk_flags = hp.hp_flags;
1983 hpk.hpk_errval = hp.hp_errval;
1984 hpk.hpk_data_version = 0;
1986 /* File may not exist in Lustre; all progress
1987 * reported to Lustre root */
1988 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1992 case LL_IOC_HSM_CT_START:
1993 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1996 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
1997 (struct lustre_kernelcomm __user *)arg);
2000 case LL_IOC_HSM_COPY_START: {
2001 struct hsm_copy *copy;
2004 OBD_ALLOC_PTR(copy);
2007 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2012 rc = ll_ioc_copy_start(inode->i_sb, copy);
2013 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2019 case LL_IOC_HSM_COPY_END: {
2020 struct hsm_copy *copy;
2023 OBD_ALLOC_PTR(copy);
2026 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2031 rc = ll_ioc_copy_end(inode->i_sb, copy);
2032 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2038 case LL_IOC_MIGRATE: {
2039 struct lmv_user_md *lum;
2046 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2050 data = (struct obd_ioctl_data *)buf;
2051 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2052 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2053 GOTO(migrate_free, rc = -EINVAL);
2055 filename = data->ioc_inlbuf1;
2056 namelen = data->ioc_inllen1;
2058 if (namelen < 1 || namelen != strlen(filename) + 1) {
2059 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2060 GOTO(migrate_free, rc = -EINVAL);
2063 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2064 if (lum->lum_magic != LMV_USER_MAGIC &&
2065 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2067 CERROR("%s: wrong lum magic %x: rc = %d\n",
2068 filename, lum->lum_magic, rc);
2069 GOTO(migrate_free, rc);
2072 rc = ll_migrate(inode, file, lum, filename);
2074 OBD_FREE_LARGE(buf, len);
2078 case LL_IOC_FSGETXATTR:
2079 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2080 case LL_IOC_FSSETXATTR:
2081 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2082 case LL_IOC_PCC_DETACH_BY_FID: {
2083 struct lu_pcc_detach_fid *detach;
2085 struct inode *inode2;
2088 OBD_ALLOC_PTR(detach);
2092 if (copy_from_user(detach,
2093 (const struct lu_pcc_detach_fid __user *)arg,
2095 GOTO(out_detach, rc = -EFAULT);
2097 fid = &detach->pccd_fid;
2098 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2099 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2101 /* Target inode is not in inode cache, and PCC file
2102 * has aleady released, return immdiately.
2104 GOTO(out_detach, rc = 0);
2106 if (!S_ISREG(inode2->i_mode))
2107 GOTO(out_iput, rc = -EINVAL);
2109 if (!inode_owner_or_capable(inode2))
2110 GOTO(out_iput, rc = -EPERM);
2112 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2116 OBD_FREE_PTR(detach);
2120 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2121 (void __user *)arg));
2125 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2127 struct inode *inode = file->f_mapping->host;
2128 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2129 struct ll_sb_info *sbi = ll_i2sbi(inode);
2130 int api32 = ll_need_32bit_api(sbi);
2131 loff_t ret = -EINVAL;
2139 offset += file->f_pos;
2145 offset += LL_DIR_END_OFF_32BIT;
2147 offset += LL_DIR_END_OFF;
2154 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2155 (!api32 && offset <= LL_DIR_END_OFF))) {
2156 if (offset != file->f_pos) {
2157 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2158 (!api32 && offset == LL_DIR_END_OFF))
2159 fd->lfd_pos = MDS_DIR_END_OFF;
2160 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2161 fd->lfd_pos = offset << 32;
2163 fd->lfd_pos = offset;
2164 file->f_pos = offset;
2165 file->f_version = 0;
2172 inode_unlock(inode);
2176 static int ll_dir_open(struct inode *inode, struct file *file)
2179 RETURN(ll_file_open(inode, file));
2182 static int ll_dir_release(struct inode *inode, struct file *file)
2185 RETURN(ll_file_release(inode, file));
2188 const struct file_operations ll_dir_operations = {
2189 .llseek = ll_dir_seek,
2190 .open = ll_dir_open,
2191 .release = ll_dir_release,
2192 .read = generic_read_dir,
2193 #ifdef HAVE_DIR_CONTEXT
2194 .iterate_shared = ll_iterate,
2196 .readdir = ll_readdir,
2198 .unlocked_ioctl = ll_dir_ioctl,