4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Directory code for lustre client.
37 #include <linux/pagemap.h>
39 #include <linux/version.h>
40 #include <linux/security.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43 #include <linux/uaccess.h>
44 #include <linux/buffer_head.h> // for wait_on_buffer
45 #include <linux/pagevec.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <uapi/linux/llcrypt.h>
53 #include <lustre_lib.h>
54 #include <lustre_dlm.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
59 #include "llite_internal.h"
62 * (new) readdir implementation overview.
64 * Original lustre readdir implementation cached exact copy of raw directory
65 * pages on the client. These pages were indexed in client page cache by
66 * logical offset in the directory file. This design, while very simple and
67 * intuitive had some inherent problems:
69 * . it implies that byte offset to the directory entry serves as a
70 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71 * ext3/htree directory entries may move due to splits, and more
74 * . it is incompatible with the design of split directories for cmd3,
75 * that assumes that names are distributed across nodes based on their
76 * hash, and so readdir should be done in hash order.
78 * New readdir implementation does readdir in hash order, and uses hash of a
79 * file name as a telldir/seekdir cookie. This led to number of complications:
81 * . hash is not unique, so it cannot be used to index cached directory
82 * pages on the client (note, that it requires a whole pageful of hash
83 * collided entries to cause two pages to have identical hashes);
85 * . hash is not unique, so it cannot, strictly speaking, be used as an
86 * entry cookie. ext3/htree has the same problem and lustre implementation
87 * mimics their solution: seekdir(hash) positions directory at the first
88 * entry with the given hash.
94 * Client caches directory pages using hash of the first entry as an index. As
95 * noted above hash is not unique, so this solution doesn't work as is:
96 * special processing is needed for "page hash chains" (i.e., sequences of
97 * pages filled with entries all having the same hash value).
99 * First, such chains have to be detected. To this end, server returns to the
100 * client the hash of the first entry on the page next to one returned. When
101 * client detects that this hash is the same as hash of the first entry on the
102 * returned page, page hash collision has to be handled. Pages in the
103 * hash chain, except first one, are termed "overflow pages".
105 * Proposed (unimplimented) solution to index uniqueness problem is to
106 * not cache overflow pages. Instead, when page hash collision is
107 * detected, all overflow pages from emerging chain should be
108 * immediately requested from the server and placed in a special data
109 * structure. This data structure can be used by ll_readdir() to
110 * process entries from overflow pages. When readdir invocation
111 * finishes, overflow pages are discarded. If page hash collision chain
112 * weren't completely processed, next call to readdir will again detect
113 * page hash collision, again read overflow pages in, process next
114 * portion of entries and again discard the pages. This is not as
115 * wasteful as it looks, because, given reasonable hash, page hash
116 * 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,
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);
180 #ifdef HAVE_DIR_CONTEXT
181 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
182 struct dir_context *ctx)
185 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
186 void *cookie, filldir_t filldir)
189 struct ll_sb_info *sbi = ll_i2sbi(inode);
191 bool is_api32 = ll_need_32bit_api(sbi);
192 bool is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
198 page = ll_get_dir_page(inode, op_data, pos);
200 while (rc == 0 && !done) {
201 struct lu_dirpage *dp;
202 struct lu_dirent *ent;
211 hash = MDS_DIR_END_OFF;
212 dp = page_address(page);
213 for (ent = lu_dirent_start(dp); ent != NULL && !done;
214 ent = lu_dirent_next(ent)) {
221 hash = le64_to_cpu(ent->lde_hash);
222 if (hash < pos) /* Skip until we find target hash */
225 namelen = le16_to_cpu(ent->lde_namelen);
226 if (namelen == 0) /* Skip dummy record */
229 if (is_api32 && is_hash64)
233 fid_le_to_cpu(&fid, &ent->lde_fid);
234 ino = cl_fid_build_ino(&fid, is_api32);
235 type = S_DT(lu_dirent_type_get(ent));
236 /* For ll_nfs_get_name_filldir(), it will try to access
237 * 'ent' through 'lde_name', so the parameter 'name'
238 * for 'filldir()' must be part of the 'ent'. */
239 #ifdef HAVE_DIR_CONTEXT
241 done = !dir_emit(ctx, ent->lde_name, namelen, ino,
244 done = filldir(cookie, ent->lde_name, namelen, lhash,
251 ll_release_page(inode, page, false);
255 next = le64_to_cpu(dp->ldp_hash_end);
257 if (pos == MDS_DIR_END_OFF) {
259 * End of directory reached.
262 ll_release_page(inode, page, false);
265 * Normal case: continue to the next
268 ll_release_page(inode, page,
269 le32_to_cpu(dp->ldp_flags) &
272 page = ll_get_dir_page(inode, op_data, pos);
275 #ifdef HAVE_DIR_CONTEXT
283 #ifdef HAVE_DIR_CONTEXT
284 static int ll_iterate(struct file *filp, struct dir_context *ctx)
286 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
289 struct inode *inode = file_inode(filp);
290 struct ll_file_data *lfd = filp->private_data;
291 struct ll_sb_info *sbi = ll_i2sbi(inode);
292 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
293 int api32 = ll_need_32bit_api(sbi);
294 struct md_op_data *op_data;
295 struct lu_fid pfid = { 0 };
296 ktime_t kstart = ktime_get();
307 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
308 PFID(ll_inode2fid(inode)),
309 inode, (unsigned long)pos, i_size_read(inode), api32);
311 if (pos == MDS_DIR_END_OFF)
317 if (unlikely(ll_dir_striped(inode))) {
319 * This is only needed for striped dir to fill ..,
320 * see lmv_read_page()
322 if (file_dentry(filp)->d_parent != NULL &&
323 file_dentry(filp)->d_parent->d_inode != NULL) {
324 __u64 ibits = MDS_INODELOCK_LOOKUP;
325 struct inode *parent =
326 file_dentry(filp)->d_parent->d_inode;
328 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
329 pfid = *ll_inode2fid(parent);
332 /* If it can not find in cache, do lookup .. on the master
334 if (fid_is_zero(&pfid)) {
335 rc = ll_dir_get_parent_fid(inode, &pfid);
341 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
342 LUSTRE_OPC_ANY, inode);
344 GOTO(out, rc = PTR_ERR(op_data));
346 /* foreign dirs are browsed out of Lustre */
347 if (unlikely(op_data->op_mea1 != NULL &&
348 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
349 ll_finish_md_op_data(op_data);
353 op_data->op_fid3 = pfid;
355 #ifdef HAVE_DIR_CONTEXT
357 rc = ll_dir_read(inode, &pos, op_data, ctx);
360 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
365 if (pos == MDS_DIR_END_OFF) {
367 pos = LL_DIR_END_OFF_32BIT;
369 pos = LL_DIR_END_OFF;
374 #ifdef HAVE_DIR_CONTEXT
379 ll_finish_md_op_data(op_data);
383 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
384 ktime_us_delta(ktime_get(), kstart));
390 * Create striped directory with specified stripe(@lump)
392 * \param[in] dparent the parent of the directory.
393 * \param[in] lump the specified stripes.
394 * \param[in] dirname the name of the directory.
395 * \param[in] mode the specified mode of the directory.
397 * \retval =0 if striped directory is being created successfully.
398 * <0 if the creation is failed.
400 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
401 size_t len, const char *dirname, umode_t mode,
404 struct inode *parent = dparent->d_inode;
405 struct ptlrpc_request *request = NULL;
406 struct md_op_data *op_data;
407 struct ll_sb_info *sbi = ll_i2sbi(parent);
408 struct inode *inode = NULL;
409 struct dentry dentry = {
413 .len = strlen(dirname),
414 .hash = ll_full_name_hash(dparent, dirname,
418 bool encrypt = false;
422 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
425 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
427 "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
428 PFID(ll_inode2fid(parent)), parent, dirname,
429 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
431 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
434 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
435 PFID(ll_inode2fid(parent)), parent, dirname,
436 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
439 if (lump->lum_stripe_count > 1 &&
440 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
443 if (IS_DEADDIR(parent) &&
444 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
447 /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
448 * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
450 if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
451 enum lmv_hash_type type = lump->lum_hash_type &
454 if (type == LMV_HASH_TYPE_CRUSH ||
455 type == LMV_HASH_TYPE_UNKNOWN)
456 lump->lum_hash_type = (lump->lum_hash_type ^ type) |
457 LMV_HASH_TYPE_FNV_1A_64;
460 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
461 lustre_swab_lmv_user_md(lump);
463 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
464 mode &= ~current_umask();
465 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
466 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
467 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
470 RETURN(PTR_ERR(op_data));
472 if (ll_sbi_has_encrypt(sbi) &&
473 (IS_ENCRYPTED(parent) ||
474 unlikely(llcrypt_dummy_context_enabled(parent)))) {
475 err = llcrypt_get_encryption_info(parent);
477 GOTO(out_op_data, err);
478 if (!llcrypt_has_encryption_key(parent))
479 GOTO(out_op_data, err = -ENOKEY);
483 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
484 /* selinux_dentry_init_security() uses dentry->d_parent and name
485 * to determine the security context for the file. So our fake
486 * dentry should be real enough for this purpose. */
487 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
488 &op_data->op_file_secctx_name,
489 &op_data->op_file_secctx,
490 &op_data->op_file_secctx_size);
492 GOTO(out_op_data, err);
496 err = llcrypt_inherit_context(parent, NULL, op_data, false);
498 GOTO(out_op_data, err);
501 op_data->op_cli_flags |= CLI_SET_MEA;
503 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
505 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
506 from_kuid(&init_user_ns, current_fsuid()),
507 from_kgid(&init_user_ns, current_fsgid()),
508 cfs_curproc_cap_pack(), 0, &request);
510 GOTO(out_request, err);
512 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
514 err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
516 GOTO(out_inode, err);
518 dentry.d_inode = inode;
520 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
521 /* no need to protect selinux_inode_setsecurity() by
522 * inode_lock. Taking it would lead to a client deadlock
525 err = security_inode_notifysecctx(inode,
526 op_data->op_file_secctx,
527 op_data->op_file_secctx_size);
529 err = ll_inode_init_security(&dentry, inode, parent);
532 GOTO(out_inode, err);
535 err = ll_set_encflags(inode, op_data->op_file_encctx,
536 op_data->op_file_encctx_size, false);
538 GOTO(out_inode, err);
544 ptlrpc_req_finished(request);
546 ll_finish_md_op_data(op_data);
551 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
554 struct ll_sb_info *sbi = ll_i2sbi(inode);
555 struct md_op_data *op_data;
556 struct ptlrpc_request *req = NULL;
562 switch (lump->lmm_magic) {
563 case LOV_USER_MAGIC_V1:
564 lum_size = sizeof(struct lov_user_md_v1);
566 case LOV_USER_MAGIC_V3:
567 lum_size = sizeof(struct lov_user_md_v3);
569 case LOV_USER_MAGIC_COMP_V1:
570 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
573 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
574 lustre_swab_lmv_user_md(
575 (struct lmv_user_md *)lump);
576 lum_size = sizeof(struct lmv_user_md);
578 case LOV_USER_MAGIC_SPECIFIC: {
579 struct lov_user_md_v3 *v3 =
580 (struct lov_user_md_v3 *)lump;
581 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
583 lum_size = lov_user_md_size(v3->lmm_stripe_count,
584 LOV_USER_MAGIC_SPECIFIC);
588 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
589 " %#08x != %#08x nor %#08x\n",
590 lump->lmm_magic, LOV_USER_MAGIC_V1,
596 * This is coming from userspace, so should be in
597 * local endian. But the MDS would like it in little
598 * endian, so we swab it before we send it.
600 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
601 le32_to_cpu(LOV_MAGIC_MAGIC))
602 lustre_swab_lov_user_md(lump, 0);
604 lum_size = sizeof(struct lov_user_md_v1);
607 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
608 LUSTRE_OPC_ANY, NULL);
610 RETURN(PTR_ERR(op_data));
612 /* swabbing is done in lov_setstripe() on server side */
613 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
614 ll_finish_md_op_data(op_data);
615 ptlrpc_req_finished(req);
622 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
624 struct ptlrpc_request **request, u64 valid,
625 enum get_default_layout_type type)
627 struct ll_sb_info *sbi = ll_i2sbi(inode);
628 struct mdt_body *body;
629 struct lov_mds_md *lmm = NULL;
630 struct ptlrpc_request *req = NULL;
632 struct md_op_data *op_data;
636 rc = ll_get_default_mdsize(sbi, &lmm_size);
640 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
641 0, lmm_size, LUSTRE_OPC_ANY,
644 RETURN(PTR_ERR(op_data));
646 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
648 if (type == GET_DEFAULT_LAYOUT_ROOT) {
649 lu_root_fid(&op_data->op_fid1);
650 fid = op_data->op_fid1;
652 fid = *ll_inode2fid(inode);
655 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
656 ll_finish_md_op_data(op_data);
658 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
663 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
664 LASSERT(body != NULL);
666 lmm_size = body->mbo_eadatasize;
668 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
670 GOTO(out, rc = -ENODATA);
673 lmm = req_capsule_server_sized_get(&req->rq_pill,
674 &RMF_MDT_MD, lmm_size);
675 LASSERT(lmm != NULL);
678 * This is coming from the MDS, so is probably in
679 * little endian. We convert it to host endian before
680 * passing it to userspace.
682 /* We don't swab objects for directories */
683 switch (le32_to_cpu(lmm->lmm_magic)) {
686 case LOV_MAGIC_COMP_V1:
687 case LOV_USER_MAGIC_SPECIFIC:
688 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
689 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
692 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
693 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
696 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
697 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
699 case LMV_MAGIC_FOREIGN: {
700 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
702 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
703 __swab32s(&lfm->lfm_magic);
704 __swab32s(&lfm->lfm_length);
705 __swab32s(&lfm->lfm_type);
706 __swab32s(&lfm->lfm_flags);
711 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
716 *plmm_size = lmm_size;
722 * This function will be used to get default LOV/LMV/Default LMV
723 * @valid will be used to indicate which stripe it will retrieve.
724 * If the directory does not have its own default layout, then the
725 * function will request the default layout from root FID.
726 * OBD_MD_MEA LMV stripe EA
727 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
728 * otherwise Default LOV EA.
729 * Each time, it can only retrieve 1 stripe EA
731 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
732 struct ptlrpc_request **request,
733 struct ptlrpc_request **root_request,
736 struct ptlrpc_request *req = NULL;
737 struct ptlrpc_request *root_req = NULL;
738 struct lov_mds_md *lmm = NULL;
743 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
745 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
746 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL){
747 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
748 &lmm_size, &root_req, valid,
749 GET_DEFAULT_LAYOUT_ROOT);
755 *plmm_size = lmm_size;
757 if (root_request != NULL)
758 *root_request = root_req;
764 * This function will be used to get default LOV/LMV/Default LMV
765 * @valid will be used to indicate which stripe it will retrieve
766 * OBD_MD_MEA LMV stripe EA
767 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
768 * otherwise Default LOV EA.
769 * Each time, it can only retrieve 1 stripe EA
771 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
772 struct ptlrpc_request **request, u64 valid)
774 struct ptlrpc_request *req = NULL;
775 struct lov_mds_md *lmm = NULL;
780 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
784 *plmm_size = lmm_size;
790 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
792 struct md_op_data *op_data;
797 OBD_ALLOC_PTR(op_data);
801 op_data->op_flags |= MF_GET_MDT_IDX;
802 op_data->op_fid1 = *fid;
803 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
804 mdt_index = op_data->op_mds;
805 OBD_FREE_PTR(op_data);
813 * Get MDT index for the inode.
815 int ll_get_mdt_idx(struct inode *inode)
817 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
821 * Generic handler to do any pre-copy work.
823 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
824 * first information for it that real work has started.
826 * Moreover, for a ARCHIVE request, it will sample the file data version and
827 * store it in \a copy.
829 * \return 0 on success.
831 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
833 struct ll_sb_info *sbi = ll_s2sbi(sb);
834 struct hsm_progress_kernel hpk;
839 /* Forge a hsm_progress based on data from copy. */
840 hpk.hpk_fid = copy->hc_hai.hai_fid;
841 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
842 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
843 hpk.hpk_extent.length = 0;
846 hpk.hpk_data_version = 0;
849 /* For archive request, we need to read the current file version. */
850 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
852 __u64 data_version = 0;
854 /* Get inode for this fid */
855 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
857 hpk.hpk_flags |= HP_FLAG_RETRY;
858 /* hpk_errval is >= 0 */
859 hpk.hpk_errval = -PTR_ERR(inode);
860 GOTO(progress, rc = PTR_ERR(inode));
863 /* Read current file data version */
864 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
867 CDEBUG(D_HSM, "Could not read file data version of "
868 DFID" (rc = %d). Archive request ("
869 "%#llx) could not be done.\n",
870 PFID(©->hc_hai.hai_fid), rc,
871 copy->hc_hai.hai_cookie);
872 hpk.hpk_flags |= HP_FLAG_RETRY;
873 /* hpk_errval must be >= 0 */
874 hpk.hpk_errval = -rc;
878 /* Store in the hsm_copy for later copytool use.
879 * Always modified even if no lsm. */
880 copy->hc_data_version = data_version;
884 /* On error, the request should be considered as completed */
885 if (hpk.hpk_errval > 0)
886 hpk.hpk_flags |= HP_FLAG_COMPLETED;
888 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
891 /* Return first error */
892 RETURN(rc != 0 ? rc : rc2);
896 * Generic handler to do any post-copy work.
898 * It will send the last hsm_progress update to coordinator to inform it
899 * that copy is finished and whether it was successful or not.
902 * - for ARCHIVE request, it will sample the file data version and compare it
903 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
904 * will be considered as failed.
905 * - for RESTORE request, it will sample the file data version and send it to
906 * coordinator which is useful if the file was imported as 'released'.
908 * \return 0 on success.
910 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
912 struct ll_sb_info *sbi = ll_s2sbi(sb);
913 struct hsm_progress_kernel hpk;
918 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
919 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
920 * initialized if copy_end was called with copy == NULL.
923 /* Forge a hsm_progress based on data from copy. */
924 hpk.hpk_fid = copy->hc_hai.hai_fid;
925 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
926 hpk.hpk_extent = copy->hc_hai.hai_extent;
927 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
928 hpk.hpk_errval = copy->hc_errval;
929 hpk.hpk_data_version = 0;
931 /* For archive request, we need to check the file data was not changed.
933 * For restore request, we need to send the file data version, this is
934 * useful when the file was created using hsm_import.
936 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
937 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
938 (copy->hc_errval == 0)) {
940 __u64 data_version = 0;
942 /* Get lsm for this fid */
943 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
945 hpk.hpk_flags |= HP_FLAG_RETRY;
946 /* hpk_errval must be >= 0 */
947 hpk.hpk_errval = -PTR_ERR(inode);
948 GOTO(progress, rc = PTR_ERR(inode));
951 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
954 CDEBUG(D_HSM, "Could not read file data version. "
955 "Request could not be confirmed.\n");
956 if (hpk.hpk_errval == 0)
957 hpk.hpk_errval = -rc;
961 /* Store in the hsm_copy for later copytool use.
962 * Always modified even if no lsm. */
963 hpk.hpk_data_version = data_version;
965 /* File could have been stripped during archiving, so we need
966 * to check anyway. */
967 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
968 (copy->hc_data_version != data_version)) {
969 CDEBUG(D_HSM, "File data version mismatched. "
970 "File content was changed during archiving. "
971 DFID", start:%#llx current:%#llx\n",
972 PFID(©->hc_hai.hai_fid),
973 copy->hc_data_version, data_version);
974 /* File was changed, send error to cdt. Do not ask for
975 * retry because if a file is modified frequently,
976 * the cdt will loop on retried archive requests.
977 * The policy engine will ask for a new archive later
978 * when the file will not be modified for some tunable
980 hpk.hpk_flags &= ~HP_FLAG_RETRY;
982 /* hpk_errval must be >= 0 */
983 hpk.hpk_errval = -rc;
990 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
993 /* Return first error */
994 RETURN(rc != 0 ? rc : rc2);
998 static int copy_and_ct_start(int cmd, struct obd_export *exp,
999 const struct lustre_kernelcomm __user *data)
1001 struct lustre_kernelcomm *lk;
1002 struct lustre_kernelcomm *tmp;
1003 size_t size = sizeof(*lk);
1008 /* copy data from userspace to get numbers of archive_id */
1009 OBD_ALLOC(lk, size);
1013 if (copy_from_user(lk, data, size))
1014 GOTO(out_lk, rc = -EFAULT);
1016 if (lk->lk_flags & LK_FLG_STOP)
1019 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1020 __u32 archive_mask = lk->lk_data_count;
1023 /* old hsm agent to old MDS */
1024 if (!exp_connect_archive_id_array(exp))
1027 /* old hsm agent to new MDS */
1028 lk->lk_flags |= LK_FLG_DATANR;
1030 if (archive_mask == 0)
1033 count = hweight32(archive_mask);
1034 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1035 OBD_ALLOC(tmp, new_size);
1037 GOTO(out_lk, rc = -ENOMEM);
1039 memcpy(tmp, lk, size);
1040 tmp->lk_data_count = count;
1046 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1047 if (BIT(i) & archive_mask) {
1048 lk->lk_data[count] = i + 1;
1055 /* new hsm agent to new mds */
1056 if (lk->lk_data_count > 0) {
1057 new_size = offsetof(struct lustre_kernelcomm,
1058 lk_data[lk->lk_data_count]);
1059 OBD_ALLOC(tmp, new_size);
1061 GOTO(out_lk, rc = -ENOMEM);
1067 if (copy_from_user(lk, data, size))
1068 GOTO(out_lk, rc = -EFAULT);
1071 /* new hsm agent to old MDS */
1072 if (!exp_connect_archive_id_array(exp)) {
1075 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1076 GOTO(out_lk, rc = -EINVAL);
1078 for (i = 0; i < lk->lk_data_count; i++) {
1079 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1081 CERROR("%s: archive id %d requested but only "
1082 "[0 - %zu] supported: rc = %d\n",
1083 exp->exp_obd->obd_name, lk->lk_data[i],
1084 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1088 if (lk->lk_data[i] == 0) {
1093 archives |= (1 << (lk->lk_data[i] - 1));
1095 lk->lk_flags &= ~LK_FLG_DATANR;
1096 lk->lk_data_count = archives;
1099 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1105 static int check_owner(int type, int id)
1109 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1113 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1122 int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1124 int cmd = qctl->qc_cmd;
1125 int type = qctl->qc_type;
1126 int id = qctl->qc_id;
1127 int valid = qctl->qc_valid;
1135 case LUSTRE_Q_SETDEFAULT:
1136 case LUSTRE_Q_SETQUOTAPOOL:
1137 case LUSTRE_Q_SETINFOPOOL:
1138 case LUSTRE_Q_SETDEFAULT_POOL:
1139 if (!capable(CAP_SYS_ADMIN))
1143 case LUSTRE_Q_GETDEFAULT:
1144 case LUSTRE_Q_GETQUOTAPOOL:
1145 case LUSTRE_Q_GETDEFAULT_POOL:
1146 if (check_owner(type, id) &&
1147 (!capable(CAP_SYS_ADMIN)))
1151 case LUSTRE_Q_GETINFOPOOL:
1154 CERROR("unsupported quotactl op: %#x\n", cmd);
1158 if (valid != QC_GENERAL) {
1159 if (cmd == Q_GETINFO)
1160 qctl->qc_cmd = Q_GETOINFO;
1161 else if (cmd == Q_GETQUOTA ||
1162 cmd == LUSTRE_Q_GETQUOTAPOOL)
1163 qctl->qc_cmd = Q_GETOQUOTA;
1169 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1170 sizeof(*qctl), qctl, NULL);
1173 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1174 sizeof(*qctl), qctl, NULL);
1177 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1178 sizeof(*qctl), qctl, NULL);
1180 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1182 sizeof(*qctl), qctl, NULL);
1194 struct obd_quotactl *oqctl;
1195 int oqctl_len = sizeof(*oqctl);
1197 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1198 oqctl_len += LOV_MAXPOOLNAME + 1;
1200 OBD_ALLOC(oqctl, oqctl_len);
1204 QCTL_COPY(oqctl, qctl);
1205 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1207 OBD_FREE(oqctl, oqctl_len);
1210 /* If QIF_SPACE is not set, client should collect the
1211 * space usage from OSSs by itself */
1212 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1213 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1214 !oqctl->qc_dqblk.dqb_curspace) {
1215 struct obd_quotactl *oqctl_tmp;
1216 int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1218 OBD_ALLOC(oqctl_tmp, qctl_len);
1219 if (oqctl_tmp == NULL)
1220 GOTO(out, rc = -ENOMEM);
1222 if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1223 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1224 memcpy(oqctl_tmp->qc_poolname,
1226 LOV_MAXPOOLNAME + 1);
1228 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1230 oqctl_tmp->qc_id = oqctl->qc_id;
1231 oqctl_tmp->qc_type = oqctl->qc_type;
1233 /* collect space usage from OSTs */
1234 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1235 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1236 if (!rc || rc == -EREMOTEIO) {
1237 oqctl->qc_dqblk.dqb_curspace =
1238 oqctl_tmp->qc_dqblk.dqb_curspace;
1239 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1242 /* collect space & inode usage from MDTs */
1243 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1244 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1245 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1246 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1247 if (!rc || rc == -EREMOTEIO) {
1248 oqctl->qc_dqblk.dqb_curspace +=
1249 oqctl_tmp->qc_dqblk.dqb_curspace;
1250 oqctl->qc_dqblk.dqb_curinodes =
1251 oqctl_tmp->qc_dqblk.dqb_curinodes;
1252 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1254 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1257 OBD_FREE(oqctl_tmp, qctl_len);
1260 QCTL_COPY(qctl, oqctl);
1261 OBD_FREE(oqctl, oqctl_len);
1267 int ll_rmfid(struct file *file, void __user *arg)
1269 const struct fid_array __user *ufa = arg;
1270 struct fid_array *lfa = NULL;
1273 int i, rc, *rcs = NULL;
1276 if (!capable(CAP_DAC_READ_SEARCH) &&
1277 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1279 /* Only need to get the buflen */
1280 if (get_user(nr, &ufa->fa_nr))
1282 /* DoS protection */
1283 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1286 size = offsetof(struct fid_array, fa_fids[nr]);
1287 OBD_ALLOC(lfa, size);
1290 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1292 GOTO(free_lfa, rc = -ENOMEM);
1294 if (copy_from_user(lfa, arg, size))
1295 GOTO(free_rcs, rc = -EFAULT);
1297 /* Call mdc_iocontrol */
1298 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1300 for (i = 0; i < nr; i++)
1302 lfa->fa_fids[i].f_ver = rcs[i];
1303 if (copy_to_user(arg, lfa, size))
1308 OBD_FREE_PTR_ARRAY(rcs, nr);
1310 OBD_FREE(lfa, size);
1315 /* This function tries to get a single name component,
1316 * to send to the server. No actual path traversal involved,
1317 * so we limit to NAME_MAX */
1318 static char *ll_getname(const char __user *filename)
1323 OBD_ALLOC(tmp, NAME_MAX + 1);
1326 return ERR_PTR(-ENOMEM);
1328 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1331 else if (len > NAME_MAX)
1332 ret = -ENAMETOOLONG;
1335 OBD_FREE(tmp, NAME_MAX + 1);
1341 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1343 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1345 struct dentry *dentry = file_dentry(file);
1346 struct inode *inode = file_inode(file);
1347 struct ll_sb_info *sbi = ll_i2sbi(inode);
1348 struct obd_ioctl_data *data = NULL;
1352 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1353 PFID(ll_inode2fid(inode)), inode, cmd);
1355 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1356 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1359 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1361 case FS_IOC_GETFLAGS:
1362 case FS_IOC_SETFLAGS:
1363 RETURN(ll_iocontrol(inode, file, cmd, arg));
1364 case FSFILT_IOC_GETVERSION:
1365 case FS_IOC_GETVERSION:
1366 RETURN(put_user(inode->i_generation, (int __user *)arg));
1367 /* We need to special case any other ioctls we want to handle,
1368 * to send them to the MDS/OST as appropriate and to properly
1369 * network encode the arg field. */
1370 case FS_IOC_SETVERSION:
1373 case LL_IOC_GET_MDTIDX: {
1376 mdtidx = ll_get_mdt_idx(inode);
1380 if (put_user((int)mdtidx, (int __user *)arg))
1385 case IOC_MDC_LOOKUP: {
1386 int namelen, len = 0;
1389 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1393 filename = data->ioc_inlbuf1;
1394 namelen = strlen(filename);
1396 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1397 GOTO(out_free, rc = -EINVAL);
1400 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1402 CERROR("%s: lookup %.*s failed: rc = %d\n",
1403 sbi->ll_fsname, namelen, filename, rc);
1407 OBD_FREE_LARGE(data, len);
1410 case LL_IOC_LMV_SETSTRIPE: {
1411 struct lmv_user_md *lum;
1416 bool createonly = false;
1420 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1424 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1425 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1426 GOTO(lmv_out_free, rc = -EINVAL);
1428 filename = data->ioc_inlbuf1;
1429 namelen = data->ioc_inllen1;
1432 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1433 GOTO(lmv_out_free, rc = -EINVAL);
1435 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1436 lumlen = data->ioc_inllen2;
1438 if (!lmv_user_magic_supported(lum->lum_magic)) {
1439 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1440 lum->lum_magic, -EINVAL);
1441 GOTO(lmv_out_free, rc = -EINVAL);
1444 if ((lum->lum_magic == LMV_USER_MAGIC ||
1445 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1446 lumlen < sizeof(*lum)) {
1447 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1448 filename, lumlen, lum->lum_magic, -EINVAL);
1449 GOTO(lmv_out_free, rc = -EINVAL);
1452 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1453 lumlen < sizeof(struct lmv_foreign_md)) {
1454 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1455 filename, lum->lum_magic, lumlen, -EFAULT);
1456 GOTO(lmv_out_free, rc = -EINVAL);
1459 mode = data->ioc_type;
1460 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1461 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1464 OBD_FREE_LARGE(data, len);
1468 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1469 struct lmv_user_md lum;
1470 struct lmv_user_md __user *ulump =
1471 (struct lmv_user_md __user *)arg;
1474 if (copy_from_user(&lum, ulump, sizeof(lum)))
1477 if (lum.lum_magic != LMV_USER_MAGIC)
1480 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1484 case LL_IOC_LOV_SETSTRIPE_NEW:
1485 case LL_IOC_LOV_SETSTRIPE: {
1486 struct lov_user_md_v3 *lumv3 = NULL;
1487 struct lov_user_md_v1 lumv1;
1488 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1489 struct lov_user_md_v1 __user *lumv1p =
1490 (struct lov_user_md_v1 __user *)arg;
1491 struct lov_user_md_v3 __user *lumv3p =
1492 (struct lov_user_md_v3 __user *)arg;
1495 int set_default = 0;
1497 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1498 sizeof(struct lov_comp_md_v1));
1499 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1500 /* first try with v1 which is smaller than v3 */
1501 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1504 if (is_root_inode(inode))
1507 switch (lumv1.lmm_magic) {
1508 case LOV_USER_MAGIC_V3:
1509 case LOV_USER_MAGIC_SPECIFIC:
1510 lum_size = ll_lov_user_md_size(&lumv1);
1513 OBD_ALLOC(lumv3, lum_size);
1516 if (copy_from_user(lumv3, lumv3p, lum_size))
1517 GOTO(out, rc = -EFAULT);
1518 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1520 case LOV_USER_MAGIC_V1:
1523 GOTO(out, rc = -ENOTSUPP);
1526 /* in v1 and v3 cases lumv1 points to data */
1527 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1530 OBD_FREE(lumv3, lum_size);
1533 case LL_IOC_LMV_GETSTRIPE: {
1534 struct lmv_user_md __user *ulmv =
1535 (struct lmv_user_md __user *)arg;
1536 struct lmv_user_md lum;
1537 struct ptlrpc_request *request = NULL;
1538 struct ptlrpc_request *root_request = NULL;
1539 union lmv_mds_md *lmm = NULL;
1542 struct lmv_user_md *tmp = NULL;
1546 int max_stripe_count;
1550 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1553 max_stripe_count = lum.lum_stripe_count;
1554 /* lum_magic will indicate which stripe the ioctl will like
1555 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1556 * is for default LMV stripe */
1557 if (lum.lum_magic == LMV_MAGIC_V1)
1558 valid |= OBD_MD_MEA;
1559 else if (lum.lum_magic == LMV_USER_MAGIC)
1560 valid |= OBD_MD_DEFAULT_MEA;
1564 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1565 &request, &root_request, valid);
1567 GOTO(finish_req, rc);
1569 /* Get default LMV EA */
1570 if (lum.lum_magic == LMV_USER_MAGIC) {
1571 if (lmmsize > sizeof(*ulmv))
1572 GOTO(finish_req, rc = -EINVAL);
1574 if (copy_to_user(ulmv, lmm, lmmsize))
1575 GOTO(finish_req, rc = -EFAULT);
1577 GOTO(finish_req, rc);
1580 /* if foreign LMV case, fake stripes number */
1581 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1582 struct lmv_foreign_md *lfm;
1584 lfm = (struct lmv_foreign_md *)lmm;
1585 if (lfm->lfm_length < XATTR_SIZE_MAX -
1586 offsetof(typeof(*lfm), lfm_value)) {
1587 __u32 size = lfm->lfm_length +
1588 offsetof(typeof(*lfm), lfm_value);
1590 stripe_count = lmv_foreign_to_md_stripes(size);
1592 CERROR("invalid %d foreign size returned\n",
1597 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1599 if (max_stripe_count < stripe_count) {
1600 lum.lum_stripe_count = stripe_count;
1601 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1602 GOTO(finish_req, rc = -EFAULT);
1603 GOTO(finish_req, rc = -E2BIG);
1606 /* enough room on user side and foreign case */
1607 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1608 struct lmv_foreign_md *lfm;
1611 lfm = (struct lmv_foreign_md *)lmm;
1612 size = lfm->lfm_length +
1613 offsetof(struct lmv_foreign_md, lfm_value);
1614 if (copy_to_user(ulmv, lfm, size))
1615 GOTO(finish_req, rc = -EFAULT);
1616 GOTO(finish_req, rc);
1619 lum_size = lmv_user_md_size(stripe_count,
1620 LMV_USER_MAGIC_SPECIFIC);
1621 OBD_ALLOC(tmp, lum_size);
1623 GOTO(finish_req, rc = -ENOMEM);
1625 mdt_index = ll_get_mdt_idx(inode);
1627 GOTO(out_tmp, rc = -ENOMEM);
1629 tmp->lum_magic = LMV_MAGIC_V1;
1630 tmp->lum_stripe_count = 0;
1631 tmp->lum_stripe_offset = mdt_index;
1632 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1633 for (i = 0; i < stripe_count; i++) {
1636 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1637 if (fid_is_sane(&fid)) {
1638 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1640 GOTO(out_tmp, rc = mdt_index);
1642 tmp->lum_objects[i].lum_mds = mdt_index;
1643 tmp->lum_objects[i].lum_fid = fid;
1646 tmp->lum_stripe_count++;
1649 if (copy_to_user(ulmv, tmp, lum_size))
1650 GOTO(out_tmp, rc = -EFAULT);
1652 OBD_FREE(tmp, lum_size);
1654 ptlrpc_req_finished(request);
1655 ptlrpc_req_finished(root_request);
1659 case LL_IOC_UNLOCK_FOREIGN:
1660 /* if not a foreign symlink do nothing */
1661 if (ll_foreign_is_removable(dentry, true)) {
1663 "prevent rmdir of non-foreign dir ("DFID")\n",
1664 PFID(ll_inode2fid(inode)));
1665 RETURN(-EOPNOTSUPP);
1669 case LL_IOC_REMOVE_ENTRY: {
1670 char *filename = NULL;
1674 /* Here is a little hack to avoid sending REINT_RMENTRY to
1675 * unsupported server, which might crash the server(LU-2730),
1676 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1677 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1678 * server will support REINT_RMENTRY XXX*/
1679 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1682 filename = ll_getname((const char __user *)arg);
1683 if (IS_ERR(filename))
1684 RETURN(PTR_ERR(filename));
1686 namelen = strlen(filename);
1688 GOTO(out_rmdir, rc = -EINVAL);
1690 rc = ll_rmdir_entry(inode, filename, namelen);
1693 ll_putname(filename);
1697 RETURN(ll_rmfid(file, (void __user *)arg));
1698 case LL_IOC_LOV_SWAP_LAYOUTS:
1700 case IOC_OBD_STATFS:
1701 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1702 case LL_IOC_LOV_GETSTRIPE:
1703 case LL_IOC_LOV_GETSTRIPE_NEW:
1704 case LL_IOC_MDC_GETINFO_V1:
1705 case LL_IOC_MDC_GETINFO_V2:
1706 case IOC_MDC_GETFILEINFO_V1:
1707 case IOC_MDC_GETFILEINFO_V2:
1708 case IOC_MDC_GETFILESTRIPE: {
1709 struct ptlrpc_request *request = NULL;
1710 struct ptlrpc_request *root_request = NULL;
1711 struct lov_user_md __user *lump;
1712 struct lov_mds_md *lmm = NULL;
1713 struct mdt_body *body;
1714 char *filename = NULL;
1715 lstat_t __user *statp = NULL;
1716 lstatx_t __user *stxp = NULL;
1717 __u64 __user *flagsp = NULL;
1718 __u32 __user *lmmsizep = NULL;
1719 struct lu_fid __user *fidp = NULL;
1722 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1723 cmd == IOC_MDC_GETFILEINFO_V2 ||
1724 cmd == IOC_MDC_GETFILESTRIPE) {
1725 filename = ll_getname((const char __user *)arg);
1726 if (IS_ERR(filename))
1727 RETURN(PTR_ERR(filename));
1729 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1730 &lmmsize, &request);
1732 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1738 body = req_capsule_server_get(&request->rq_pill,
1740 LASSERT(body != NULL);
1745 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1746 cmd == LL_IOC_MDC_GETINFO_V1 ||
1747 cmd == IOC_MDC_GETFILEINFO_V2 ||
1748 cmd == LL_IOC_MDC_GETINFO_V2)) {
1756 if (cmd == IOC_MDC_GETFILESTRIPE ||
1757 cmd == LL_IOC_LOV_GETSTRIPE ||
1758 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1759 lump = (struct lov_user_md __user *)arg;
1760 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1761 cmd == LL_IOC_MDC_GETINFO_V1){
1762 struct lov_user_mds_data_v1 __user *lmdp;
1764 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1765 statp = &lmdp->lmd_st;
1766 lump = &lmdp->lmd_lmm;
1768 struct lov_user_mds_data __user *lmdp;
1770 lmdp = (struct lov_user_mds_data __user *)arg;
1771 fidp = &lmdp->lmd_fid;
1772 stxp = &lmdp->lmd_stx;
1773 flagsp = &lmdp->lmd_flags;
1774 lmmsizep = &lmdp->lmd_lmmsize;
1775 lump = &lmdp->lmd_lmm;
1779 /* If the file has no striping then zero out *lump so
1780 * that the caller isn't confused by garbage. */
1781 if (clear_user(lump, sizeof(*lump)))
1782 GOTO(out_req, rc = -EFAULT);
1783 } else if (copy_to_user(lump, lmm, lmmsize)) {
1784 if (copy_to_user(lump, lmm, sizeof(*lump)))
1785 GOTO(out_req, rc = -EFAULT);
1789 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1790 cmd == LL_IOC_MDC_GETINFO_V1) {
1793 st.st_dev = inode->i_sb->s_dev;
1794 st.st_mode = body->mbo_mode;
1795 st.st_nlink = body->mbo_nlink;
1796 st.st_uid = body->mbo_uid;
1797 st.st_gid = body->mbo_gid;
1798 st.st_rdev = body->mbo_rdev;
1799 st.st_size = body->mbo_size;
1800 st.st_blksize = PAGE_SIZE;
1801 st.st_blocks = body->mbo_blocks;
1802 st.st_atime = body->mbo_atime;
1803 st.st_mtime = body->mbo_mtime;
1804 st.st_ctime = body->mbo_ctime;
1805 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1809 if (copy_to_user(statp, &st, sizeof(st)))
1810 GOTO(out_req, rc = -EFAULT);
1811 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1812 cmd == LL_IOC_MDC_GETINFO_V2) {
1813 lstatx_t stx = { 0 };
1814 __u64 valid = body->mbo_valid;
1816 stx.stx_blksize = PAGE_SIZE;
1817 stx.stx_nlink = body->mbo_nlink;
1818 stx.stx_uid = body->mbo_uid;
1819 stx.stx_gid = body->mbo_gid;
1820 stx.stx_mode = body->mbo_mode;
1821 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1824 stx.stx_size = body->mbo_size;
1825 stx.stx_blocks = body->mbo_blocks;
1826 stx.stx_atime.tv_sec = body->mbo_atime;
1827 stx.stx_ctime.tv_sec = body->mbo_ctime;
1828 stx.stx_mtime.tv_sec = body->mbo_mtime;
1829 stx.stx_btime.tv_sec = body->mbo_btime;
1830 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1831 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1832 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1833 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1834 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1837 * For a striped directory, the size and blocks returned
1838 * from MDT is not correct.
1839 * The size and blocks are aggregated by client across
1841 * Thus for a striped directory, do not return the valid
1842 * FLSIZE and FLBLOCKS flags to the caller.
1843 * However, this whould be better decided by the MDS
1844 * instead of the client.
1846 if (cmd == LL_IOC_MDC_GETINFO_V2 &&
1847 ll_i2info(inode)->lli_lsm_md != NULL)
1848 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1850 if (flagsp && copy_to_user(flagsp, &valid,
1852 GOTO(out_req, rc = -EFAULT);
1854 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1856 GOTO(out_req, rc = -EFAULT);
1858 if (!(valid & OBD_MD_FLSIZE))
1859 stx.stx_mask &= ~STATX_SIZE;
1860 if (!(valid & OBD_MD_FLBLOCKS))
1861 stx.stx_mask &= ~STATX_BLOCKS;
1863 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1864 GOTO(out_req, rc = -EFAULT);
1866 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1868 GOTO(out_req, rc = -EFAULT);
1873 ptlrpc_req_finished(request);
1874 ptlrpc_req_finished(root_request);
1876 ll_putname(filename);
1879 case OBD_IOC_QUOTACTL: {
1880 struct if_quotactl *qctl;
1881 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1883 OBD_ALLOC(qctl, qctl_len);
1887 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1888 GOTO(out_quotactl, rc = -EFAULT);
1890 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
1891 char __user *from = (char __user *)arg +
1892 offsetof(typeof(*qctl), qc_poolname);
1893 if (copy_from_user(qctl->qc_poolname, from,
1894 LOV_MAXPOOLNAME + 1))
1895 GOTO(out_quotactl, rc = -EFAULT);
1898 rc = quotactl_ioctl(sbi, qctl);
1900 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1904 OBD_FREE(qctl, qctl_len);
1907 case OBD_IOC_GETNAME_OLD:
1909 case OBD_IOC_GETDTNAME:
1911 case OBD_IOC_GETMDNAME:
1912 RETURN(ll_get_obd_name(inode, cmd, arg));
1913 case LL_IOC_FLUSHCTX:
1914 RETURN(ll_flush_ctx(inode));
1915 case LL_IOC_GETOBDCOUNT: {
1917 struct obd_export *exp;
1919 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1922 /* get ost count when count is zero, get mdt count otherwise */
1923 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1924 vallen = sizeof(count);
1925 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1926 KEY_TGT_COUNT, &vallen, &count);
1928 CERROR("get target count failed: %d\n", rc);
1932 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1937 case LL_IOC_PATH2FID:
1938 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1939 sizeof(struct lu_fid)))
1942 case LL_IOC_GET_CONNECT_FLAGS: {
1943 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1944 (void __user *)arg));
1946 case OBD_IOC_FID2PATH:
1947 RETURN(ll_fid2path(inode, (void __user *)arg));
1948 case LL_IOC_GETPARENT:
1949 RETURN(ll_getparent(file, (void __user *)arg));
1950 case LL_IOC_FID2MDTIDX: {
1951 struct obd_export *exp = ll_i2mdexp(inode);
1955 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1959 /* Call mdc_iocontrol */
1960 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1961 (__u32 __user *)&index);
1967 case LL_IOC_HSM_REQUEST: {
1968 struct hsm_user_request *hur;
1975 /* We don't know the true size yet; copy the fixed-size part */
1976 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1981 /* Compute the whole struct size */
1982 totalsize = hur_len(hur);
1987 /* Final size will be more than double totalsize */
1988 if (totalsize >= MDS_MAXREQSIZE / 3)
1991 OBD_ALLOC_LARGE(hur, totalsize);
1995 /* Copy the whole struct */
1996 if (copy_from_user(hur, (void __user *)arg, totalsize))
1997 GOTO(out_hur, rc = -EFAULT);
1999 if (hur->hur_request.hr_action == HUA_RELEASE) {
2000 const struct lu_fid *fid;
2004 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2005 fid = &hur->hur_user_item[i].hui_fid;
2006 f = search_inode_for_lustre(inode->i_sb, fid);
2012 rc = ll_hsm_release(f);
2018 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2023 OBD_FREE_LARGE(hur, totalsize);
2027 case LL_IOC_HSM_PROGRESS: {
2028 struct hsm_progress_kernel hpk;
2029 struct hsm_progress hp;
2031 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2034 hpk.hpk_fid = hp.hp_fid;
2035 hpk.hpk_cookie = hp.hp_cookie;
2036 hpk.hpk_extent = hp.hp_extent;
2037 hpk.hpk_flags = hp.hp_flags;
2038 hpk.hpk_errval = hp.hp_errval;
2039 hpk.hpk_data_version = 0;
2041 /* File may not exist in Lustre; all progress
2042 * reported to Lustre root */
2043 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2047 case LL_IOC_HSM_CT_START:
2048 if (!capable(CAP_SYS_ADMIN))
2051 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2052 (struct lustre_kernelcomm __user *)arg);
2055 case LL_IOC_HSM_COPY_START: {
2056 struct hsm_copy *copy;
2059 OBD_ALLOC_PTR(copy);
2062 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2067 rc = ll_ioc_copy_start(inode->i_sb, copy);
2068 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2074 case LL_IOC_HSM_COPY_END: {
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_end(inode->i_sb, copy);
2087 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2093 case LL_IOC_MIGRATE: {
2094 struct lmv_user_md *lum;
2100 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
2104 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2105 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2106 GOTO(migrate_free, rc = -EINVAL);
2108 filename = data->ioc_inlbuf1;
2109 namelen = data->ioc_inllen1;
2111 if (namelen < 1 || namelen != strlen(filename) + 1) {
2112 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2113 GOTO(migrate_free, rc = -EINVAL);
2116 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2117 if (lum->lum_magic != LMV_USER_MAGIC &&
2118 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2120 CERROR("%s: wrong lum magic %x: rc = %d\n",
2121 filename, lum->lum_magic, rc);
2122 GOTO(migrate_free, rc);
2125 rc = ll_migrate(inode, file, lum, filename);
2127 OBD_FREE_LARGE(data, len);
2131 case FS_IOC_FSGETXATTR:
2132 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2133 case FS_IOC_FSSETXATTR:
2134 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2135 case LL_IOC_PCC_DETACH_BY_FID: {
2136 struct lu_pcc_detach_fid *detach;
2138 struct inode *inode2;
2141 OBD_ALLOC_PTR(detach);
2145 if (copy_from_user(detach,
2146 (const struct lu_pcc_detach_fid __user *)arg,
2148 GOTO(out_detach, rc = -EFAULT);
2150 fid = &detach->pccd_fid;
2151 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2152 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2154 /* Target inode is not in inode cache, and PCC file
2155 * has aleady released, return immdiately.
2157 GOTO(out_detach, rc = 0);
2159 if (!S_ISREG(inode2->i_mode))
2160 GOTO(out_iput, rc = -EINVAL);
2162 if (!inode_owner_or_capable(inode2))
2163 GOTO(out_iput, rc = -EPERM);
2165 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2169 OBD_FREE_PTR(detach);
2172 #ifdef HAVE_LUSTRE_CRYPTO
2173 case LL_IOC_SET_ENCRYPTION_POLICY:
2174 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2176 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2177 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2178 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2180 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2181 case LL_IOC_ADD_ENCRYPTION_KEY:
2182 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2184 return llcrypt_ioctl_add_key(file, (void __user *)arg);
2185 case LL_IOC_REMOVE_ENCRYPTION_KEY:
2186 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2188 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2189 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2190 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2192 return llcrypt_ioctl_remove_key_all_users(file,
2193 (void __user *)arg);
2194 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2195 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2197 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2200 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2201 (void __user *)arg));
2205 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2207 struct inode *inode = file->f_mapping->host;
2208 struct ll_file_data *fd = file->private_data;
2209 struct ll_sb_info *sbi = ll_i2sbi(inode);
2210 int api32 = ll_need_32bit_api(sbi);
2211 loff_t ret = -EINVAL;
2219 offset += file->f_pos;
2225 offset += LL_DIR_END_OFF_32BIT;
2227 offset += LL_DIR_END_OFF;
2234 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2235 (!api32 && offset <= LL_DIR_END_OFF))) {
2236 if (offset != file->f_pos) {
2237 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2238 (!api32 && offset == LL_DIR_END_OFF))
2239 fd->lfd_pos = MDS_DIR_END_OFF;
2240 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2241 fd->lfd_pos = offset << 32;
2243 fd->lfd_pos = offset;
2244 file->f_pos = offset;
2245 file->f_version = 0;
2252 inode_unlock(inode);
2256 static int ll_dir_open(struct inode *inode, struct file *file)
2259 RETURN(ll_file_open(inode, file));
2262 static int ll_dir_release(struct inode *inode, struct file *file)
2265 RETURN(ll_file_release(inode, file));
2268 const struct file_operations ll_dir_operations = {
2269 .llseek = ll_dir_seek,
2270 .open = ll_dir_open,
2271 .release = ll_dir_release,
2272 .read = generic_read_dir,
2273 #ifdef HAVE_DIR_CONTEXT
2274 .iterate_shared = ll_iterate,
2276 .readdir = ll_readdir,
2278 .unlocked_ioctl = ll_dir_ioctl,