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 (ll_sbi_has_encrypt(sbi) &&
482 (IS_ENCRYPTED(parent) ||
483 unlikely(llcrypt_dummy_context_enabled(parent)))) {
484 err = llcrypt_get_encryption_info(parent);
486 GOTO(out_op_data, err);
487 if (!llcrypt_has_encryption_key(parent))
488 GOTO(out_op_data, err = -ENOKEY);
492 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
493 /* selinux_dentry_init_security() uses dentry->d_parent and name
494 * to determine the security context for the file. So our fake
495 * dentry should be real enough for this purpose. */
496 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
497 &op_data->op_file_secctx_name,
498 &op_data->op_file_secctx,
499 &op_data->op_file_secctx_size);
501 GOTO(out_op_data, err);
505 err = llcrypt_inherit_context(parent, NULL, op_data, false);
507 GOTO(out_op_data, err);
510 op_data->op_cli_flags |= CLI_SET_MEA;
511 err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
512 from_kuid(&init_user_ns, current_fsuid()),
513 from_kgid(&init_user_ns, current_fsgid()),
514 cfs_curproc_cap_pack(), 0, &request);
516 GOTO(out_request, err);
518 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
520 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
522 GOTO(out_inode, err);
524 dentry.d_inode = inode;
526 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
527 /* no need to protect selinux_inode_setsecurity() by
528 * inode_lock. Taking it would lead to a client deadlock
531 err = security_inode_notifysecctx(inode,
532 op_data->op_file_secctx,
533 op_data->op_file_secctx_size);
535 err = ll_inode_init_security(&dentry, inode, parent);
538 GOTO(out_inode, err);
541 err = ll_set_encflags(inode, op_data->op_file_encctx,
542 op_data->op_file_encctx_size, false);
544 GOTO(out_inode, err);
551 ptlrpc_req_finished(request);
553 ll_finish_md_op_data(op_data);
558 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
561 struct ll_sb_info *sbi = ll_i2sbi(inode);
562 struct md_op_data *op_data;
563 struct ptlrpc_request *req = NULL;
569 switch (lump->lmm_magic) {
570 case LOV_USER_MAGIC_V1:
571 lum_size = sizeof(struct lov_user_md_v1);
573 case LOV_USER_MAGIC_V3:
574 lum_size = sizeof(struct lov_user_md_v3);
576 case LOV_USER_MAGIC_COMP_V1:
577 lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
580 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
581 lustre_swab_lmv_user_md(
582 (struct lmv_user_md *)lump);
583 lum_size = sizeof(struct lmv_user_md);
585 case LOV_USER_MAGIC_SPECIFIC: {
586 struct lov_user_md_v3 *v3 =
587 (struct lov_user_md_v3 *)lump;
588 if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
590 lum_size = lov_user_md_size(v3->lmm_stripe_count,
591 LOV_USER_MAGIC_SPECIFIC);
595 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
596 " %#08x != %#08x nor %#08x\n",
597 lump->lmm_magic, LOV_USER_MAGIC_V1,
603 * This is coming from userspace, so should be in
604 * local endian. But the MDS would like it in little
605 * endian, so we swab it before we send it.
607 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
608 le32_to_cpu(LOV_MAGIC_MAGIC))
609 lustre_swab_lov_user_md(lump, 0);
611 lum_size = sizeof(struct lov_user_md_v1);
614 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
615 LUSTRE_OPC_ANY, NULL);
617 RETURN(PTR_ERR(op_data));
619 /* swabbing is done in lov_setstripe() on server side */
620 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
621 ll_finish_md_op_data(op_data);
622 ptlrpc_req_finished(req);
629 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
631 struct ptlrpc_request **request, u64 valid,
632 enum get_default_layout_type type)
634 struct ll_sb_info *sbi = ll_i2sbi(inode);
635 struct mdt_body *body;
636 struct lov_mds_md *lmm = NULL;
637 struct ptlrpc_request *req = NULL;
639 struct md_op_data *op_data;
643 rc = ll_get_default_mdsize(sbi, &lmm_size);
647 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
648 0, lmm_size, LUSTRE_OPC_ANY,
651 RETURN(PTR_ERR(op_data));
653 op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
655 if (type == GET_DEFAULT_LAYOUT_ROOT) {
656 lu_root_fid(&op_data->op_fid1);
657 fid = op_data->op_fid1;
659 fid = *ll_inode2fid(inode);
662 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
663 ll_finish_md_op_data(op_data);
665 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
670 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
671 LASSERT(body != NULL);
673 lmm_size = body->mbo_eadatasize;
675 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
677 GOTO(out, rc = -ENODATA);
680 lmm = req_capsule_server_sized_get(&req->rq_pill,
681 &RMF_MDT_MD, lmm_size);
682 LASSERT(lmm != NULL);
685 * This is coming from the MDS, so is probably in
686 * little endian. We convert it to host endian before
687 * passing it to userspace.
689 /* We don't swab objects for directories */
690 switch (le32_to_cpu(lmm->lmm_magic)) {
693 case LOV_MAGIC_COMP_V1:
694 case LOV_USER_MAGIC_SPECIFIC:
695 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
696 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
699 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
700 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
703 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
704 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
706 case LMV_MAGIC_FOREIGN: {
707 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
709 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
710 __swab32s(&lfm->lfm_magic);
711 __swab32s(&lfm->lfm_length);
712 __swab32s(&lfm->lfm_type);
713 __swab32s(&lfm->lfm_flags);
718 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
723 *plmm_size = lmm_size;
729 * This function will be used to get default LOV/LMV/Default LMV
730 * @valid will be used to indicate which stripe it will retrieve.
731 * If the directory does not have its own default layout, then the
732 * function will request the default layout from root FID.
733 * OBD_MD_MEA LMV stripe EA
734 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
735 * otherwise Default LOV EA.
736 * Each time, it can only retrieve 1 stripe EA
738 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
739 struct ptlrpc_request **request,
740 struct ptlrpc_request **root_request,
743 struct ptlrpc_request *req = NULL;
744 struct ptlrpc_request *root_req = NULL;
745 struct lov_mds_md *lmm = NULL;
750 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
752 if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
753 !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
754 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
756 GET_DEFAULT_LAYOUT_ROOT);
759 *plmm_size = lmm_size;
761 if (root_request != NULL)
762 *root_request = root_req;
768 * This function will be used to get default LOV/LMV/Default LMV
769 * @valid will be used to indicate which stripe it will retrieve
770 * OBD_MD_MEA LMV stripe EA
771 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
772 * otherwise Default LOV EA.
773 * Each time, it can only retrieve 1 stripe EA
775 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
776 struct ptlrpc_request **request, u64 valid)
778 struct ptlrpc_request *req = NULL;
779 struct lov_mds_md *lmm = NULL;
784 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
788 *plmm_size = lmm_size;
794 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
796 struct md_op_data *op_data;
801 OBD_ALLOC_PTR(op_data);
805 op_data->op_flags |= MF_GET_MDT_IDX;
806 op_data->op_fid1 = *fid;
807 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
808 mdt_index = op_data->op_mds;
809 OBD_FREE_PTR(op_data);
817 * Get MDT index for the inode.
819 int ll_get_mdt_idx(struct inode *inode)
821 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
825 * Generic handler to do any pre-copy work.
827 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
828 * first information for it that real work has started.
830 * Moreover, for a ARCHIVE request, it will sample the file data version and
831 * store it in \a copy.
833 * \return 0 on success.
835 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
837 struct ll_sb_info *sbi = ll_s2sbi(sb);
838 struct hsm_progress_kernel hpk;
843 /* Forge a hsm_progress based on data from copy. */
844 hpk.hpk_fid = copy->hc_hai.hai_fid;
845 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
846 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
847 hpk.hpk_extent.length = 0;
850 hpk.hpk_data_version = 0;
853 /* For archive request, we need to read the current file version. */
854 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
856 __u64 data_version = 0;
858 /* Get inode for this fid */
859 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
861 hpk.hpk_flags |= HP_FLAG_RETRY;
862 /* hpk_errval is >= 0 */
863 hpk.hpk_errval = -PTR_ERR(inode);
864 GOTO(progress, rc = PTR_ERR(inode));
867 /* Read current file data version */
868 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
871 CDEBUG(D_HSM, "Could not read file data version of "
872 DFID" (rc = %d). Archive request ("
873 "%#llx) could not be done.\n",
874 PFID(©->hc_hai.hai_fid), rc,
875 copy->hc_hai.hai_cookie);
876 hpk.hpk_flags |= HP_FLAG_RETRY;
877 /* hpk_errval must be >= 0 */
878 hpk.hpk_errval = -rc;
882 /* Store in the hsm_copy for later copytool use.
883 * Always modified even if no lsm. */
884 copy->hc_data_version = data_version;
888 /* On error, the request should be considered as completed */
889 if (hpk.hpk_errval > 0)
890 hpk.hpk_flags |= HP_FLAG_COMPLETED;
892 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
895 /* Return first error */
896 RETURN(rc != 0 ? rc : rc2);
900 * Generic handler to do any post-copy work.
902 * It will send the last hsm_progress update to coordinator to inform it
903 * that copy is finished and whether it was successful or not.
906 * - for ARCHIVE request, it will sample the file data version and compare it
907 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
908 * will be considered as failed.
909 * - for RESTORE request, it will sample the file data version and send it to
910 * coordinator which is useful if the file was imported as 'released'.
912 * \return 0 on success.
914 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
916 struct ll_sb_info *sbi = ll_s2sbi(sb);
917 struct hsm_progress_kernel hpk;
922 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
923 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
924 * initialized if copy_end was called with copy == NULL.
927 /* Forge a hsm_progress based on data from copy. */
928 hpk.hpk_fid = copy->hc_hai.hai_fid;
929 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
930 hpk.hpk_extent = copy->hc_hai.hai_extent;
931 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
932 hpk.hpk_errval = copy->hc_errval;
933 hpk.hpk_data_version = 0;
935 /* For archive request, we need to check the file data was not changed.
937 * For restore request, we need to send the file data version, this is
938 * useful when the file was created using hsm_import.
940 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
941 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
942 (copy->hc_errval == 0)) {
944 __u64 data_version = 0;
946 /* Get lsm for this fid */
947 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
949 hpk.hpk_flags |= HP_FLAG_RETRY;
950 /* hpk_errval must be >= 0 */
951 hpk.hpk_errval = -PTR_ERR(inode);
952 GOTO(progress, rc = PTR_ERR(inode));
955 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
958 CDEBUG(D_HSM, "Could not read file data version. "
959 "Request could not be confirmed.\n");
960 if (hpk.hpk_errval == 0)
961 hpk.hpk_errval = -rc;
965 /* Store in the hsm_copy for later copytool use.
966 * Always modified even if no lsm. */
967 hpk.hpk_data_version = data_version;
969 /* File could have been stripped during archiving, so we need
970 * to check anyway. */
971 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
972 (copy->hc_data_version != data_version)) {
973 CDEBUG(D_HSM, "File data version mismatched. "
974 "File content was changed during archiving. "
975 DFID", start:%#llx current:%#llx\n",
976 PFID(©->hc_hai.hai_fid),
977 copy->hc_data_version, data_version);
978 /* File was changed, send error to cdt. Do not ask for
979 * retry because if a file is modified frequently,
980 * the cdt will loop on retried archive requests.
981 * The policy engine will ask for a new archive later
982 * when the file will not be modified for some tunable
984 hpk.hpk_flags &= ~HP_FLAG_RETRY;
986 /* hpk_errval must be >= 0 */
987 hpk.hpk_errval = -rc;
994 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
997 /* Return first error */
998 RETURN(rc != 0 ? rc : rc2);
1002 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1003 const struct lustre_kernelcomm __user *data)
1005 struct lustre_kernelcomm *lk;
1006 struct lustre_kernelcomm *tmp;
1007 size_t size = sizeof(*lk);
1012 /* copy data from userspace to get numbers of archive_id */
1013 OBD_ALLOC(lk, size);
1017 if (copy_from_user(lk, data, size))
1018 GOTO(out_lk, rc = -EFAULT);
1020 if (lk->lk_flags & LK_FLG_STOP)
1023 if (!(lk->lk_flags & LK_FLG_DATANR)) {
1024 __u32 archive_mask = lk->lk_data_count;
1027 /* old hsm agent to old MDS */
1028 if (!exp_connect_archive_id_array(exp))
1031 /* old hsm agent to new MDS */
1032 lk->lk_flags |= LK_FLG_DATANR;
1034 if (archive_mask == 0)
1037 count = hweight32(archive_mask);
1038 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1039 OBD_ALLOC(tmp, new_size);
1041 GOTO(out_lk, rc = -ENOMEM);
1043 memcpy(tmp, lk, size);
1044 tmp->lk_data_count = count;
1050 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1051 if (BIT(i) & archive_mask) {
1052 lk->lk_data[count] = i + 1;
1059 /* new hsm agent to new mds */
1060 if (lk->lk_data_count > 0) {
1061 new_size = offsetof(struct lustre_kernelcomm,
1062 lk_data[lk->lk_data_count]);
1063 OBD_ALLOC(tmp, new_size);
1065 GOTO(out_lk, rc = -ENOMEM);
1071 if (copy_from_user(lk, data, size))
1072 GOTO(out_lk, rc = -EFAULT);
1075 /* new hsm agent to old MDS */
1076 if (!exp_connect_archive_id_array(exp)) {
1079 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1080 GOTO(out_lk, rc = -EINVAL);
1082 for (i = 0; i < lk->lk_data_count; i++) {
1083 if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1085 CERROR("%s: archive id %d requested but only "
1086 "[0 - %zu] supported: rc = %d\n",
1087 exp->exp_obd->obd_name, lk->lk_data[i],
1088 LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1092 if (lk->lk_data[i] == 0) {
1097 archives |= (1 << (lk->lk_data[i] - 1));
1099 lk->lk_flags &= ~LK_FLG_DATANR;
1100 lk->lk_data_count = archives;
1103 rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1109 static int check_owner(int type, int id)
1113 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1117 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1126 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1128 int cmd = qctl->qc_cmd;
1129 int type = qctl->qc_type;
1130 int id = qctl->qc_id;
1131 int valid = qctl->qc_valid;
1138 case LUSTRE_Q_SETDEFAULT:
1139 case LUSTRE_Q_SETQUOTAPOOL:
1140 case LUSTRE_Q_SETINFOPOOL:
1141 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1145 case LUSTRE_Q_GETDEFAULT:
1146 case LUSTRE_Q_GETQUOTAPOOL:
1147 if (check_owner(type, id) &&
1148 (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1152 case LUSTRE_Q_GETINFOPOOL:
1155 CERROR("unsupported quotactl op: %#x\n", cmd);
1159 if (valid != QC_GENERAL) {
1160 if (cmd == Q_GETINFO)
1161 qctl->qc_cmd = Q_GETOINFO;
1162 else if (cmd == Q_GETQUOTA ||
1163 cmd == LUSTRE_Q_GETQUOTAPOOL)
1164 qctl->qc_cmd = Q_GETOQUOTA;
1170 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1171 sizeof(*qctl), qctl, NULL);
1174 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1175 sizeof(*qctl), qctl, NULL);
1178 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1179 sizeof(*qctl), qctl, NULL);
1181 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1183 sizeof(*qctl), qctl, NULL);
1195 struct obd_quotactl *oqctl;
1196 int oqctl_len = sizeof(*oqctl);
1198 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1199 oqctl_len += LOV_MAXPOOLNAME + 1;
1201 OBD_ALLOC(oqctl, oqctl_len);
1205 QCTL_COPY(oqctl, qctl);
1206 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1208 OBD_FREE(oqctl, oqctl_len);
1211 /* If QIF_SPACE is not set, client should collect the
1212 * space usage from OSSs by itself */
1213 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1214 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1215 !oqctl->qc_dqblk.dqb_curspace) {
1216 struct obd_quotactl *oqctl_tmp;
1218 OBD_ALLOC_PTR(oqctl_tmp);
1219 if (oqctl_tmp == NULL)
1220 GOTO(out, rc = -ENOMEM);
1222 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1223 oqctl_tmp->qc_id = oqctl->qc_id;
1224 oqctl_tmp->qc_type = oqctl->qc_type;
1226 /* collect space usage from OSTs */
1227 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1228 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1229 if (!rc || rc == -EREMOTEIO) {
1230 oqctl->qc_dqblk.dqb_curspace =
1231 oqctl_tmp->qc_dqblk.dqb_curspace;
1232 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1235 /* collect space & inode usage from MDTs */
1236 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1237 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1238 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1239 if (!rc || rc == -EREMOTEIO) {
1240 oqctl->qc_dqblk.dqb_curspace +=
1241 oqctl_tmp->qc_dqblk.dqb_curspace;
1242 oqctl->qc_dqblk.dqb_curinodes =
1243 oqctl_tmp->qc_dqblk.dqb_curinodes;
1244 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1246 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1249 OBD_FREE_PTR(oqctl_tmp);
1252 QCTL_COPY(qctl, oqctl);
1253 OBD_FREE(oqctl, oqctl_len);
1259 int ll_rmfid(struct file *file, void __user *arg)
1261 const struct fid_array __user *ufa = arg;
1262 struct fid_array *lfa = NULL;
1265 int i, rc, *rcs = NULL;
1268 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1269 !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1271 /* Only need to get the buflen */
1272 if (get_user(nr, &ufa->fa_nr))
1274 /* DoS protection */
1275 if (nr > OBD_MAX_FIDS_IN_ARRAY)
1278 size = offsetof(struct fid_array, fa_fids[nr]);
1279 OBD_ALLOC(lfa, size);
1282 OBD_ALLOC_PTR_ARRAY(rcs, nr);
1284 GOTO(free_lfa, rc = -ENOMEM);
1286 if (copy_from_user(lfa, arg, size))
1287 GOTO(free_rcs, rc = -EFAULT);
1289 /* Call mdc_iocontrol */
1290 rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1292 for (i = 0; i < nr; i++)
1294 lfa->fa_fids[i].f_ver = rcs[i];
1295 if (copy_to_user(arg, lfa, size))
1300 OBD_FREE_PTR_ARRAY(rcs, nr);
1302 OBD_FREE(lfa, size);
1307 /* This function tries to get a single name component,
1308 * to send to the server. No actual path traversal involved,
1309 * so we limit to NAME_MAX */
1310 static char *ll_getname(const char __user *filename)
1315 OBD_ALLOC(tmp, NAME_MAX + 1);
1318 return ERR_PTR(-ENOMEM);
1320 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1323 else if (len > NAME_MAX)
1324 ret = -ENAMETOOLONG;
1327 OBD_FREE(tmp, NAME_MAX + 1);
1333 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1335 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1337 struct dentry *dentry = file_dentry(file);
1338 struct inode *inode = file_inode(file);
1339 struct ll_sb_info *sbi = ll_i2sbi(inode);
1340 struct obd_ioctl_data *data;
1344 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1345 PFID(ll_inode2fid(inode)), inode, cmd);
1347 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1348 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1351 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1353 case FS_IOC_GETFLAGS:
1354 case FS_IOC_SETFLAGS:
1355 RETURN(ll_iocontrol(inode, file, cmd, arg));
1356 case FSFILT_IOC_GETVERSION:
1357 case FS_IOC_GETVERSION:
1358 RETURN(put_user(inode->i_generation, (int __user *)arg));
1359 /* We need to special case any other ioctls we want to handle,
1360 * to send them to the MDS/OST as appropriate and to properly
1361 * network encode the arg field. */
1362 case FS_IOC_SETVERSION:
1365 case LL_IOC_GET_MDTIDX: {
1368 mdtidx = ll_get_mdt_idx(inode);
1372 if (put_user((int)mdtidx, (int __user *)arg))
1377 case IOC_MDC_LOOKUP: {
1378 int namelen, len = 0;
1382 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1387 filename = data->ioc_inlbuf1;
1388 namelen = strlen(filename);
1390 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1391 GOTO(out_free, rc = -EINVAL);
1394 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1396 CERROR("%s: lookup %.*s failed: rc = %d\n",
1397 sbi->ll_fsname, namelen, filename, rc);
1401 OBD_FREE_LARGE(buf, len);
1404 case LL_IOC_LMV_SETSTRIPE: {
1405 struct lmv_user_md *lum;
1414 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1419 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1420 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1421 GOTO(lmv_out_free, rc = -EINVAL);
1423 filename = data->ioc_inlbuf1;
1424 namelen = data->ioc_inllen1;
1427 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1428 GOTO(lmv_out_free, rc = -EINVAL);
1430 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1431 lumlen = data->ioc_inllen2;
1433 if (!lmv_user_magic_supported(lum->lum_magic)) {
1434 CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1435 lum->lum_magic, -EINVAL);
1436 GOTO(lmv_out_free, rc = -EINVAL);
1439 if ((lum->lum_magic == LMV_USER_MAGIC ||
1440 lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1441 lumlen < sizeof(*lum)) {
1442 CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1443 filename, lumlen, lum->lum_magic, -EINVAL);
1444 GOTO(lmv_out_free, rc = -EINVAL);
1447 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1448 lumlen < sizeof(struct lmv_foreign_md)) {
1449 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1450 filename, lum->lum_magic, lumlen, -EFAULT);
1451 GOTO(lmv_out_free, rc = -EINVAL);
1454 mode = data->ioc_type;
1455 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1457 OBD_FREE_LARGE(buf, len);
1461 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1462 struct lmv_user_md lum;
1463 struct lmv_user_md __user *ulump =
1464 (struct lmv_user_md __user *)arg;
1467 if (copy_from_user(&lum, ulump, sizeof(lum)))
1470 if (lum.lum_magic != LMV_USER_MAGIC)
1473 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1477 case LL_IOC_LOV_SETSTRIPE_NEW:
1478 case LL_IOC_LOV_SETSTRIPE: {
1479 struct lov_user_md_v3 *lumv3 = NULL;
1480 struct lov_user_md_v1 lumv1;
1481 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1482 struct lov_user_md_v1 __user *lumv1p =
1483 (struct lov_user_md_v1 __user *)arg;
1484 struct lov_user_md_v3 __user *lumv3p =
1485 (struct lov_user_md_v3 __user *)arg;
1488 int set_default = 0;
1490 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1491 sizeof(struct lov_comp_md_v1));
1492 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1493 /* first try with v1 which is smaller than v3 */
1494 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1497 if (inode->i_sb->s_root == file_dentry(file))
1500 switch (lumv1.lmm_magic) {
1501 case LOV_USER_MAGIC_V3:
1502 case LOV_USER_MAGIC_SPECIFIC:
1503 lum_size = ll_lov_user_md_size(&lumv1);
1506 OBD_ALLOC(lumv3, lum_size);
1509 if (copy_from_user(lumv3, lumv3p, lum_size))
1510 GOTO(out, rc = -EFAULT);
1511 lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1513 case LOV_USER_MAGIC_V1:
1516 GOTO(out, rc = -ENOTSUPP);
1519 /* in v1 and v3 cases lumv1 points to data */
1520 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1523 OBD_FREE(lumv3, lum_size);
1526 case LL_IOC_LMV_GETSTRIPE: {
1527 struct lmv_user_md __user *ulmv =
1528 (struct lmv_user_md __user *)arg;
1529 struct lmv_user_md lum;
1530 struct ptlrpc_request *request = NULL;
1531 struct ptlrpc_request *root_request = NULL;
1532 union lmv_mds_md *lmm = NULL;
1535 struct lmv_user_md *tmp = NULL;
1539 int max_stripe_count;
1543 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1546 max_stripe_count = lum.lum_stripe_count;
1547 /* lum_magic will indicate which stripe the ioctl will like
1548 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1549 * is for default LMV stripe */
1550 if (lum.lum_magic == LMV_MAGIC_V1)
1551 valid |= OBD_MD_MEA;
1552 else if (lum.lum_magic == LMV_USER_MAGIC)
1553 valid |= OBD_MD_DEFAULT_MEA;
1557 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1558 &request, &root_request, valid);
1560 GOTO(finish_req, rc);
1562 /* Get default LMV EA */
1563 if (lum.lum_magic == LMV_USER_MAGIC) {
1564 if (lmmsize > sizeof(*ulmv))
1565 GOTO(finish_req, rc = -EINVAL);
1567 if (copy_to_user(ulmv, lmm, lmmsize))
1568 GOTO(finish_req, rc = -EFAULT);
1570 GOTO(finish_req, rc);
1573 /* if foreign LMV case, fake stripes number */
1574 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1575 struct lmv_foreign_md *lfm;
1577 lfm = (struct lmv_foreign_md *)lmm;
1578 if (lfm->lfm_length < XATTR_SIZE_MAX -
1579 offsetof(typeof(*lfm), lfm_value)) {
1580 __u32 size = lfm->lfm_length +
1581 offsetof(typeof(*lfm), lfm_value);
1583 stripe_count = lmv_foreign_to_md_stripes(size);
1585 CERROR("invalid %d foreign size returned\n",
1590 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1592 if (max_stripe_count < stripe_count) {
1593 lum.lum_stripe_count = stripe_count;
1594 if (copy_to_user(ulmv, &lum, sizeof(lum)))
1595 GOTO(finish_req, rc = -EFAULT);
1596 GOTO(finish_req, rc = -E2BIG);
1599 /* enough room on user side and foreign case */
1600 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1601 struct lmv_foreign_md *lfm;
1604 lfm = (struct lmv_foreign_md *)lmm;
1605 size = lfm->lfm_length +
1606 offsetof(struct lmv_foreign_md, lfm_value);
1607 if (copy_to_user(ulmv, lfm, size))
1608 GOTO(finish_req, rc = -EFAULT);
1609 GOTO(finish_req, rc);
1612 lum_size = lmv_user_md_size(stripe_count,
1613 LMV_USER_MAGIC_SPECIFIC);
1614 OBD_ALLOC(tmp, lum_size);
1616 GOTO(finish_req, rc = -ENOMEM);
1618 mdt_index = ll_get_mdt_idx(inode);
1620 GOTO(out_tmp, rc = -ENOMEM);
1622 tmp->lum_magic = LMV_MAGIC_V1;
1623 tmp->lum_stripe_count = 0;
1624 tmp->lum_stripe_offset = mdt_index;
1625 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1626 for (i = 0; i < stripe_count; i++) {
1629 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1630 if (fid_is_sane(&fid)) {
1631 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1633 GOTO(out_tmp, rc = mdt_index);
1635 tmp->lum_objects[i].lum_mds = mdt_index;
1636 tmp->lum_objects[i].lum_fid = fid;
1639 tmp->lum_stripe_count++;
1642 if (copy_to_user(ulmv, tmp, lum_size))
1643 GOTO(out_tmp, rc = -EFAULT);
1645 OBD_FREE(tmp, lum_size);
1647 ptlrpc_req_finished(request);
1648 ptlrpc_req_finished(root_request);
1652 case LL_IOC_REMOVE_ENTRY: {
1653 char *filename = NULL;
1657 /* Here is a little hack to avoid sending REINT_RMENTRY to
1658 * unsupported server, which might crash the server(LU-2730),
1659 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1660 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1661 * server will support REINT_RMENTRY XXX*/
1662 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1665 filename = ll_getname((const char __user *)arg);
1666 if (IS_ERR(filename))
1667 RETURN(PTR_ERR(filename));
1669 namelen = strlen(filename);
1671 GOTO(out_rmdir, rc = -EINVAL);
1673 rc = ll_rmdir_entry(inode, filename, namelen);
1676 ll_putname(filename);
1680 RETURN(ll_rmfid(file, (void __user *)arg));
1681 case LL_IOC_LOV_SWAP_LAYOUTS:
1683 case IOC_OBD_STATFS:
1684 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1685 case LL_IOC_LOV_GETSTRIPE:
1686 case LL_IOC_LOV_GETSTRIPE_NEW:
1687 case LL_IOC_MDC_GETINFO:
1688 case LL_IOC_MDC_GETINFO_OLD:
1689 case IOC_MDC_GETFILEINFO:
1690 case IOC_MDC_GETFILEINFO_OLD:
1691 case IOC_MDC_GETFILESTRIPE: {
1692 struct ptlrpc_request *request = NULL;
1693 struct ptlrpc_request *root_request = NULL;
1694 struct lov_user_md __user *lump;
1695 struct lov_mds_md *lmm = NULL;
1696 struct mdt_body *body;
1697 char *filename = NULL;
1698 lstat_t __user *statp = NULL;
1699 lstatx_t __user *stxp = NULL;
1700 __u64 __user *flagsp = NULL;
1701 __u32 __user *lmmsizep = NULL;
1702 struct lu_fid __user *fidp = NULL;
1705 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1706 cmd == IOC_MDC_GETFILEINFO ||
1707 cmd == IOC_MDC_GETFILESTRIPE) {
1708 filename = ll_getname((const char __user *)arg);
1709 if (IS_ERR(filename))
1710 RETURN(PTR_ERR(filename));
1712 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1713 &lmmsize, &request);
1715 rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1721 body = req_capsule_server_get(&request->rq_pill,
1723 LASSERT(body != NULL);
1728 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1729 cmd == LL_IOC_MDC_GETINFO ||
1730 cmd == IOC_MDC_GETFILEINFO_OLD ||
1731 cmd == LL_IOC_MDC_GETINFO_OLD)) {
1739 if (cmd == IOC_MDC_GETFILESTRIPE ||
1740 cmd == LL_IOC_LOV_GETSTRIPE ||
1741 cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1742 lump = (struct lov_user_md __user *)arg;
1743 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1744 cmd == LL_IOC_MDC_GETINFO_OLD){
1745 struct lov_user_mds_data_v1 __user *lmdp;
1747 lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1748 statp = &lmdp->lmd_st;
1749 lump = &lmdp->lmd_lmm;
1751 struct lov_user_mds_data __user *lmdp;
1753 lmdp = (struct lov_user_mds_data __user *)arg;
1754 fidp = &lmdp->lmd_fid;
1755 stxp = &lmdp->lmd_stx;
1756 flagsp = &lmdp->lmd_flags;
1757 lmmsizep = &lmdp->lmd_lmmsize;
1758 lump = &lmdp->lmd_lmm;
1762 /* If the file has no striping then zero out *lump so
1763 * that the caller isn't confused by garbage. */
1764 if (clear_user(lump, sizeof(*lump)))
1765 GOTO(out_req, rc = -EFAULT);
1766 } else if (copy_to_user(lump, lmm, lmmsize)) {
1767 if (copy_to_user(lump, lmm, sizeof(*lump)))
1768 GOTO(out_req, rc = -EFAULT);
1772 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1773 cmd == LL_IOC_MDC_GETINFO_OLD) {
1776 st.st_dev = inode->i_sb->s_dev;
1777 st.st_mode = body->mbo_mode;
1778 st.st_nlink = body->mbo_nlink;
1779 st.st_uid = body->mbo_uid;
1780 st.st_gid = body->mbo_gid;
1781 st.st_rdev = body->mbo_rdev;
1782 st.st_size = body->mbo_size;
1783 st.st_blksize = PAGE_SIZE;
1784 st.st_blocks = body->mbo_blocks;
1785 st.st_atime = body->mbo_atime;
1786 st.st_mtime = body->mbo_mtime;
1787 st.st_ctime = body->mbo_ctime;
1788 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1792 if (copy_to_user(statp, &st, sizeof(st)))
1793 GOTO(out_req, rc = -EFAULT);
1794 } else if (cmd == IOC_MDC_GETFILEINFO ||
1795 cmd == LL_IOC_MDC_GETINFO) {
1796 lstatx_t stx = { 0 };
1797 __u64 valid = body->mbo_valid;
1799 stx.stx_blksize = PAGE_SIZE;
1800 stx.stx_nlink = body->mbo_nlink;
1801 stx.stx_uid = body->mbo_uid;
1802 stx.stx_gid = body->mbo_gid;
1803 stx.stx_mode = body->mbo_mode;
1804 stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1807 stx.stx_size = body->mbo_size;
1808 stx.stx_blocks = body->mbo_blocks;
1809 stx.stx_atime.tv_sec = body->mbo_atime;
1810 stx.stx_ctime.tv_sec = body->mbo_ctime;
1811 stx.stx_mtime.tv_sec = body->mbo_mtime;
1812 stx.stx_btime.tv_sec = body->mbo_btime;
1813 stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1814 stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1815 stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1816 stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1817 stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1820 * For a striped directory, the size and blocks returned
1821 * from MDT is not correct.
1822 * The size and blocks are aggregated by client across
1824 * Thus for a striped directory, do not return the valid
1825 * FLSIZE and FLBLOCKS flags to the caller.
1826 * However, this whould be better decided by the MDS
1827 * instead of the client.
1829 if (cmd == LL_IOC_MDC_GETINFO &&
1830 ll_i2info(inode)->lli_lsm_md != NULL)
1831 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1833 if (flagsp && copy_to_user(flagsp, &valid,
1835 GOTO(out_req, rc = -EFAULT);
1837 if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1839 GOTO(out_req, rc = -EFAULT);
1841 if (!(valid & OBD_MD_FLSIZE))
1842 stx.stx_mask &= ~STATX_SIZE;
1843 if (!(valid & OBD_MD_FLBLOCKS))
1844 stx.stx_mask &= ~STATX_BLOCKS;
1846 if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1847 GOTO(out_req, rc = -EFAULT);
1849 if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1851 GOTO(out_req, rc = -EFAULT);
1856 ptlrpc_req_finished(request);
1857 ptlrpc_req_finished(root_request);
1859 ll_putname(filename);
1862 case OBD_IOC_QUOTACTL: {
1863 struct if_quotactl *qctl;
1864 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1866 OBD_ALLOC(qctl, qctl_len);
1870 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1871 GOTO(out_quotactl, rc = -EFAULT);
1873 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
1874 char __user *from = (char __user *)arg +
1875 offsetof(typeof(*qctl), qc_poolname);
1876 if (copy_from_user(qctl->qc_poolname, from,
1877 LOV_MAXPOOLNAME + 1))
1878 GOTO(out_quotactl, rc = -EFAULT);
1881 rc = quotactl_ioctl(sbi, qctl);
1883 copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1887 OBD_FREE(qctl, qctl_len);
1890 case OBD_IOC_GETDTNAME:
1891 case OBD_IOC_GETMDNAME:
1892 RETURN(ll_get_obd_name(inode, cmd, arg));
1893 case LL_IOC_FLUSHCTX:
1894 RETURN(ll_flush_ctx(inode));
1895 case LL_IOC_GETOBDCOUNT: {
1897 struct obd_export *exp;
1899 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1902 /* get ost count when count is zero, get mdt count otherwise */
1903 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1904 vallen = sizeof(count);
1905 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1906 KEY_TGT_COUNT, &vallen, &count);
1908 CERROR("get target count failed: %d\n", rc);
1912 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1917 case LL_IOC_PATH2FID:
1918 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1919 sizeof(struct lu_fid)))
1922 case LL_IOC_GET_CONNECT_FLAGS: {
1923 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1924 (void __user *)arg));
1926 case OBD_IOC_FID2PATH:
1927 RETURN(ll_fid2path(inode, (void __user *)arg));
1928 case LL_IOC_GETPARENT:
1929 RETURN(ll_getparent(file, (void __user *)arg));
1930 case LL_IOC_FID2MDTIDX: {
1931 struct obd_export *exp = ll_i2mdexp(inode);
1935 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1939 /* Call mdc_iocontrol */
1940 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1941 (__u32 __user *)&index);
1947 case LL_IOC_HSM_REQUEST: {
1948 struct hsm_user_request *hur;
1955 /* We don't know the true size yet; copy the fixed-size part */
1956 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1961 /* Compute the whole struct size */
1962 totalsize = hur_len(hur);
1967 /* Final size will be more than double totalsize */
1968 if (totalsize >= MDS_MAXREQSIZE / 3)
1971 OBD_ALLOC_LARGE(hur, totalsize);
1975 /* Copy the whole struct */
1976 if (copy_from_user(hur, (void __user *)arg, totalsize))
1977 GOTO(out_hur, rc = -EFAULT);
1979 if (hur->hur_request.hr_action == HUA_RELEASE) {
1980 const struct lu_fid *fid;
1984 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1985 fid = &hur->hur_user_item[i].hui_fid;
1986 f = search_inode_for_lustre(inode->i_sb, fid);
1992 rc = ll_hsm_release(f);
1998 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2003 OBD_FREE_LARGE(hur, totalsize);
2007 case LL_IOC_HSM_PROGRESS: {
2008 struct hsm_progress_kernel hpk;
2009 struct hsm_progress hp;
2011 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2014 hpk.hpk_fid = hp.hp_fid;
2015 hpk.hpk_cookie = hp.hp_cookie;
2016 hpk.hpk_extent = hp.hp_extent;
2017 hpk.hpk_flags = hp.hp_flags;
2018 hpk.hpk_errval = hp.hp_errval;
2019 hpk.hpk_data_version = 0;
2021 /* File may not exist in Lustre; all progress
2022 * reported to Lustre root */
2023 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2027 case LL_IOC_HSM_CT_START:
2028 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2031 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2032 (struct lustre_kernelcomm __user *)arg);
2035 case LL_IOC_HSM_COPY_START: {
2036 struct hsm_copy *copy;
2039 OBD_ALLOC_PTR(copy);
2042 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2047 rc = ll_ioc_copy_start(inode->i_sb, copy);
2048 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2054 case LL_IOC_HSM_COPY_END: {
2055 struct hsm_copy *copy;
2058 OBD_ALLOC_PTR(copy);
2061 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2066 rc = ll_ioc_copy_end(inode->i_sb, copy);
2067 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2073 case LL_IOC_MIGRATE: {
2074 struct lmv_user_md *lum;
2081 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2085 data = (struct obd_ioctl_data *)buf;
2086 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2087 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2088 GOTO(migrate_free, rc = -EINVAL);
2090 filename = data->ioc_inlbuf1;
2091 namelen = data->ioc_inllen1;
2093 if (namelen < 1 || namelen != strlen(filename) + 1) {
2094 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2095 GOTO(migrate_free, rc = -EINVAL);
2098 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2099 if (lum->lum_magic != LMV_USER_MAGIC &&
2100 lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2102 CERROR("%s: wrong lum magic %x: rc = %d\n",
2103 filename, lum->lum_magic, rc);
2104 GOTO(migrate_free, rc);
2107 rc = ll_migrate(inode, file, lum, filename);
2109 OBD_FREE_LARGE(buf, len);
2113 case FS_IOC_FSGETXATTR:
2114 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2115 case FS_IOC_FSSETXATTR:
2116 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2117 case LL_IOC_PCC_DETACH_BY_FID: {
2118 struct lu_pcc_detach_fid *detach;
2120 struct inode *inode2;
2123 OBD_ALLOC_PTR(detach);
2127 if (copy_from_user(detach,
2128 (const struct lu_pcc_detach_fid __user *)arg,
2130 GOTO(out_detach, rc = -EFAULT);
2132 fid = &detach->pccd_fid;
2133 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2134 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2136 /* Target inode is not in inode cache, and PCC file
2137 * has aleady released, return immdiately.
2139 GOTO(out_detach, rc = 0);
2141 if (!S_ISREG(inode2->i_mode))
2142 GOTO(out_iput, rc = -EINVAL);
2144 if (!inode_owner_or_capable(inode2))
2145 GOTO(out_iput, rc = -EPERM);
2147 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2151 OBD_FREE_PTR(detach);
2154 #ifdef HAVE_LUSTRE_CRYPTO
2155 case LL_IOC_SET_ENCRYPTION_POLICY:
2156 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2158 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2159 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2160 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2162 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2163 case LL_IOC_ADD_ENCRYPTION_KEY:
2164 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2166 return llcrypt_ioctl_add_key(file, (void __user *)arg);
2167 case LL_IOC_REMOVE_ENCRYPTION_KEY:
2168 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2170 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2171 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2172 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2174 return llcrypt_ioctl_remove_key_all_users(file,
2175 (void __user *)arg);
2176 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2177 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2179 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2182 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2183 (void __user *)arg));
2187 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2189 struct inode *inode = file->f_mapping->host;
2190 struct ll_file_data *fd = file->private_data;
2191 struct ll_sb_info *sbi = ll_i2sbi(inode);
2192 int api32 = ll_need_32bit_api(sbi);
2193 loff_t ret = -EINVAL;
2201 offset += file->f_pos;
2207 offset += LL_DIR_END_OFF_32BIT;
2209 offset += LL_DIR_END_OFF;
2216 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2217 (!api32 && offset <= LL_DIR_END_OFF))) {
2218 if (offset != file->f_pos) {
2219 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2220 (!api32 && offset == LL_DIR_END_OFF))
2221 fd->lfd_pos = MDS_DIR_END_OFF;
2222 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2223 fd->lfd_pos = offset << 32;
2225 fd->lfd_pos = offset;
2226 file->f_pos = offset;
2227 file->f_version = 0;
2234 inode_unlock(inode);
2238 static int ll_dir_open(struct inode *inode, struct file *file)
2241 RETURN(ll_file_open(inode, file));
2244 static int ll_dir_release(struct inode *inode, struct file *file)
2247 RETURN(ll_file_release(inode, file));
2250 const struct file_operations ll_dir_operations = {
2251 .llseek = ll_dir_seek,
2252 .open = ll_dir_open,
2253 .release = ll_dir_release,
2254 .read = generic_read_dir,
2255 #ifdef HAVE_DIR_CONTEXT
2256 .iterate_shared = ll_iterate,
2258 .readdir = ll_readdir,
2260 .unlocked_ioctl = ll_dir_ioctl,