1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2011, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
35 * Directory code for lustre client.
39 #include <linux/pagemap.h>
41 #include <linux/uaccess.h>
42 #include <linux/buffer_head.h> /* for wait_on_buffer */
43 #include <linux/pagevec.h>
44 #include <linux/prefetch.h>
46 #define DEBUG_SUBSYSTEM S_LLITE
48 #include <obd_support.h>
49 #include <obd_class.h>
50 #include <uapi/linux/lustre/lustre_ioctl.h>
51 #include <lustre_lib.h>
52 #include <lustre_dlm.h>
53 #include <lustre_fid.h>
54 #include <lustre_kernelcomm.h>
55 #include <lustre_swab.h>
57 #include "llite_internal.h"
60 * (new) readdir implementation overview.
62 * Original lustre readdir implementation cached exact copy of raw directory
63 * pages on the client. These pages were indexed in client page cache by
64 * logical offset in the directory file. This design, while very simple and
65 * intuitive had some inherent problems:
67 * . it implies that byte offset to the directory entry serves as a
68 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
69 * ext3/htree directory entries may move due to splits, and more
72 * . it is incompatible with the design of split directories for cmd3,
73 * that assumes that names are distributed across nodes based on their
74 * hash, and so readdir should be done in hash order.
76 * New readdir implementation does readdir in hash order, and uses hash of a
77 * file name as a telldir/seekdir cookie. This led to number of complications:
79 * . hash is not unique, so it cannot be used to index cached directory
80 * pages on the client (note, that it requires a whole pageful of hash
81 * collided entries to cause two pages to have identical hashes);
83 * . hash is not unique, so it cannot, strictly speaking, be used as an
84 * entry cookie. ext3/htree has the same problem and lustre implementation
85 * mimics their solution: seekdir(hash) positions directory at the first
86 * entry with the given hash.
92 * Client caches directory pages using hash of the first entry as an index. As
93 * noted above hash is not unique, so this solution doesn't work as is:
94 * special processing is needed for "page hash chains" (i.e., sequences of
95 * pages filled with entries all having the same hash value).
97 * First, such chains have to be detected. To this end, server returns to the
98 * client the hash of the first entry on the page next to one returned. When
99 * client detects that this hash is the same as hash of the first entry on the
100 * returned page, page hash collision has to be handled. Pages in the
101 * hash chain, except first one, are termed "overflow pages".
103 * Solution to index uniqueness problem is to not cache overflow
104 * pages. Instead, when page hash collision is detected, all overflow pages
105 * from emerging chain are immediately requested from the server and placed in
106 * a special data structure (struct ll_dir_chain). This data structure is used
107 * by ll_readdir() to process entries from overflow pages. When readdir
108 * invocation finishes, overflow pages are discarded. If page hash collision
109 * chain weren't completely processed, next call to readdir will again detect
110 * page hash collision, again read overflow pages in, process next portion of
111 * entries and again discard the pages. This is not as wasteful as it looks,
112 * because, given reasonable hash, page hash collisions are extremely rare.
114 * 1. directory positioning
116 * When seekdir(hash) is called, original
127 * identification of and access to overflow pages
131 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
132 * a header lu_dirpage which describes the start/end hash, and whether this
133 * page is empty (contains no dir entry) or hash collide with next page.
134 * After client receives reply, several pages will be integrated into dir page
135 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the lu_dirpage
136 * for this integrated page will be adjusted. See lmv_adjust_dirpages().
139 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
142 struct md_callback cb_op;
146 cb_op.md_blocking_ast = ll_md_blocking_ast;
147 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
154 void ll_release_page(struct inode *inode, struct page *page, bool remove)
159 * Always remove the page for striped dir, because the page is
160 * built from temporarily in LMV layer
162 if (inode && S_ISDIR(inode->i_mode) &&
163 ll_i2info(inode)->lli_lsm_md) {
170 if (likely(page->mapping))
171 truncate_complete_page(page->mapping, page);
178 * return IF_* type for given lu_dirent entry.
179 * IF_* flag shld be converted to particular OS file type in
180 * platform llite module.
182 static __u16 ll_dirent_type_get(struct lu_dirent *ent)
185 struct luda_type *lt;
188 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
189 const unsigned int align = sizeof(struct luda_type) - 1;
191 len = le16_to_cpu(ent->lde_namelen);
192 len = (len + align) & ~align;
193 lt = (void *)ent->lde_name + len;
194 type = IFTODT(le16_to_cpu(lt->lt_type));
199 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
200 struct dir_context *ctx)
202 struct ll_sb_info *sbi = ll_i2sbi(inode);
204 int is_api32 = ll_need_32bit_api(sbi);
205 int is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
210 page = ll_get_dir_page(inode, op_data, pos);
212 while (rc == 0 && !done) {
213 struct lu_dirpage *dp;
214 struct lu_dirent *ent;
223 hash = MDS_DIR_END_OFF;
224 dp = page_address(page);
225 for (ent = lu_dirent_start(dp); ent && !done;
226 ent = lu_dirent_next(ent)) {
233 hash = le64_to_cpu(ent->lde_hash);
236 * Skip until we find target hash
241 namelen = le16_to_cpu(ent->lde_namelen);
248 if (is_api32 && is_hash64)
252 fid_le_to_cpu(&fid, &ent->lde_fid);
253 ino = cl_fid_build_ino(&fid, is_api32);
254 type = ll_dirent_type_get(ent);
256 /* For 'll_nfs_get_name_filldir()', it will try
257 * to access the 'ent' through its 'lde_name',
258 * so the parameter 'name' for 'ctx->actor()'
259 * must be part of the 'ent'.
261 done = !dir_emit(ctx, ent->lde_name,
267 ll_release_page(inode, page, false);
271 next = le64_to_cpu(dp->ldp_hash_end);
273 if (pos == MDS_DIR_END_OFF) {
275 * End of directory reached.
278 ll_release_page(inode, page, false);
281 * Normal case: continue to the next
284 ll_release_page(inode, page,
285 le32_to_cpu(dp->ldp_flags) &
288 page = ll_get_dir_page(inode, op_data, pos);
296 static int ll_readdir(struct file *filp, struct dir_context *ctx)
298 struct inode *inode = file_inode(filp);
299 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
300 struct ll_sb_info *sbi = ll_i2sbi(inode);
301 __u64 pos = lfd ? lfd->lfd_pos : 0;
302 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
303 int api32 = ll_need_32bit_api(sbi);
304 struct md_op_data *op_data;
308 "VFS Op:inode=" DFID "(%p) pos/size %lu/%llu 32bit_api %d\n",
309 PFID(ll_inode2fid(inode)), inode, (unsigned long)pos,
310 i_size_read(inode), api32);
312 if (pos == MDS_DIR_END_OFF) {
320 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
321 LUSTRE_OPC_ANY, inode);
322 if (IS_ERR(op_data)) {
323 rc = PTR_ERR(op_data);
327 if (unlikely(op_data->op_mea1)) {
329 * This is only needed for striped dir to fill ..,
332 if (file_dentry(filp)->d_parent &&
333 file_dentry(filp)->d_parent->d_inode) {
334 __u64 ibits = MDS_INODELOCK_UPDATE;
335 struct inode *parent;
337 parent = file_dentry(filp)->d_parent->d_inode;
338 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
339 op_data->op_fid3 = *ll_inode2fid(parent);
343 * If it can not find in cache, do lookup .. on the master
346 if (fid_is_zero(&op_data->op_fid3)) {
347 rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
349 ll_finish_md_op_data(op_data);
354 op_data->op_max_pages = sbi->ll_md_brw_pages;
356 rc = ll_dir_read(inode, &pos, op_data, ctx);
361 if (pos == MDS_DIR_END_OFF) {
363 pos = LL_DIR_END_OFF_32BIT;
365 pos = LL_DIR_END_OFF;
371 ll_finish_md_op_data(op_data);
372 filp->f_version = inode->i_version;
376 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
381 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
383 struct mgs_send_param *msp;
386 msp = kzalloc(sizeof(*msp), GFP_NOFS);
390 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
391 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
392 sizeof(struct mgs_send_param), msp, NULL);
394 CERROR("Failed to set parameter: %d\n", rc);
401 * Create striped directory with specified stripe(@lump)
403 * param[in] parent the parent of the directory.
404 * param[in] lump the specified stripes.
405 * param[in] dirname the name of the directory.
406 * param[in] mode the specified mode of the directory.
408 * retval =0 if striped directory is being created successfully.
409 * <0 if the creation is failed.
411 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
412 const char *dirname, umode_t mode)
414 struct ptlrpc_request *request = NULL;
415 struct md_op_data *op_data;
416 struct ll_sb_info *sbi = ll_i2sbi(parent);
417 struct inode *inode = NULL;
418 struct dentry dentry;
421 if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
424 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) name %s stripe_offset %d, stripe_count: %u\n",
425 PFID(ll_inode2fid(parent)), parent, dirname,
426 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
428 if (lump->lum_stripe_count > 1 &&
429 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
432 if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
433 lustre_swab_lmv_user_md(lump);
435 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
436 mode &= ~current_umask();
437 mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
438 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
439 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
441 if (IS_ERR(op_data)) {
442 err = PTR_ERR(op_data);
446 op_data->op_cli_flags |= CLI_SET_MEA;
447 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
448 from_kuid(&init_user_ns, current_fsuid()),
449 from_kgid(&init_user_ns, current_fsgid()),
450 cfs_curproc_cap_pack(), 0, &request);
451 ll_finish_md_op_data(op_data);
453 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
457 memset(&dentry, 0, sizeof(dentry));
458 dentry.d_inode = inode;
460 err = ll_init_security(&dentry, inode, parent);
464 ptlrpc_req_finished(request);
468 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
471 struct ll_sb_info *sbi = ll_i2sbi(inode);
472 struct md_op_data *op_data;
473 struct ptlrpc_request *req = NULL;
475 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
476 struct obd_device *mgc = lsi->lsi_mgc;
481 * This is coming from userspace, so should be in
482 * local endian. But the MDS would like it in little
483 * endian, so we swab it before we send it.
485 switch (lump->lmm_magic) {
486 case LOV_USER_MAGIC_V1: {
487 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
488 lustre_swab_lov_user_md_v1(lump);
489 lum_size = sizeof(struct lov_user_md_v1);
492 case LOV_USER_MAGIC_V3: {
493 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
494 lustre_swab_lov_user_md_v3(
495 (struct lov_user_md_v3 *)lump);
496 lum_size = sizeof(struct lov_user_md_v3);
499 case LMV_USER_MAGIC: {
500 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
501 lustre_swab_lmv_user_md(
502 (struct lmv_user_md *)lump);
503 lum_size = sizeof(struct lmv_user_md);
508 "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
509 lump->lmm_magic, LOV_USER_MAGIC_V1,
515 lum_size = sizeof(struct lov_user_md_v1);
518 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
519 LUSTRE_OPC_ANY, NULL);
521 return PTR_ERR(op_data);
523 /* swabbing is done in lov_setstripe() on server side */
524 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
525 ll_finish_md_op_data(op_data);
526 ptlrpc_req_finished(req);
530 #if OBD_OCD_VERSION(2, 13, 53, 0) > LUSTRE_VERSION_CODE
532 * 2.9 server has stored filesystem default stripe in ROOT xattr,
533 * and it's stored into system config for backward compatibility.
535 * In the following we use the fact that LOV_USER_MAGIC_V1 and
536 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
537 * need to make the distinction between the 2 versions
539 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
543 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
548 /* Get fsname and assume devname to be -MDT0000. */
549 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
550 strcat(buf, "-MDT0000.lov");
553 /* Set root stripesize */
554 sprintf(buf, ".stripesize=%u",
555 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
556 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
560 /* Set root stripecount */
561 sprintf(buf, ".stripecount=%hd",
562 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
563 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
567 /* Set root stripeoffset */
568 sprintf(buf, ".stripeoffset=%hd",
569 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
570 (typeof(lump->lmm_stripe_offset))(-1));
571 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
581 * This function will be used to get default LOV/LMV/Default LMV
582 * @valid will be used to indicate which stripe it will retrieve
583 * OBD_MD_MEA LMV stripe EA
584 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
585 * otherwise Default LOV EA.
586 * Each time, it can only retrieve 1 stripe EA
588 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
589 struct ptlrpc_request **request, u64 valid)
591 struct ll_sb_info *sbi = ll_i2sbi(inode);
592 struct mdt_body *body;
593 struct lov_mds_md *lmm = NULL;
594 struct ptlrpc_request *req = NULL;
596 struct md_op_data *op_data;
598 rc = ll_get_max_mdsize(sbi, &lmmsize);
602 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
603 0, lmmsize, LUSTRE_OPC_ANY,
606 return PTR_ERR(op_data);
608 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
609 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
610 ll_finish_md_op_data(op_data);
612 CDEBUG(D_INFO, "md_getattr failed on inode " DFID ": rc %d\n",
613 PFID(ll_inode2fid(inode)), rc);
617 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
619 lmmsize = body->mbo_eadatasize;
621 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
627 lmm = req_capsule_server_sized_get(&req->rq_pill,
628 &RMF_MDT_MD, lmmsize);
632 * This is coming from the MDS, so is probably in
633 * little endian. We convert it to host endian before
634 * passing it to userspace.
636 /* We don't swab objects for directories */
637 switch (le32_to_cpu(lmm->lmm_magic)) {
639 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
640 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
643 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
644 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
647 if (cpu_to_le32(LMV_MAGIC) != LMV_MAGIC)
648 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
651 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
652 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
655 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
660 *plmm_size = lmmsize;
665 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
667 struct md_op_data *op_data;
670 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
674 op_data->op_flags |= MF_GET_MDT_IDX;
675 op_data->op_fid1 = *fid;
676 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
677 mdt_index = op_data->op_mds;
686 * Get MDT index for the inode.
688 int ll_get_mdt_idx(struct inode *inode)
690 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
694 * Generic handler to do any pre-copy work.
696 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
697 * first information for it that real work has started.
699 * Moreover, for a ARCHIVE request, it will sample the file data version and
700 * store it in \a copy.
702 * \return 0 on success.
704 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
706 struct ll_sb_info *sbi = ll_s2sbi(sb);
707 struct hsm_progress_kernel hpk;
710 /* Forge a hsm_progress based on data from copy. */
711 hpk.hpk_fid = copy->hc_hai.hai_fid;
712 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
713 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
714 hpk.hpk_extent.length = 0;
717 hpk.hpk_data_version = 0;
719 /* For archive request, we need to read the current file version. */
720 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
722 __u64 data_version = 0;
724 /* Get inode for this fid */
725 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
727 hpk.hpk_flags |= HP_FLAG_RETRY;
728 /* hpk_errval is >= 0 */
729 hpk.hpk_errval = -PTR_ERR(inode);
734 /* Read current file data version */
735 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
739 "Could not read file data version of " DFID " (rc = %d). Archive request (%#llx) could not be done.\n",
740 PFID(©->hc_hai.hai_fid), rc,
741 copy->hc_hai.hai_cookie);
742 hpk.hpk_flags |= HP_FLAG_RETRY;
743 /* hpk_errval must be >= 0 */
744 hpk.hpk_errval = -rc;
748 /* Store in the hsm_copy for later copytool use.
749 * Always modified even if no lsm.
751 copy->hc_data_version = data_version;
755 /* On error, the request should be considered as completed */
756 if (hpk.hpk_errval > 0)
757 hpk.hpk_flags |= HP_FLAG_COMPLETED;
758 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
761 return rc ? rc : rc2;
765 * Generic handler to do any post-copy work.
767 * It will send the last hsm_progress update to coordinator to inform it
768 * that copy is finished and whether it was successful or not.
771 * - for ARCHIVE request, it will sample the file data version and compare it
772 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
773 * will be considered as failed.
774 * - for RESTORE request, it will sample the file data version and send it to
775 * coordinator which is useful if the file was imported as 'released'.
777 * \return 0 on success.
779 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
781 struct ll_sb_info *sbi = ll_s2sbi(sb);
782 struct hsm_progress_kernel hpk;
785 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
786 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
787 * initialized if copy_end was called with copy == NULL.
790 /* Forge a hsm_progress based on data from copy. */
791 hpk.hpk_fid = copy->hc_hai.hai_fid;
792 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
793 hpk.hpk_extent = copy->hc_hai.hai_extent;
794 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
795 hpk.hpk_errval = copy->hc_errval;
796 hpk.hpk_data_version = 0;
798 /* For archive request, we need to check the file data was not changed.
800 * For restore request, we need to send the file data version, this is
801 * useful when the file was created using hsm_import.
803 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
804 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
805 (copy->hc_errval == 0)) {
807 __u64 data_version = 0;
809 /* Get lsm for this fid */
810 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
812 hpk.hpk_flags |= HP_FLAG_RETRY;
813 /* hpk_errval must be >= 0 */
814 hpk.hpk_errval = -PTR_ERR(inode);
819 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
823 "Could not read file data version. Request could not be confirmed.\n");
824 if (hpk.hpk_errval == 0)
825 hpk.hpk_errval = -rc;
829 /* Store in the hsm_copy for later copytool use.
830 * Always modified even if no lsm.
832 hpk.hpk_data_version = data_version;
834 /* File could have been stripped during archiving, so we need
837 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
838 (copy->hc_data_version != data_version)) {
839 CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. " DFID ", start:%#llx current:%#llx\n",
840 PFID(©->hc_hai.hai_fid),
841 copy->hc_data_version, data_version);
842 /* File was changed, send error to cdt. Do not ask for
843 * retry because if a file is modified frequently,
844 * the cdt will loop on retried archive requests.
845 * The policy engine will ask for a new archive later
846 * when the file will not be modified for some tunable
849 hpk.hpk_flags &= ~HP_FLAG_RETRY;
851 /* hpk_errval must be >= 0 */
852 hpk.hpk_errval = -rc;
857 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
860 return rc ? rc : rc2;
863 static int copy_and_ioctl(int cmd, struct obd_export *exp,
864 const void __user *data, size_t size)
869 copy = memdup_user(data, size);
871 return PTR_ERR(copy);
873 rc = obd_iocontrol(cmd, exp, size, copy, NULL);
879 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
881 int cmd = qctl->qc_cmd;
882 int type = qctl->qc_type;
883 int id = qctl->qc_id;
884 int valid = qctl->qc_valid;
890 if (!capable(CFS_CAP_SYS_ADMIN))
894 if (((type == USRQUOTA &&
895 !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
897 !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
898 !capable(CFS_CAP_SYS_ADMIN))
904 CERROR("unsupported quotactl op: %#x\n", cmd);
908 if (valid != QC_GENERAL) {
909 if (cmd == Q_GETINFO)
910 qctl->qc_cmd = Q_GETOINFO;
911 else if (cmd == Q_GETQUOTA)
912 qctl->qc_cmd = Q_GETOQUOTA;
918 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
919 sizeof(*qctl), qctl, NULL);
922 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
923 sizeof(*qctl), qctl, NULL);
926 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
927 sizeof(*qctl), qctl, NULL);
929 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
931 sizeof(*qctl), qctl, NULL);
943 struct obd_quotactl *oqctl;
945 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
949 QCTL_COPY(oqctl, qctl);
950 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
955 /* If QIF_SPACE is not set, client should collect the
956 * space usage from OSSs by itself
958 if (cmd == Q_GETQUOTA &&
959 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
960 !oqctl->qc_dqblk.dqb_curspace) {
961 struct obd_quotactl *oqctl_tmp;
963 oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
969 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
970 oqctl_tmp->qc_id = oqctl->qc_id;
971 oqctl_tmp->qc_type = oqctl->qc_type;
973 /* collect space usage from OSTs */
974 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
975 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
976 if (!rc || rc == -EREMOTEIO) {
977 oqctl->qc_dqblk.dqb_curspace =
978 oqctl_tmp->qc_dqblk.dqb_curspace;
979 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
982 /* collect space & inode usage from MDTs */
983 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
984 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
985 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
986 if (!rc || rc == -EREMOTEIO) {
987 oqctl->qc_dqblk.dqb_curspace +=
988 oqctl_tmp->qc_dqblk.dqb_curspace;
989 oqctl->qc_dqblk.dqb_curinodes =
990 oqctl_tmp->qc_dqblk.dqb_curinodes;
991 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
993 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
999 QCTL_COPY(qctl, oqctl);
1006 /* This function tries to get a single name component,
1007 * to send to the server. No actual path traversal involved,
1008 * so we limit to NAME_MAX
1010 static char *ll_getname(const char __user *filename)
1015 tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
1017 return ERR_PTR(-ENOMEM);
1019 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1024 else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
1025 ret = -ENAMETOOLONG;
1034 #define ll_putname(filename) kfree(filename)
1036 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1038 struct inode *inode = file_inode(file);
1039 struct ll_sb_info *sbi = ll_i2sbi(inode);
1040 struct obd_ioctl_data *data;
1043 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), cmd=%#x\n",
1044 PFID(ll_inode2fid(inode)), inode, cmd);
1046 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1047 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1050 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1052 case FSFILT_IOC_GETFLAGS:
1053 case FSFILT_IOC_SETFLAGS:
1054 return ll_iocontrol(inode, file, cmd, arg);
1055 case FSFILT_IOC_GETVERSION_OLD:
1056 case FSFILT_IOC_GETVERSION:
1057 return put_user(inode->i_generation, (int __user *)arg);
1058 /* We need to special case any other ioctls we want to handle,
1059 * to send them to the MDS/OST as appropriate and to properly
1060 * network encode the arg field.
1061 case FSFILT_IOC_SETVERSION_OLD:
1062 case FSFILT_IOC_SETVERSION:
1064 case LL_IOC_GET_MDTIDX: {
1067 mdtidx = ll_get_mdt_idx(inode);
1071 if (put_user((int)mdtidx, (int __user *)arg))
1076 case IOC_MDC_LOOKUP: {
1077 int namelen, len = 0;
1081 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1086 filename = data->ioc_inlbuf1;
1087 namelen = strlen(filename);
1090 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1095 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1097 CERROR("%s: lookup %.*s failed: rc = %d\n",
1098 ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1106 case LL_IOC_LMV_SETSTRIPE: {
1107 struct lmv_user_md *lum;
1116 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1121 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1122 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1127 filename = data->ioc_inlbuf1;
1128 namelen = data->ioc_inllen1;
1131 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1135 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1136 lumlen = data->ioc_inllen2;
1138 if (lum->lum_magic != LMV_USER_MAGIC ||
1139 lumlen != sizeof(*lum)) {
1140 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1141 filename, lum->lum_magic, lumlen, -EFAULT);
1146 #if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1147 mode = data->ioc_type != 0 ? data->ioc_type : 0777;
1149 mode = data->ioc_type;
1151 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1156 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1157 struct lmv_user_md __user *ulump;
1158 struct lmv_user_md lum;
1161 ulump = (struct lmv_user_md __user *)arg;
1162 if (copy_from_user(&lum, ulump, sizeof(lum)))
1165 if (lum.lum_magic != LMV_USER_MAGIC)
1168 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1172 case LL_IOC_LOV_SETSTRIPE: {
1173 struct lov_user_md_v3 lumv3;
1174 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1175 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1176 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1178 int set_default = 0;
1180 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1181 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1182 sizeof(lumv3p->lmm_objects[0]));
1183 /* first try with v1 which is smaller than v3 */
1184 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1187 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1188 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1192 if (is_root_inode(inode))
1195 /* in v1 and v3 cases lumv1 points to data */
1196 rc = ll_dir_setstripe(inode, lumv1, set_default);
1200 case LL_IOC_LMV_GETSTRIPE: {
1201 struct lmv_user_md __user *ulmv;
1202 struct lmv_user_md lum;
1203 struct ptlrpc_request *request = NULL;
1204 struct lmv_user_md *tmp = NULL;
1205 union lmv_mds_md *lmm = NULL;
1207 int max_stripe_count;
1215 ulmv = (struct lmv_user_md __user *)arg;
1216 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1219 max_stripe_count = lum.lum_stripe_count;
1221 * lum_magic will indicate which stripe the ioctl will like
1222 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1223 * is for default LMV stripe
1225 if (lum.lum_magic == LMV_MAGIC_V1)
1226 valid |= OBD_MD_MEA;
1227 else if (lum.lum_magic == LMV_USER_MAGIC)
1228 valid |= OBD_MD_DEFAULT_MEA;
1232 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1237 /* Get default LMV EA */
1238 if (lum.lum_magic == LMV_USER_MAGIC) {
1239 if (lmmsize > sizeof(*ulmv)) {
1244 if (copy_to_user(ulmv, lmm, lmmsize))
1250 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1251 if (max_stripe_count < stripe_count) {
1252 lum.lum_stripe_count = stripe_count;
1253 if (copy_to_user(ulmv, &lum, sizeof(lum))) {
1261 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1262 tmp = kzalloc(lum_size, GFP_NOFS);
1268 mdt_index = ll_get_mdt_idx(inode);
1269 if (mdt_index < 0) {
1273 tmp->lum_magic = LMV_MAGIC_V1;
1274 tmp->lum_stripe_count = 0;
1275 tmp->lum_stripe_offset = mdt_index;
1276 for (i = 0; i < stripe_count; i++) {
1279 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1280 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1281 if (mdt_index < 0) {
1285 tmp->lum_objects[i].lum_mds = mdt_index;
1286 tmp->lum_objects[i].lum_fid = fid;
1287 tmp->lum_stripe_count++;
1290 if (copy_to_user(ulmv, tmp, lum_size)) {
1297 ptlrpc_req_finished(request);
1301 case LL_IOC_LOV_SWAP_LAYOUTS:
1303 case IOC_OBD_STATFS:
1304 return ll_obd_statfs(inode, (void __user *)arg);
1305 case LL_IOC_LOV_GETSTRIPE:
1306 case LL_IOC_MDC_GETINFO:
1307 case IOC_MDC_GETFILEINFO:
1308 case IOC_MDC_GETFILESTRIPE: {
1309 struct ptlrpc_request *request = NULL;
1310 struct lov_user_md __user *lump;
1311 struct lov_mds_md *lmm = NULL;
1312 struct mdt_body *body;
1313 char *filename = NULL;
1316 if (cmd == IOC_MDC_GETFILEINFO ||
1317 cmd == IOC_MDC_GETFILESTRIPE) {
1318 filename = ll_getname((const char __user *)arg);
1319 if (IS_ERR(filename))
1320 return PTR_ERR(filename);
1322 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1323 &lmmsize, &request);
1325 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1330 body = req_capsule_server_get(&request->rq_pill,
1338 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1339 cmd == LL_IOC_MDC_GETINFO)) {
1347 if (cmd == IOC_MDC_GETFILESTRIPE ||
1348 cmd == LL_IOC_LOV_GETSTRIPE) {
1349 lump = (struct lov_user_md __user *)arg;
1351 struct lov_user_mds_data __user *lmdp;
1353 lmdp = (struct lov_user_mds_data __user *)arg;
1354 lump = &lmdp->lmd_lmm;
1356 if (copy_to_user(lump, lmm, lmmsize)) {
1357 if (copy_to_user(lump, lmm, sizeof(*lump))) {
1364 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1365 struct lov_user_mds_data __user *lmdp;
1368 st.st_dev = inode->i_sb->s_dev;
1369 st.st_mode = body->mbo_mode;
1370 st.st_nlink = body->mbo_nlink;
1371 st.st_uid = body->mbo_uid;
1372 st.st_gid = body->mbo_gid;
1373 st.st_rdev = body->mbo_rdev;
1374 st.st_size = body->mbo_size;
1375 st.st_blksize = PAGE_SIZE;
1376 st.st_blocks = body->mbo_blocks;
1377 st.st_atime = body->mbo_atime;
1378 st.st_mtime = body->mbo_mtime;
1379 st.st_ctime = body->mbo_ctime;
1380 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1384 lmdp = (struct lov_user_mds_data __user *)arg;
1385 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1392 ptlrpc_req_finished(request);
1394 ll_putname(filename);
1397 case OBD_IOC_QUOTACTL: {
1398 struct if_quotactl *qctl;
1400 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
1404 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
1409 rc = quotactl_ioctl(sbi, qctl);
1411 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1419 case OBD_IOC_GETDTNAME:
1420 case OBD_IOC_GETMDNAME:
1421 return ll_get_obd_name(inode, cmd, arg);
1422 case LL_IOC_FLUSHCTX:
1423 return ll_flush_ctx(inode);
1424 case LL_IOC_GETOBDCOUNT: {
1426 struct obd_export *exp;
1428 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1431 /* get ost count when count is zero, get mdt count otherwise */
1432 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1433 vallen = sizeof(count);
1434 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1435 KEY_TGT_COUNT, &vallen, &count);
1437 CERROR("get target count failed: %d\n", rc);
1441 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1446 case LL_IOC_PATH2FID:
1447 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1448 sizeof(struct lu_fid)))
1451 case LL_IOC_GET_CONNECT_FLAGS: {
1452 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1453 (void __user *)arg);
1455 case OBD_IOC_CHANGELOG_SEND:
1456 case OBD_IOC_CHANGELOG_CLEAR:
1457 if (!capable(CFS_CAP_SYS_ADMIN))
1460 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1461 sizeof(struct ioc_changelog));
1463 case OBD_IOC_FID2PATH:
1464 return ll_fid2path(inode, (void __user *)arg);
1465 case LL_IOC_GETPARENT:
1466 return ll_getparent(file, (void __user *)arg);
1467 case LL_IOC_FID2MDTIDX: {
1468 struct obd_export *exp = ll_i2mdexp(inode);
1472 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1476 /* Call mdc_iocontrol */
1477 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1484 case LL_IOC_HSM_REQUEST: {
1485 struct hsm_user_request *hur;
1488 hur = memdup_user((void __user *)arg, sizeof(*hur));
1490 return PTR_ERR(hur);
1492 /* Compute the whole struct size */
1493 totalsize = hur_len(hur);
1498 /* Final size will be more than double totalsize */
1499 if (totalsize >= MDS_MAXREQSIZE / 3)
1502 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
1506 /* Copy the whole struct */
1507 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
1512 if (hur->hur_request.hr_action == HUA_RELEASE) {
1513 const struct lu_fid *fid;
1517 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1518 fid = &hur->hur_user_item[i].hui_fid;
1519 f = search_inode_for_lustre(inode->i_sb, fid);
1525 rc = ll_hsm_release(f);
1531 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1539 case LL_IOC_HSM_PROGRESS: {
1540 struct hsm_progress_kernel hpk;
1541 struct hsm_progress hp;
1543 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1546 hpk.hpk_fid = hp.hp_fid;
1547 hpk.hpk_cookie = hp.hp_cookie;
1548 hpk.hpk_extent = hp.hp_extent;
1549 hpk.hpk_flags = hp.hp_flags;
1550 hpk.hpk_errval = hp.hp_errval;
1551 hpk.hpk_data_version = 0;
1553 /* File may not exist in Lustre; all progress
1554 * reported to Lustre root
1556 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1560 case LL_IOC_HSM_CT_START:
1561 if (!capable(CFS_CAP_SYS_ADMIN))
1564 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1565 sizeof(struct lustre_kernelcomm));
1568 case LL_IOC_HSM_COPY_START: {
1569 struct hsm_copy *copy;
1572 copy = memdup_user((char __user *)arg, sizeof(*copy));
1574 return PTR_ERR(copy);
1576 rc = ll_ioc_copy_start(inode->i_sb, copy);
1577 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1583 case LL_IOC_HSM_COPY_END: {
1584 struct hsm_copy *copy;
1587 copy = memdup_user((char __user *)arg, sizeof(*copy));
1589 return PTR_ERR(copy);
1591 rc = ll_ioc_copy_end(inode->i_sb, copy);
1592 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1598 case LL_IOC_MIGRATE: {
1600 const char *filename;
1606 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1610 data = (struct obd_ioctl_data *)buf;
1611 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1612 !data->ioc_inllen1 || !data->ioc_inllen2) {
1617 filename = data->ioc_inlbuf1;
1618 namelen = data->ioc_inllen1;
1619 if (namelen < 1 || namelen != strlen(filename) + 1) {
1624 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1628 mdtidx = *(int *)data->ioc_inlbuf2;
1630 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1638 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1639 (void __user *)arg);
1643 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1645 struct inode *inode = file->f_mapping->host;
1646 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1647 struct ll_sb_info *sbi = ll_i2sbi(inode);
1648 int api32 = ll_need_32bit_api(sbi);
1649 loff_t ret = -EINVAL;
1655 offset += file->f_pos;
1661 offset += LL_DIR_END_OFF_32BIT;
1663 offset += LL_DIR_END_OFF;
1670 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1671 (!api32 && offset <= LL_DIR_END_OFF))) {
1672 if (offset != file->f_pos) {
1673 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1674 (!api32 && offset == LL_DIR_END_OFF))
1675 fd->lfd_pos = MDS_DIR_END_OFF;
1676 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1677 fd->lfd_pos = offset << 32;
1679 fd->lfd_pos = offset;
1680 file->f_pos = offset;
1681 file->f_version = 0;
1691 static int ll_dir_open(struct inode *inode, struct file *file)
1693 return ll_file_open(inode, file);
1696 static int ll_dir_release(struct inode *inode, struct file *file)
1698 return ll_file_release(inode, file);
1701 const struct file_operations ll_dir_operations = {
1702 .llseek = ll_dir_seek,
1703 .open = ll_dir_open,
1704 .release = ll_dir_release,
1705 .read = generic_read_dir,
1706 .iterate_shared = ll_readdir,
1707 .unlocked_ioctl = ll_dir_ioctl,