4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Directory code for lustre client.
38 #include <linux/pagemap.h>
40 #include <linux/version.h>
41 #include <linux/security.h>
42 #include <linux/user_namespace.h>
43 #include <linux/uidgid.h>
44 #include <linux/uaccess.h>
45 #include <linux/buffer_head.h> // for wait_on_buffer
46 #include <linux/pagevec.h>
48 #define DEBUG_SUBSYSTEM S_LLITE
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <uapi/linux/llcrypt.h>
54 #include <lustre_lib.h>
55 #include <lustre_dlm.h>
56 #include <lustre_fid.h>
57 #include <lustre_kernelcomm.h>
58 #include <lustre_swab.h>
60 #include "llite_internal.h"
63 * (new) readdir implementation overview.
65 * Original lustre readdir implementation cached exact copy of raw directory
66 * pages on the client. These pages were indexed in client page cache by
67 * logical offset in the directory file. This design, while very simple and
68 * intuitive had some inherent problems:
70 * . it implies that byte offset to the directory entry serves as a
71 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72 * ext3/htree directory entries may move due to splits, and more
75 * . it is incompatible with the design of split directories for cmd3,
76 * that assumes that names are distributed across nodes based on their
77 * hash, and so readdir should be done in hash order.
79 * New readdir implementation does readdir in hash order, and uses hash of a
80 * file name as a telldir/seekdir cookie. This led to number of complications:
82 * . hash is not unique, so it cannot be used to index cached directory
83 * pages on the client (note, that it requires a whole pageful of hash
84 * collided entries to cause two pages to have identical hashes);
86 * . hash is not unique, so it cannot, strictly speaking, be used as an
87 * entry cookie. ext3/htree has the same problem and lustre implementation
88 * mimics their solution: seekdir(hash) positions directory at the first
89 * entry with the given hash.
95 * Client caches directory pages using hash of the first entry as an index. As
96 * noted above hash is not unique, so this solution doesn't work as is:
97 * special processing is needed for "page hash chains" (i.e., sequences of
98 * pages filled with entries all having the same hash value).
100 * First, such chains have to be detected. To this end, server returns to the
101 * client the hash of the first entry on the page next to one returned. When
102 * client detects that this hash is the same as hash of the first entry on the
103 * returned page, page hash collision has to be handled. Pages in the
104 * hash chain, except first one, are termed "overflow pages".
106 * Solution to index uniqueness problem is to not cache overflow
107 * pages. Instead, when page hash collision is detected, all overflow pages
108 * from emerging chain are immediately requested from the server and placed in
109 * a special data structure (struct ll_dir_chain). This data structure is used
110 * by ll_readdir() to process entries from overflow pages. When readdir
111 * invocation finishes, overflow pages are discarded. If page hash collision
112 * chain weren't completely processed, next call to readdir will again detect
113 * page hash collision, again read overflow pages in, process next portion of
114 * entries and again discard the pages. This is not as wasteful as it looks,
115 * because, given reasonable hash, page hash collisions are extremely rare.
117 * 1. directory positioning
119 * When seekdir(hash) is called, original
130 * identification of and access to overflow pages
134 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
135 * a header lu_dirpage which describes the start/end hash, and whether this
136 * page is empty (contains no dir entry) or hash collide with next page.
137 * After client receives reply, several pages will be integrated into dir page
138 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
139 * lu_dirpage for this integrated page will be adjusted. See
140 * mdc_adjust_dirpages().
143 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
144 __u64 offset, struct ll_dir_chain *chain)
146 struct md_callback cb_op;
150 cb_op.md_blocking_ast = ll_md_blocking_ast;
151 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
158 void ll_release_page(struct inode *inode, struct page *page,
163 /* Always remove the page for striped dir, because the page is
164 * built from temporarily in LMV layer */
165 if (inode && ll_dir_striped(inode)) {
172 if (likely(page->mapping != NULL))
173 delete_from_page_cache(page);
179 #ifdef HAVE_DIR_CONTEXT
180 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
181 struct dir_context *ctx)
184 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
185 void *cookie, filldir_t filldir)
188 struct ll_sb_info *sbi = ll_i2sbi(inode);
190 bool is_api32 = ll_need_32bit_api(sbi);
191 bool is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
193 struct ll_dir_chain chain;
198 ll_dir_chain_init(&chain);
200 page = ll_get_dir_page(inode, op_data, pos, &chain);
202 while (rc == 0 && !done) {
203 struct lu_dirpage *dp;
204 struct lu_dirent *ent;
213 hash = MDS_DIR_END_OFF;
214 dp = page_address(page);
215 for (ent = lu_dirent_start(dp); ent != NULL && !done;
216 ent = lu_dirent_next(ent)) {
223 hash = le64_to_cpu(ent->lde_hash);
224 if (hash < pos) /* Skip until we find target hash */
227 namelen = le16_to_cpu(ent->lde_namelen);
228 if (namelen == 0) /* Skip dummy record */
231 if (is_api32 && is_hash64)
235 fid_le_to_cpu(&fid, &ent->lde_fid);
236 ino = cl_fid_build_ino(&fid, is_api32);
237 type = IFTODT(lu_dirent_type_get(ent));
238 /* For ll_nfs_get_name_filldir(), it will try to access
239 * 'ent' through 'lde_name', so the parameter 'name'
240 * for 'filldir()' must be part of the 'ent'. */
241 #ifdef HAVE_DIR_CONTEXT
243 done = !dir_emit(ctx, ent->lde_name, namelen, ino,
246 done = filldir(cookie, ent->lde_name, namelen, lhash,
253 ll_release_page(inode, page, false);
257 next = le64_to_cpu(dp->ldp_hash_end);
259 if (pos == MDS_DIR_END_OFF) {
261 * End of directory reached.
264 ll_release_page(inode, page, false);
267 * Normal case: continue to the next
270 ll_release_page(inode, page,
271 le32_to_cpu(dp->ldp_flags) &
274 page = ll_get_dir_page(inode, op_data, pos,
278 #ifdef HAVE_DIR_CONTEXT
283 ll_dir_chain_fini(&chain);
287 #ifdef HAVE_DIR_CONTEXT
288 static int ll_iterate(struct file *filp, struct dir_context *ctx)
290 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
293 struct inode *inode = file_inode(filp);
294 struct ll_file_data *lfd = filp->private_data;
295 struct ll_sb_info *sbi = ll_i2sbi(inode);
296 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
297 int api32 = ll_need_32bit_api(sbi);
298 struct md_op_data *op_data;
299 struct lu_fid pfid = { 0 };
300 ktime_t kstart = ktime_get();
311 "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
312 PFID(ll_inode2fid(inode)),
313 inode, (unsigned long)pos, i_size_read(inode), api32);
315 if (pos == MDS_DIR_END_OFF)
321 if (unlikely(ll_dir_striped(inode))) {
323 * This is only needed for striped dir to fill ..,
324 * see lmv_read_page()
326 if (file_dentry(filp)->d_parent != NULL &&
327 file_dentry(filp)->d_parent->d_inode != NULL) {
328 __u64 ibits = MDS_INODELOCK_LOOKUP;
329 struct inode *parent =
330 file_dentry(filp)->d_parent->d_inode;
332 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
333 pfid = *ll_inode2fid(parent);
336 /* If it can not find in cache, do lookup .. on the master
338 if (fid_is_zero(&pfid)) {
339 rc = ll_dir_get_parent_fid(inode, &pfid);
345 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
346 LUSTRE_OPC_ANY, inode);
348 GOTO(out, rc = PTR_ERR(op_data));
350 /* foreign dirs are browsed out of Lustre */
351 if (unlikely(op_data->op_mea1 != NULL &&
352 op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
353 ll_finish_md_op_data(op_data);
357 op_data->op_fid3 = pfid;
359 #ifdef HAVE_DIR_CONTEXT
361 rc = ll_dir_read(inode, &pos, op_data, ctx);
364 rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
369 if (pos == MDS_DIR_END_OFF) {
371 pos = LL_DIR_END_OFF_32BIT;
373 pos = LL_DIR_END_OFF;
378 #ifdef HAVE_DIR_CONTEXT
383 ll_finish_md_op_data(op_data);
387 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
388 ktime_us_delta(ktime_get(), kstart));
394 * Create striped directory with specified stripe(@lump)
396 * \param[in] dparent the parent of the directory.
397 * \param[in] lump the specified stripes.
398 * \param[in] dirname the name of the directory.
399 * \param[in] mode the specified mode of the directory.
401 * \retval =0 if striped directory is being created successfully.
402 * <0 if the creation is failed.
404 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
405 size_t len, const char *dirname, umode_t mode)
407 struct inode *parent = dparent->d_inode;
408 struct ptlrpc_request *request = NULL;
409 struct md_op_data *op_data;
410 struct ll_sb_info *sbi = ll_i2sbi(parent);
411 struct inode *inode = NULL;
412 struct dentry dentry = {
416 .len = strlen(dirname),
417 .hash = ll_full_name_hash(dparent, dirname,
421 bool encrypt = false;
425 if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
428 if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
430 "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
431 PFID(ll_inode2fid(parent)), parent, dirname,
432 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
434 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
437 "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
438 PFID(ll_inode2fid(parent)), parent, dirname,
439 lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
442 if (lump->lum_stripe_count > 1 &&
443 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
446 if (IS_DEADDIR(parent) &&
447 !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
450 if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
451 if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
452 LMV_HASH_TYPE_CRUSH) {
453 /* if server doesn't support 'crush' hash type,
454 * switch to fnv_1a_64.
456 lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
457 lump->lum_hash_type |= LMV_HASH_TYPE_FNV_1A_64;
458 } else if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
459 LMV_HASH_TYPE_UNKNOWN) {
460 /* from 2.14 MDT will choose default hash type if client
461 * doesn't set a valid one, while old server doesn't
464 lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
465 lump->lum_hash_type |= LMV_HASH_TYPE_DEFAULT;
469 if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
470 lustre_swab_lmv_user_md(lump);
472 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
473 mode &= ~current_umask();
474 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
475 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
476 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
479 RETURN(PTR_ERR(op_data));
481 if (IS_ENCRYPTED(parent) ||
482 unlikely(llcrypt_dummy_context_enabled(parent))) {
483 err = llcrypt_get_encryption_info(parent);
485 GOTO(out_op_data, err);
486 if (!llcrypt_has_encryption_key(parent))
487 GOTO(out_op_data, err = -ENOKEY);
491 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
492 /* selinux_dentry_init_security() uses dentry->d_parent and name
493 * to determine the security context for the file. So our fake
494 * dentry should be real enough for this purpose. */
495 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
496 &op_data->op_file_secctx_name,
497 &op_data->op_file_secctx,
498 &op_data->op_file_secctx_size);
500 GOTO(out_op_data, err);
503 op_data->op_cli_flags |= CLI_SET_MEA;
504 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
505 from_kuid(&init_user_ns, current_fsuid()),
506 from_kgid(&init_user_ns, current_fsgid()),
507 cfs_curproc_cap_pack(), 0, &request);
509 GOTO(out_request, err);
511 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
513 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
515 GOTO(out_inode, err);
517 dentry.d_inode = inode;
519 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
520 /* no need to protect selinux_inode_setsecurity() by
521 * inode_lock. Taking it would lead to a client deadlock
524 err = security_inode_notifysecctx(inode,
525 op_data->op_file_secctx,
526 op_data->op_file_secctx_size);
528 err = ll_inode_init_security(&dentry, inode, parent);
531 GOTO(out_inode, err);
534 err = llcrypt_inherit_context(parent, inode, NULL, false);
536 GOTO(out_inode, err);
543 ptlrpc_req_finished(request);
545 ll_finish_md_op_data(op_data);
550 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
553 struct ll_sb_info *sbi = ll_i2sbi(inode);
554 struct md_op_data *op_data;
555 struct ptlrpc_request *req = NULL;
561 switch (lump->lmm_magic) {
562 case LOV_USER_MAGIC_V1:
563 lum_size = sizeof(struct lov_user_md_v1);
565 case LOV_USER_MAGIC_V3:
566 lum_size = sizeof(struct lov_user_md_v3);
568 case LOV_USER_MAGIC_COMP_V1:
569 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
572 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
573 lustre_swab_lmv_user_md(
574 (struct lmv_user_md *)lump);
575 lum_size = sizeof(struct lmv_user_md);
577 case LOV_USER_MAGIC_SPECIFIC: {
578 struct lov_user_md_v3 *v3 =
579 (struct lov_user_md_v3 *)lump;
580 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
582 lum_size = lov_user_md_size(v3->lmm_stripe_count,
583 LOV_USER_MAGIC_SPECIFIC);
587 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
588 " %#08x != %#08x nor %#08x\n",
589 lump->lmm_magic, LOV_USER_MAGIC_V1,
595 * This is coming from userspace, so should be in
596 * local endian. But the MDS would like it in little
597 * endian, so we swab it before we send it.
599 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
600 le32_to_cpu(LOV_MAGIC_MAGIC))
601 lustre_swab_lov_user_md(lump, 0);
603 lum_size = sizeof(struct lov_user_md_v1);
606 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
607 LUSTRE_OPC_ANY, NULL);
609 RETURN(PTR_ERR(op_data));
611 /* swabbing is done in lov_setstripe() on server side */
612 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
613 ll_finish_md_op_data(op_data);
614 ptlrpc_req_finished(req);
621 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
623 struct ptlrpc_request **request, u64 valid,
624 enum get_default_layout_type type)
626 struct ll_sb_info *sbi = ll_i2sbi(inode);
627 struct mdt_body *body;
628 struct lov_mds_md *lmm = NULL;
629 struct ptlrpc_request *req = NULL;
631 struct md_op_data *op_data;
635 rc = ll_get_default_mdsize(sbi, &lmm_size);
639 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
640 0, lmm_size, LUSTRE_OPC_ANY,
643 RETURN(PTR_ERR(op_data));
645 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
647 if (type == GET_DEFAULT_LAYOUT_ROOT) {
648 lu_root_fid(&op_data->op_fid1);
649 fid = op_data->op_fid1;
651 fid = *ll_inode2fid(inode);
654 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
655 ll_finish_md_op_data(op_data);
657 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
662 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
663 LASSERT(body != NULL);
665 lmm_size = body->mbo_eadatasize;
667 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
669 GOTO(out, rc = -ENODATA);
672 lmm = req_capsule_server_sized_get(&req->rq_pill,
673 &RMF_MDT_MD, lmm_size);
674 LASSERT(lmm != NULL);
677 * This is coming from the MDS, so is probably in
678 * little endian. We convert it to host endian before
679 * passing it to userspace.
681 /* We don't swab objects for directories */
682 switch (le32_to_cpu(lmm->lmm_magic)) {
685 case LOV_MAGIC_COMP_V1:
686 case LOV_USER_MAGIC_SPECIFIC:
687 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
688 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
691 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
692 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
695 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
696 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
698 case LMV_MAGIC_FOREIGN: {
699 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
701 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
702 __swab32s(&lfm->lfm_magic);
703 __swab32s(&lfm->lfm_length);
704 __swab32s(&lfm->lfm_type);
705 __swab32s(&lfm->lfm_flags);
710 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
715 *plmm_size = lmm_size;
721 * This function will be used to get default LOV/LMV/Default LMV
722 * @valid will be used to indicate which stripe it will retrieve.
723 * If the directory does not have its own default layout, then the
724 * function will request the default layout from root FID.
725 * OBD_MD_MEA LMV stripe EA
726 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
727 * otherwise Default LOV EA.
728 * Each time, it can only retrieve 1 stripe EA
730 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
731 struct ptlrpc_request **request,
732 struct ptlrpc_request **root_request,
735 struct ptlrpc_request *req = NULL;
736 struct ptlrpc_request *root_req = NULL;
737 struct lov_mds_md *lmm = NULL;
742 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
744 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
745 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
746 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
748 GET_DEFAULT_LAYOUT_ROOT);
751 *plmm_size = lmm_size;
753 if (root_request != NULL)
754 *root_request = root_req;
760 * This function will be used to get default LOV/LMV/Default LMV
761 * @valid will be used to indicate which stripe it will retrieve
762 * OBD_MD_MEA LMV stripe EA
763 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
764 * otherwise Default LOV EA.
765 * Each time, it can only retrieve 1 stripe EA
767 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
768 struct ptlrpc_request **request, u64 valid)
770 struct ptlrpc_request *req = NULL;
771 struct lov_mds_md *lmm = NULL;
776 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
780 *plmm_size = lmm_size;
786 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
788 struct md_op_data *op_data;
793 OBD_ALLOC_PTR(op_data);
797 op_data->op_flags |= MF_GET_MDT_IDX;
798 op_data->op_fid1 = *fid;
799 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
800 mdt_index = op_data->op_mds;
801 OBD_FREE_PTR(op_data);
809 * Get MDT index for the inode.
811 int ll_get_mdt_idx(struct inode *inode)
813 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
817 * Generic handler to do any pre-copy work.
819 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
820 * first information for it that real work has started.
822 * Moreover, for a ARCHIVE request, it will sample the file data version and
823 * store it in \a copy.
825 * \return 0 on success.
827 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
829 struct ll_sb_info *sbi = ll_s2sbi(sb);
830 struct hsm_progress_kernel hpk;
835 /* Forge a hsm_progress based on data from copy. */
836 hpk.hpk_fid = copy->hc_hai.hai_fid;
837 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
838 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
839 hpk.hpk_extent.length = 0;
842 hpk.hpk_data_version = 0;
845 /* For archive request, we need to read the current file version. */
846 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
848 __u64 data_version = 0;
850 /* Get inode for this fid */
851 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
853 hpk.hpk_flags |= HP_FLAG_RETRY;
854 /* hpk_errval is >= 0 */
855 hpk.hpk_errval = -PTR_ERR(inode);
856 GOTO(progress, rc = PTR_ERR(inode));
859 /* Read current file data version */
860 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
863 CDEBUG(D_HSM, "Could not read file data version of "
864 DFID" (rc = %d). Archive request ("
865 "%#llx) could not be done.\n",
866 PFID(©->hc_hai.hai_fid), rc,
867 copy->hc_hai.hai_cookie);
868 hpk.hpk_flags |= HP_FLAG_RETRY;
869 /* hpk_errval must be >= 0 */
870 hpk.hpk_errval = -rc;
874 /* Store in the hsm_copy for later copytool use.
875 * Always modified even if no lsm. */
876 copy->hc_data_version = data_version;
880 /* On error, the request should be considered as completed */
881 if (hpk.hpk_errval > 0)
882 hpk.hpk_flags |= HP_FLAG_COMPLETED;
884 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
887 /* Return first error */
888 RETURN(rc != 0 ? rc : rc2);
892 * Generic handler to do any post-copy work.
894 * It will send the last hsm_progress update to coordinator to inform it
895 * that copy is finished and whether it was successful or not.
898 * - for ARCHIVE request, it will sample the file data version and compare it
899 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
900 * will be considered as failed.
901 * - for RESTORE request, it will sample the file data version and send it to
902 * coordinator which is useful if the file was imported as 'released'.
904 * \return 0 on success.
906 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
908 struct ll_sb_info *sbi = ll_s2sbi(sb);
909 struct hsm_progress_kernel hpk;
914 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
915 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
916 * initialized if copy_end was called with copy == NULL.
919 /* Forge a hsm_progress based on data from copy. */
920 hpk.hpk_fid = copy->hc_hai.hai_fid;
921 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
922 hpk.hpk_extent = copy->hc_hai.hai_extent;
923 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
924 hpk.hpk_errval = copy->hc_errval;
925 hpk.hpk_data_version = 0;
927 /* For archive request, we need to check the file data was not changed.
929 * For restore request, we need to send the file data version, this is
930 * useful when the file was created using hsm_import.
932 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
933 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
934 (copy->hc_errval == 0)) {
936 __u64 data_version = 0;
938 /* Get lsm for this fid */
939 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
941 hpk.hpk_flags |= HP_FLAG_RETRY;
942 /* hpk_errval must be >= 0 */
943 hpk.hpk_errval = -PTR_ERR(inode);
944 GOTO(progress, rc = PTR_ERR(inode));
947 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
950 CDEBUG(D_HSM, "Could not read file data version. "
951 "Request could not be confirmed.\n");
952 if (hpk.hpk_errval == 0)
953 hpk.hpk_errval = -rc;
957 /* Store in the hsm_copy for later copytool use.
958 * Always modified even if no lsm. */
959 hpk.hpk_data_version = data_version;
961 /* File could have been stripped during archiving, so we need
962 * to check anyway. */
963 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
964 (copy->hc_data_version != data_version)) {
965 CDEBUG(D_HSM, "File data version mismatched. "
966 "File content was changed during archiving. "
967 DFID", start:%#llx current:%#llx\n",
968 PFID(©->hc_hai.hai_fid),
969 copy->hc_data_version, data_version);
970 /* File was changed, send error to cdt. Do not ask for
971 * retry because if a file is modified frequently,
972 * the cdt will loop on retried archive requests.
973 * The policy engine will ask for a new archive later
974 * when the file will not be modified for some tunable
976 hpk.hpk_flags &= ~HP_FLAG_RETRY;
978 /* hpk_errval must be >= 0 */
979 hpk.hpk_errval = -rc;
986 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
989 /* Return first error */
990 RETURN(rc != 0 ? rc : rc2);
994 static int copy_and_ct_start(int cmd, struct obd_export *exp,
995 const struct lustre_kernelcomm __user *data)
997 struct lustre_kernelcomm *lk;
998 struct lustre_kernelcomm *tmp;
999 size_t size = sizeof(*lk);
1004 /* copy data from userspace to get numbers of archive_id */
1005 OBD_ALLOC(lk, size);
1009 if (copy_from_user(lk, data, size))
1010 GOTO(out_lk, rc = -EFAULT);
1012 if (lk->lk_flags & LK_FLG_STOP)
1015 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1016 __u32 archive_mask = lk->lk_data_count;
1019 /* old hsm agent to old MDS */
1020 if (!exp_connect_archive_id_array(exp))
1023 /* old hsm agent to new MDS */
1024 lk->lk_flags |= LK_FLG_DATANR;
1026 if (archive_mask == 0)
1029 count = hweight32(archive_mask);
1030 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1031 OBD_ALLOC(tmp, new_size);
1033 GOTO(out_lk, rc = -ENOMEM);
1035 memcpy(tmp, lk, size);
1036 tmp->lk_data_count = count;
1042 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1043 if (BIT(i) & archive_mask) {
1044 lk->lk_data[count] = i + 1;
1051 /* new hsm agent to new mds */
1052 if (lk->lk_data_count > 0) {
1053 new_size = offsetof(struct lustre_kernelcomm,
1054 lk_data[lk->lk_data_count]);
1055 OBD_ALLOC(tmp, new_size);
1057 GOTO(out_lk, rc = -ENOMEM);
1063 if (copy_from_user(lk, data, size))
1064 GOTO(out_lk, rc = -EFAULT);
1067 /* new hsm agent to old MDS */
1068 if (!exp_connect_archive_id_array(exp)) {
1071 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1072 GOTO(out_lk, rc = -EINVAL);
1074 for (i = 0; i < lk->lk_data_count; i++) {
1075 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1077 CERROR("%s: archive id %d requested but only "
1078 "[0 - %zu] supported: rc = %d\n",
1079 exp->exp_obd->obd_name, lk->lk_data[i],
1080 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1084 if (lk->lk_data[i] == 0) {
1089 archives |= (1 << (lk->lk_data[i] - 1));
1091 lk->lk_flags &= ~LK_FLG_DATANR;
1092 lk->lk_data_count = archives;
1095 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1101 static int check_owner(int type, int id)
1105 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1109 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1118 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1120 int cmd = qctl->qc_cmd;
1121 int type = qctl->qc_type;
1122 int id = qctl->qc_id;
1123 int valid = qctl->qc_valid;
1130 case LUSTRE_Q_SETDEFAULT:
1131 case LUSTRE_Q_SETQUOTAPOOL:
1132 case LUSTRE_Q_SETINFOPOOL:
1133 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1137 case LUSTRE_Q_GETDEFAULT:
1138 case LUSTRE_Q_GETQUOTAPOOL:
1139 if (check_owner(type, id) &&
1140 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1144 case LUSTRE_Q_GETINFOPOOL:
1147 CERROR("unsupported quotactl op: %#x\n", cmd);
1151 if (valid != QC_GENERAL) {
1152 if (cmd == Q_GETINFO)
1153 qctl->qc_cmd = Q_GETOINFO;
1154 else if (cmd == Q_GETQUOTA ||
1155 cmd == LUSTRE_Q_GETQUOTAPOOL)
1156 qctl->qc_cmd = Q_GETOQUOTA;
1162 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1163 sizeof(*qctl), qctl, NULL);
1166 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1167 sizeof(*qctl), qctl, NULL);
1170 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1171 sizeof(*qctl), qctl, NULL);
1173 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1175 sizeof(*qctl), qctl, NULL);
1187 struct obd_quotactl *oqctl;
1188 int oqctl_len = sizeof(*oqctl);
1190 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1191 oqctl_len += LOV_MAXPOOLNAME + 1;
1193 OBD_ALLOC(oqctl, oqctl_len);
1197 QCTL_COPY(oqctl, qctl);
1198 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1200 OBD_FREE(oqctl, oqctl_len);
1203 /* If QIF_SPACE is not set, client should collect the
1204 * space usage from OSSs by itself */
1205 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1206 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1207 !oqctl->qc_dqblk.dqb_curspace) {
1208 struct obd_quotactl *oqctl_tmp;
1210 OBD_ALLOC_PTR(oqctl_tmp);
1211 if (oqctl_tmp == NULL)
1212 GOTO(out, rc = -ENOMEM);
1214 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1215 oqctl_tmp->qc_id = oqctl->qc_id;
1216 oqctl_tmp->qc_type = oqctl->qc_type;
1218 /* collect space usage from OSTs */
1219 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1220 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1221 if (!rc || rc == -EREMOTEIO) {
1222 oqctl->qc_dqblk.dqb_curspace =
1223 oqctl_tmp->qc_dqblk.dqb_curspace;
1224 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1227 /* collect space & inode usage from MDTs */
1228 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1229 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1230 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1231 if (!rc || rc == -EREMOTEIO) {
1232 oqctl->qc_dqblk.dqb_curspace +=
1233 oqctl_tmp->qc_dqblk.dqb_curspace;
1234 oqctl->qc_dqblk.dqb_curinodes =
1235 oqctl_tmp->qc_dqblk.dqb_curinodes;
1236 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1238 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1241 OBD_FREE_PTR(oqctl_tmp);
1244 QCTL_COPY(qctl, oqctl);
1245 OBD_FREE(oqctl, oqctl_len);
1251 int ll_rmfid(struct file *file, void __user *arg)
1253 const struct fid_array __user *ufa = arg;
1254 struct fid_array *lfa = NULL;
1257 int i, rc, *rcs = NULL;
1260 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1261 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1263 /* Only need to get the buflen */
1264 if (get_user(nr, &ufa->fa_nr))
1266 /* DoS protection */
1267 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1270 size = offsetof(struct fid_array, fa_fids[nr]);
1271 OBD_ALLOC(lfa, size);
1274 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1276 GOTO(free_lfa, rc = -ENOMEM);
1278 if (copy_from_user(lfa, arg, size))
1279 GOTO(free_rcs, rc = -EFAULT);
1281 /* Call mdc_iocontrol */
1282 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1284 for (i = 0; i < nr; i++)
1286 lfa->fa_fids[i].f_ver = rcs[i];
1287 if (copy_to_user(arg, lfa, size))
1292 OBD_FREE_PTR_ARRAY(rcs, nr);
1294 OBD_FREE(lfa, size);
1299 /* This function tries to get a single name component,
1300 * to send to the server. No actual path traversal involved,
1301 * so we limit to NAME_MAX */
1302 static char *ll_getname(const char __user *filename)
1307 OBD_ALLOC(tmp, NAME_MAX + 1);
1310 return ERR_PTR(-ENOMEM);
1312 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1315 else if (len > NAME_MAX)
1316 ret = -ENAMETOOLONG;
1319 OBD_FREE(tmp, NAME_MAX + 1);
1325 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1327 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1329 struct dentry *dentry = file_dentry(file);
1330 struct inode *inode = file_inode(file);
1331 struct ll_sb_info *sbi = ll_i2sbi(inode);
1332 struct obd_ioctl_data *data;
1336 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1337 PFID(ll_inode2fid(inode)), inode, cmd);
1339 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1340 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1343 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1345 case FS_IOC_GETFLAGS:
1346 case FS_IOC_SETFLAGS:
1347 RETURN(ll_iocontrol(inode, file, cmd, arg));
1348 case FSFILT_IOC_GETVERSION:
1349 case FS_IOC_GETVERSION:
1350 RETURN(put_user(inode->i_generation, (int __user *)arg));
1351 /* We need to special case any other ioctls we want to handle,
1352 * to send them to the MDS/OST as appropriate and to properly
1353 * network encode the arg field. */
1354 case FS_IOC_SETVERSION:
1357 case LL_IOC_GET_MDTIDX: {
1360 mdtidx = ll_get_mdt_idx(inode);
1364 if (put_user((int)mdtidx, (int __user *)arg))
1369 case IOC_MDC_LOOKUP: {
1370 int namelen, len = 0;
1374 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1379 filename = data->ioc_inlbuf1;
1380 namelen = strlen(filename);
1382 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1383 GOTO(out_free, rc = -EINVAL);
1386 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1388 CERROR("%s: lookup %.*s failed: rc = %d\n",
1389 sbi->ll_fsname, namelen, filename, rc);
1393 OBD_FREE_LARGE(buf, len);
1396 case LL_IOC_LMV_SETSTRIPE: {
1397 struct lmv_user_md *lum;
1406 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1411 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1412 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1413 GOTO(lmv_out_free, rc = -EINVAL);
1415 filename = data->ioc_inlbuf1;
1416 namelen = data->ioc_inllen1;
1419 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1420 GOTO(lmv_out_free, rc = -EINVAL);
1422 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1423 lumlen = data->ioc_inllen2;
1425 if (!lmv_user_magic_supported(lum->lum_magic)) {
1426 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1427 lum->lum_magic, -EINVAL);
1428 GOTO(lmv_out_free, rc = -EINVAL);
1431 if ((lum->lum_magic == LMV_USER_MAGIC ||
1432 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1433 lumlen < sizeof(*lum)) {
1434 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1435 filename, lumlen, lum->lum_magic, -EINVAL);
1436 GOTO(lmv_out_free, rc = -EINVAL);
1439 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1440 lumlen < sizeof(struct lmv_foreign_md)) {
1441 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1442 filename, lum->lum_magic, lumlen, -EFAULT);
1443 GOTO(lmv_out_free, rc = -EINVAL);
1446 mode = data->ioc_type;
1447 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1449 OBD_FREE_LARGE(buf, len);
1453 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1454 struct lmv_user_md lum;
1455 struct lmv_user_md __user *ulump =
1456 (struct lmv_user_md __user *)arg;
1459 if (copy_from_user(&lum, ulump, sizeof(lum)))
1462 if (lum.lum_magic != LMV_USER_MAGIC)
1465 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1469 case LL_IOC_LOV_SETSTRIPE_NEW:
1470 case LL_IOC_LOV_SETSTRIPE: {
1471 struct lov_user_md_v3 *lumv3 = NULL;
1472 struct lov_user_md_v1 lumv1;
1473 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1474 struct lov_user_md_v1 __user *lumv1p =
1475 (struct lov_user_md_v1 __user *)arg;
1476 struct lov_user_md_v3 __user *lumv3p =
1477 (struct lov_user_md_v3 __user *)arg;
1480 int set_default = 0;
1482 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1483 sizeof(struct lov_comp_md_v1));
1484 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1485 /* first try with v1 which is smaller than v3 */
1486 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1489 if (inode->i_sb->s_root == file_dentry(file))
1492 switch (lumv1.lmm_magic) {
1493 case LOV_USER_MAGIC_V3:
1494 case LOV_USER_MAGIC_SPECIFIC:
1495 lum_size = ll_lov_user_md_size(&lumv1);
1498 OBD_ALLOC(lumv3, lum_size);
1501 if (copy_from_user(lumv3, lumv3p, lum_size))
1502 GOTO(out, rc = -EFAULT);
1503 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1505 case LOV_USER_MAGIC_V1:
1508 GOTO(out, rc = -ENOTSUPP);
1511 /* in v1 and v3 cases lumv1 points to data */
1512 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1515 OBD_FREE(lumv3, lum_size);
1518 case LL_IOC_LMV_GETSTRIPE: {
1519 struct lmv_user_md __user *ulmv =
1520 (struct lmv_user_md __user *)arg;
1521 struct lmv_user_md lum;
1522 struct ptlrpc_request *request = NULL;
1523 struct ptlrpc_request *root_request = NULL;
1524 union lmv_mds_md *lmm = NULL;
1527 struct lmv_user_md *tmp = NULL;
1531 int max_stripe_count;
1535 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1538 max_stripe_count = lum.lum_stripe_count;
1539 /* lum_magic will indicate which stripe the ioctl will like
1540 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1541 * is for default LMV stripe */
1542 if (lum.lum_magic == LMV_MAGIC_V1)
1543 valid |= OBD_MD_MEA;
1544 else if (lum.lum_magic == LMV_USER_MAGIC)
1545 valid |= OBD_MD_DEFAULT_MEA;
1549 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1550 &request, &root_request, valid);
1552 GOTO(finish_req, rc);
1554 /* Get default LMV EA */
1555 if (lum.lum_magic == LMV_USER_MAGIC) {
1556 if (lmmsize > sizeof(*ulmv))
1557 GOTO(finish_req, rc = -EINVAL);
1559 if (copy_to_user(ulmv, lmm, lmmsize))
1560 GOTO(finish_req, rc = -EFAULT);
1562 GOTO(finish_req, rc);
1565 /* if foreign LMV case, fake stripes number */
1566 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1567 struct lmv_foreign_md *lfm;
1569 lfm = (struct lmv_foreign_md *)lmm;
1570 if (lfm->lfm_length < XATTR_SIZE_MAX -
1571 offsetof(typeof(*lfm), lfm_value)) {
1572 __u32 size = lfm->lfm_length +
1573 offsetof(typeof(*lfm), lfm_value);
1575 stripe_count = lmv_foreign_to_md_stripes(size);
1577 CERROR("invalid %d foreign size returned\n",
1582 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1584 if (max_stripe_count < stripe_count) {
1585 lum.lum_stripe_count = stripe_count;
1586 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1587 GOTO(finish_req, rc = -EFAULT);
1588 GOTO(finish_req, rc = -E2BIG);
1591 /* enough room on user side and foreign case */
1592 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1593 struct lmv_foreign_md *lfm;
1596 lfm = (struct lmv_foreign_md *)lmm;
1597 size = lfm->lfm_length +
1598 offsetof(struct lmv_foreign_md, lfm_value);
1599 if (copy_to_user(ulmv, lfm, size))
1600 GOTO(finish_req, rc = -EFAULT);
1601 GOTO(finish_req, rc);
1604 lum_size = lmv_user_md_size(stripe_count,
1605 LMV_USER_MAGIC_SPECIFIC);
1606 OBD_ALLOC(tmp, lum_size);
1608 GOTO(finish_req, rc = -ENOMEM);
1610 mdt_index = ll_get_mdt_idx(inode);
1612 GOTO(out_tmp, rc = -ENOMEM);
1614 tmp->lum_magic = LMV_MAGIC_V1;
1615 tmp->lum_stripe_count = 0;
1616 tmp->lum_stripe_offset = mdt_index;
1617 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1618 for (i = 0; i < stripe_count; i++) {
1621 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1622 if (fid_is_sane(&fid)) {
1623 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1625 GOTO(out_tmp, rc = mdt_index);
1627 tmp->lum_objects[i].lum_mds = mdt_index;
1628 tmp->lum_objects[i].lum_fid = fid;
1631 tmp->lum_stripe_count++;
1634 if (copy_to_user(ulmv, tmp, lum_size))
1635 GOTO(out_tmp, rc = -EFAULT);
1637 OBD_FREE(tmp, lum_size);
1639 ptlrpc_req_finished(request);
1640 ptlrpc_req_finished(root_request);
1644 case LL_IOC_REMOVE_ENTRY: {
1645 char *filename = NULL;
1649 /* Here is a little hack to avoid sending REINT_RMENTRY to
1650 * unsupported server, which might crash the server(LU-2730),
1651 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1652 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1653 * server will support REINT_RMENTRY XXX*/
1654 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1657 filename = ll_getname((const char __user *)arg);
1658 if (IS_ERR(filename))
1659 RETURN(PTR_ERR(filename));
1661 namelen = strlen(filename);
1663 GOTO(out_rmdir, rc = -EINVAL);
1665 rc = ll_rmdir_entry(inode, filename, namelen);
1668 ll_putname(filename);
1672 RETURN(ll_rmfid(file, (void __user *)arg));
1673 case LL_IOC_LOV_SWAP_LAYOUTS:
1675 case IOC_OBD_STATFS:
1676 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1677 case LL_IOC_LOV_GETSTRIPE:
1678 case LL_IOC_LOV_GETSTRIPE_NEW:
1679 case LL_IOC_MDC_GETINFO:
1680 case LL_IOC_MDC_GETINFO_OLD:
1681 case IOC_MDC_GETFILEINFO:
1682 case IOC_MDC_GETFILEINFO_OLD:
1683 case IOC_MDC_GETFILESTRIPE: {
1684 struct ptlrpc_request *request = NULL;
1685 struct ptlrpc_request *root_request = NULL;
1686 struct lov_user_md __user *lump;
1687 struct lov_mds_md *lmm = NULL;
1688 struct mdt_body *body;
1689 char *filename = NULL;
1690 lstat_t __user *statp = NULL;
1691 lstatx_t __user *stxp = NULL;
1692 __u64 __user *flagsp = NULL;
1693 __u32 __user *lmmsizep = NULL;
1694 struct lu_fid __user *fidp = NULL;
1697 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1698 cmd == IOC_MDC_GETFILEINFO ||
1699 cmd == IOC_MDC_GETFILESTRIPE) {
1700 filename = ll_getname((const char __user *)arg);
1701 if (IS_ERR(filename))
1702 RETURN(PTR_ERR(filename));
1704 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1705 &lmmsize, &request);
1707 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1713 body = req_capsule_server_get(&request->rq_pill,
1715 LASSERT(body != NULL);
1720 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1721 cmd == LL_IOC_MDC_GETINFO ||
1722 cmd == IOC_MDC_GETFILEINFO_OLD ||
1723 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1731 if (cmd == IOC_MDC_GETFILESTRIPE ||
1732 cmd == LL_IOC_LOV_GETSTRIPE ||
1733 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1734 lump = (struct lov_user_md __user *)arg;
1735 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1736 cmd == LL_IOC_MDC_GETINFO_OLD){
1737 struct lov_user_mds_data_v1 __user *lmdp;
1739 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1740 statp = &lmdp->lmd_st;
1741 lump = &lmdp->lmd_lmm;
1743 struct lov_user_mds_data __user *lmdp;
1745 lmdp = (struct lov_user_mds_data __user *)arg;
1746 fidp = &lmdp->lmd_fid;
1747 stxp = &lmdp->lmd_stx;
1748 flagsp = &lmdp->lmd_flags;
1749 lmmsizep = &lmdp->lmd_lmmsize;
1750 lump = &lmdp->lmd_lmm;
1754 /* If the file has no striping then zero out *lump so
1755 * that the caller isn't confused by garbage. */
1756 if (clear_user(lump, sizeof(*lump)))
1757 GOTO(out_req, rc = -EFAULT);
1758 } else if (copy_to_user(lump, lmm, lmmsize)) {
1759 if (copy_to_user(lump, lmm, sizeof(*lump)))
1760 GOTO(out_req, rc = -EFAULT);
1764 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1765 cmd == LL_IOC_MDC_GETINFO_OLD) {
1768 st.st_dev = inode->i_sb->s_dev;
1769 st.st_mode = body->mbo_mode;
1770 st.st_nlink = body->mbo_nlink;
1771 st.st_uid = body->mbo_uid;
1772 st.st_gid = body->mbo_gid;
1773 st.st_rdev = body->mbo_rdev;
1774 st.st_size = body->mbo_size;
1775 st.st_blksize = PAGE_SIZE;
1776 st.st_blocks = body->mbo_blocks;
1777 st.st_atime = body->mbo_atime;
1778 st.st_mtime = body->mbo_mtime;
1779 st.st_ctime = body->mbo_ctime;
1780 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1784 if (copy_to_user(statp, &st, sizeof(st)))
1785 GOTO(out_req, rc = -EFAULT);
1786 } else if (cmd == IOC_MDC_GETFILEINFO ||
1787 cmd == LL_IOC_MDC_GETINFO) {
1788 lstatx_t stx = { 0 };
1789 __u64 valid = body->mbo_valid;
1791 stx.stx_blksize = PAGE_SIZE;
1792 stx.stx_nlink = body->mbo_nlink;
1793 stx.stx_uid = body->mbo_uid;
1794 stx.stx_gid = body->mbo_gid;
1795 stx.stx_mode = body->mbo_mode;
1796 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1799 stx.stx_size = body->mbo_size;
1800 stx.stx_blocks = body->mbo_blocks;
1801 stx.stx_atime.tv_sec = body->mbo_atime;
1802 stx.stx_ctime.tv_sec = body->mbo_ctime;
1803 stx.stx_mtime.tv_sec = body->mbo_mtime;
1804 stx.stx_btime.tv_sec = body->mbo_btime;
1805 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1806 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1807 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1808 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1809 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1812 * For a striped directory, the size and blocks returned
1813 * from MDT is not correct.
1814 * The size and blocks are aggregated by client across
1816 * Thus for a striped directory, do not return the valid
1817 * FLSIZE and FLBLOCKS flags to the caller.
1818 * However, this whould be better decided by the MDS
1819 * instead of the client.
1821 if (cmd == LL_IOC_MDC_GETINFO &&
1822 ll_i2info(inode)->lli_lsm_md != NULL)
1823 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1825 if (flagsp && copy_to_user(flagsp, &valid,
1827 GOTO(out_req, rc = -EFAULT);
1829 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1831 GOTO(out_req, rc = -EFAULT);
1833 if (!(valid & OBD_MD_FLSIZE))
1834 stx.stx_mask &= ~STATX_SIZE;
1835 if (!(valid & OBD_MD_FLBLOCKS))
1836 stx.stx_mask &= ~STATX_BLOCKS;
1838 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1839 GOTO(out_req, rc = -EFAULT);
1841 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1843 GOTO(out_req, rc = -EFAULT);
1848 ptlrpc_req_finished(request);
1849 ptlrpc_req_finished(root_request);
1851 ll_putname(filename);
1854 case OBD_IOC_QUOTACTL: {
1855 struct if_quotactl *qctl;
1856 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1858 OBD_ALLOC(qctl, qctl_len);
1862 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1863 GOTO(out_quotactl, rc = -EFAULT);
1865 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
1866 char __user *from = (char __user *)arg +
1867 offsetof(typeof(*qctl), qc_poolname);
1868 if (copy_from_user(qctl->qc_poolname, from,
1869 LOV_MAXPOOLNAME + 1))
1870 GOTO(out_quotactl, rc = -EFAULT);
1873 rc = quotactl_ioctl(sbi, qctl);
1875 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1879 OBD_FREE(qctl, qctl_len);
1882 case OBD_IOC_GETDTNAME:
1883 case OBD_IOC_GETMDNAME:
1884 RETURN(ll_get_obd_name(inode, cmd, arg));
1885 case LL_IOC_FLUSHCTX:
1886 RETURN(ll_flush_ctx(inode));
1887 case LL_IOC_GETOBDCOUNT: {
1889 struct obd_export *exp;
1891 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1894 /* get ost count when count is zero, get mdt count otherwise */
1895 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1896 vallen = sizeof(count);
1897 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1898 KEY_TGT_COUNT, &vallen, &count);
1900 CERROR("get target count failed: %d\n", rc);
1904 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1909 case LL_IOC_PATH2FID:
1910 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1911 sizeof(struct lu_fid)))
1914 case LL_IOC_GET_CONNECT_FLAGS: {
1915 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1916 (void __user *)arg));
1918 case OBD_IOC_FID2PATH:
1919 RETURN(ll_fid2path(inode, (void __user *)arg));
1920 case LL_IOC_GETPARENT:
1921 RETURN(ll_getparent(file, (void __user *)arg));
1922 case LL_IOC_FID2MDTIDX: {
1923 struct obd_export *exp = ll_i2mdexp(inode);
1927 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1931 /* Call mdc_iocontrol */
1932 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1933 (__u32 __user *)&index);
1939 case LL_IOC_HSM_REQUEST: {
1940 struct hsm_user_request *hur;
1947 /* We don't know the true size yet; copy the fixed-size part */
1948 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1953 /* Compute the whole struct size */
1954 totalsize = hur_len(hur);
1959 /* Final size will be more than double totalsize */
1960 if (totalsize >= MDS_MAXREQSIZE / 3)
1963 OBD_ALLOC_LARGE(hur, totalsize);
1967 /* Copy the whole struct */
1968 if (copy_from_user(hur, (void __user *)arg, totalsize))
1969 GOTO(out_hur, rc = -EFAULT);
1971 if (hur->hur_request.hr_action == HUA_RELEASE) {
1972 const struct lu_fid *fid;
1976 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1977 fid = &hur->hur_user_item[i].hui_fid;
1978 f = search_inode_for_lustre(inode->i_sb, fid);
1984 rc = ll_hsm_release(f);
1990 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1995 OBD_FREE_LARGE(hur, totalsize);
1999 case LL_IOC_HSM_PROGRESS: {
2000 struct hsm_progress_kernel hpk;
2001 struct hsm_progress hp;
2003 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2006 hpk.hpk_fid = hp.hp_fid;
2007 hpk.hpk_cookie = hp.hp_cookie;
2008 hpk.hpk_extent = hp.hp_extent;
2009 hpk.hpk_flags = hp.hp_flags;
2010 hpk.hpk_errval = hp.hp_errval;
2011 hpk.hpk_data_version = 0;
2013 /* File may not exist in Lustre; all progress
2014 * reported to Lustre root */
2015 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2019 case LL_IOC_HSM_CT_START:
2020 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2023 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2024 (struct lustre_kernelcomm __user *)arg);
2027 case LL_IOC_HSM_COPY_START: {
2028 struct hsm_copy *copy;
2031 OBD_ALLOC_PTR(copy);
2034 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2039 rc = ll_ioc_copy_start(inode->i_sb, copy);
2040 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2046 case LL_IOC_HSM_COPY_END: {
2047 struct hsm_copy *copy;
2050 OBD_ALLOC_PTR(copy);
2053 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2058 rc = ll_ioc_copy_end(inode->i_sb, copy);
2059 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2065 case LL_IOC_MIGRATE: {
2066 struct lmv_user_md *lum;
2073 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2077 data = (struct obd_ioctl_data *)buf;
2078 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2079 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2080 GOTO(migrate_free, rc = -EINVAL);
2082 filename = data->ioc_inlbuf1;
2083 namelen = data->ioc_inllen1;
2085 if (namelen < 1 || namelen != strlen(filename) + 1) {
2086 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2087 GOTO(migrate_free, rc = -EINVAL);
2090 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2091 if (lum->lum_magic != LMV_USER_MAGIC &&
2092 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2094 CERROR("%s: wrong lum magic %x: rc = %d\n",
2095 filename, lum->lum_magic, rc);
2096 GOTO(migrate_free, rc);
2099 rc = ll_migrate(inode, file, lum, filename);
2101 OBD_FREE_LARGE(buf, len);
2105 case FS_IOC_FSGETXATTR:
2106 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2107 case FS_IOC_FSSETXATTR:
2108 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2109 case LL_IOC_PCC_DETACH_BY_FID: {
2110 struct lu_pcc_detach_fid *detach;
2112 struct inode *inode2;
2115 OBD_ALLOC_PTR(detach);
2119 if (copy_from_user(detach,
2120 (const struct lu_pcc_detach_fid __user *)arg,
2122 GOTO(out_detach, rc = -EFAULT);
2124 fid = &detach->pccd_fid;
2125 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2126 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2128 /* Target inode is not in inode cache, and PCC file
2129 * has aleady released, return immdiately.
2131 GOTO(out_detach, rc = 0);
2133 if (!S_ISREG(inode2->i_mode))
2134 GOTO(out_iput, rc = -EINVAL);
2136 if (!inode_owner_or_capable(inode2))
2137 GOTO(out_iput, rc = -EPERM);
2139 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2143 OBD_FREE_PTR(detach);
2146 #ifdef HAVE_LUSTRE_CRYPTO
2147 case LL_IOC_SET_ENCRYPTION_POLICY:
2148 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2150 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2151 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2152 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2154 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2155 case LL_IOC_ADD_ENCRYPTION_KEY:
2156 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2158 return llcrypt_ioctl_add_key(file, (void __user *)arg);
2159 case LL_IOC_REMOVE_ENCRYPTION_KEY:
2160 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2162 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2163 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2164 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2166 return llcrypt_ioctl_remove_key_all_users(file,
2167 (void __user *)arg);
2168 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2169 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2171 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2174 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2175 (void __user *)arg));
2179 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2181 struct inode *inode = file->f_mapping->host;
2182 struct ll_file_data *fd = file->private_data;
2183 struct ll_sb_info *sbi = ll_i2sbi(inode);
2184 int api32 = ll_need_32bit_api(sbi);
2185 loff_t ret = -EINVAL;
2193 offset += file->f_pos;
2199 offset += LL_DIR_END_OFF_32BIT;
2201 offset += LL_DIR_END_OFF;
2208 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2209 (!api32 && offset <= LL_DIR_END_OFF))) {
2210 if (offset != file->f_pos) {
2211 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2212 (!api32 && offset == LL_DIR_END_OFF))
2213 fd->lfd_pos = MDS_DIR_END_OFF;
2214 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2215 fd->lfd_pos = offset << 32;
2217 fd->lfd_pos = offset;
2218 file->f_pos = offset;
2219 file->f_version = 0;
2226 inode_unlock(inode);
2230 static int ll_dir_open(struct inode *inode, struct file *file)
2233 RETURN(ll_file_open(inode, file));
2236 static int ll_dir_release(struct inode *inode, struct file *file)
2239 RETURN(ll_file_release(inode, file));
2242 const struct file_operations ll_dir_operations = {
2243 .llseek = ll_dir_seek,
2244 .open = ll_dir_open,
2245 .release = ll_dir_release,
2246 .read = generic_read_dir,
2247 #ifdef HAVE_DIR_CONTEXT
2248 .iterate_shared = ll_iterate,
2250 .readdir = ll_readdir,
2252 .unlocked_ioctl = ll_dir_ioctl,