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);
374 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
379 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
381 struct mgs_send_param *msp;
384 msp = kzalloc(sizeof(*msp), GFP_NOFS);
388 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
389 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
390 sizeof(struct mgs_send_param), msp, NULL);
392 CERROR("Failed to set parameter: %d\n", rc);
399 * Create striped directory with specified stripe(@lump)
401 * param[in] parent the parent of the directory.
402 * param[in] lump the specified stripes.
403 * param[in] dirname the name of the directory.
404 * param[in] mode the specified mode of the directory.
406 * retval =0 if striped directory is being created successfully.
407 * <0 if the creation is failed.
409 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
410 const char *dirname, umode_t mode)
412 struct ptlrpc_request *request = NULL;
413 struct md_op_data *op_data;
414 struct ll_sb_info *sbi = ll_i2sbi(parent);
415 struct inode *inode = NULL;
416 struct dentry dentry;
419 if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
422 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) name %s stripe_offset %d, stripe_count: %u\n",
423 PFID(ll_inode2fid(parent)), parent, dirname,
424 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
426 if (lump->lum_stripe_count > 1 &&
427 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
430 if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
431 lustre_swab_lmv_user_md(lump);
433 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
434 mode &= ~current_umask();
435 mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
436 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
437 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
439 if (IS_ERR(op_data)) {
440 err = PTR_ERR(op_data);
444 op_data->op_cli_flags |= CLI_SET_MEA;
445 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
446 from_kuid(&init_user_ns, current_fsuid()),
447 from_kgid(&init_user_ns, current_fsgid()),
448 cfs_curproc_cap_pack(), 0, &request);
449 ll_finish_md_op_data(op_data);
451 err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
455 memset(&dentry, 0, sizeof(dentry));
456 dentry.d_inode = inode;
458 err = ll_init_security(&dentry, inode, parent);
462 ptlrpc_req_finished(request);
466 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
469 struct ll_sb_info *sbi = ll_i2sbi(inode);
470 struct md_op_data *op_data;
471 struct ptlrpc_request *req = NULL;
473 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
474 struct obd_device *mgc = lsi->lsi_mgc;
479 * This is coming from userspace, so should be in
480 * local endian. But the MDS would like it in little
481 * endian, so we swab it before we send it.
483 switch (lump->lmm_magic) {
484 case LOV_USER_MAGIC_V1: {
485 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
486 lustre_swab_lov_user_md_v1(lump);
487 lum_size = sizeof(struct lov_user_md_v1);
490 case LOV_USER_MAGIC_V3: {
491 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
492 lustre_swab_lov_user_md_v3(
493 (struct lov_user_md_v3 *)lump);
494 lum_size = sizeof(struct lov_user_md_v3);
497 case LMV_USER_MAGIC: {
498 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
499 lustre_swab_lmv_user_md(
500 (struct lmv_user_md *)lump);
501 lum_size = sizeof(struct lmv_user_md);
506 "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
507 lump->lmm_magic, LOV_USER_MAGIC_V1,
513 lum_size = sizeof(struct lov_user_md_v1);
516 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
517 LUSTRE_OPC_ANY, NULL);
519 return PTR_ERR(op_data);
521 /* swabbing is done in lov_setstripe() on server side */
522 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
523 ll_finish_md_op_data(op_data);
524 ptlrpc_req_finished(req);
528 #if OBD_OCD_VERSION(2, 13, 53, 0) > LUSTRE_VERSION_CODE
530 * 2.9 server has stored filesystem default stripe in ROOT xattr,
531 * and it's stored into system config for backward compatibility.
533 * In the following we use the fact that LOV_USER_MAGIC_V1 and
534 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
535 * need to make the distinction between the 2 versions
537 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
541 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
546 /* Get fsname and assume devname to be -MDT0000. */
547 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
548 strcat(buf, "-MDT0000.lov");
551 /* Set root stripesize */
552 sprintf(buf, ".stripesize=%u",
553 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
554 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
558 /* Set root stripecount */
559 sprintf(buf, ".stripecount=%hd",
560 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
561 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
565 /* Set root stripeoffset */
566 sprintf(buf, ".stripeoffset=%hd",
567 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
568 (typeof(lump->lmm_stripe_offset))(-1));
569 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
579 * This function will be used to get default LOV/LMV/Default LMV
580 * @valid will be used to indicate which stripe it will retrieve
581 * OBD_MD_MEA LMV stripe EA
582 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
583 * otherwise Default LOV EA.
584 * Each time, it can only retrieve 1 stripe EA
586 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
587 struct ptlrpc_request **request, u64 valid)
589 struct ll_sb_info *sbi = ll_i2sbi(inode);
590 struct mdt_body *body;
591 struct lov_mds_md *lmm = NULL;
592 struct ptlrpc_request *req = NULL;
594 struct md_op_data *op_data;
596 rc = ll_get_max_mdsize(sbi, &lmmsize);
600 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
601 0, lmmsize, LUSTRE_OPC_ANY,
604 return PTR_ERR(op_data);
606 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
607 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
608 ll_finish_md_op_data(op_data);
610 CDEBUG(D_INFO, "md_getattr failed on inode " DFID ": rc %d\n",
611 PFID(ll_inode2fid(inode)), rc);
615 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
617 lmmsize = body->mbo_eadatasize;
619 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
625 lmm = req_capsule_server_sized_get(&req->rq_pill,
626 &RMF_MDT_MD, lmmsize);
630 * This is coming from the MDS, so is probably in
631 * little endian. We convert it to host endian before
632 * passing it to userspace.
634 /* We don't swab objects for directories */
635 switch (le32_to_cpu(lmm->lmm_magic)) {
637 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
638 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
641 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
642 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
645 if (cpu_to_le32(LMV_MAGIC) != LMV_MAGIC)
646 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
649 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
650 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
653 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
658 *plmm_size = lmmsize;
663 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
665 struct md_op_data *op_data;
668 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
672 op_data->op_flags |= MF_GET_MDT_IDX;
673 op_data->op_fid1 = *fid;
674 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
675 mdt_index = op_data->op_mds;
684 * Get MDT index for the inode.
686 int ll_get_mdt_idx(struct inode *inode)
688 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
692 * Generic handler to do any pre-copy work.
694 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
695 * first information for it that real work has started.
697 * Moreover, for a ARCHIVE request, it will sample the file data version and
698 * store it in \a copy.
700 * \return 0 on success.
702 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
704 struct ll_sb_info *sbi = ll_s2sbi(sb);
705 struct hsm_progress_kernel hpk;
708 /* Forge a hsm_progress based on data from copy. */
709 hpk.hpk_fid = copy->hc_hai.hai_fid;
710 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
711 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
712 hpk.hpk_extent.length = 0;
715 hpk.hpk_data_version = 0;
717 /* For archive request, we need to read the current file version. */
718 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
720 __u64 data_version = 0;
722 /* Get inode for this fid */
723 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
725 hpk.hpk_flags |= HP_FLAG_RETRY;
726 /* hpk_errval is >= 0 */
727 hpk.hpk_errval = -PTR_ERR(inode);
732 /* Read current file data version */
733 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
737 "Could not read file data version of " DFID " (rc = %d). Archive request (%#llx) could not be done.\n",
738 PFID(©->hc_hai.hai_fid), rc,
739 copy->hc_hai.hai_cookie);
740 hpk.hpk_flags |= HP_FLAG_RETRY;
741 /* hpk_errval must be >= 0 */
742 hpk.hpk_errval = -rc;
746 /* Store in the hsm_copy for later copytool use.
747 * Always modified even if no lsm.
749 copy->hc_data_version = data_version;
753 /* On error, the request should be considered as completed */
754 if (hpk.hpk_errval > 0)
755 hpk.hpk_flags |= HP_FLAG_COMPLETED;
756 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
759 return rc ? rc : rc2;
763 * Generic handler to do any post-copy work.
765 * It will send the last hsm_progress update to coordinator to inform it
766 * that copy is finished and whether it was successful or not.
769 * - for ARCHIVE request, it will sample the file data version and compare it
770 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
771 * will be considered as failed.
772 * - for RESTORE request, it will sample the file data version and send it to
773 * coordinator which is useful if the file was imported as 'released'.
775 * \return 0 on success.
777 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
779 struct ll_sb_info *sbi = ll_s2sbi(sb);
780 struct hsm_progress_kernel hpk;
783 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
784 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
785 * initialized if copy_end was called with copy == NULL.
788 /* Forge a hsm_progress based on data from copy. */
789 hpk.hpk_fid = copy->hc_hai.hai_fid;
790 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
791 hpk.hpk_extent = copy->hc_hai.hai_extent;
792 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
793 hpk.hpk_errval = copy->hc_errval;
794 hpk.hpk_data_version = 0;
796 /* For archive request, we need to check the file data was not changed.
798 * For restore request, we need to send the file data version, this is
799 * useful when the file was created using hsm_import.
801 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
802 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
803 (copy->hc_errval == 0)) {
805 __u64 data_version = 0;
807 /* Get lsm for this fid */
808 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
810 hpk.hpk_flags |= HP_FLAG_RETRY;
811 /* hpk_errval must be >= 0 */
812 hpk.hpk_errval = -PTR_ERR(inode);
817 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
821 "Could not read file data version. Request could not be confirmed.\n");
822 if (hpk.hpk_errval == 0)
823 hpk.hpk_errval = -rc;
827 /* Store in the hsm_copy for later copytool use.
828 * Always modified even if no lsm.
830 hpk.hpk_data_version = data_version;
832 /* File could have been stripped during archiving, so we need
835 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
836 (copy->hc_data_version != data_version)) {
837 CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. " DFID ", start:%#llx current:%#llx\n",
838 PFID(©->hc_hai.hai_fid),
839 copy->hc_data_version, data_version);
840 /* File was changed, send error to cdt. Do not ask for
841 * retry because if a file is modified frequently,
842 * the cdt will loop on retried archive requests.
843 * The policy engine will ask for a new archive later
844 * when the file will not be modified for some tunable
847 hpk.hpk_flags &= ~HP_FLAG_RETRY;
849 /* hpk_errval must be >= 0 */
850 hpk.hpk_errval = -rc;
855 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
858 return rc ? rc : rc2;
861 static int copy_and_ioctl(int cmd, struct obd_export *exp,
862 const void __user *data, size_t size)
867 copy = memdup_user(data, size);
869 return PTR_ERR(copy);
871 rc = obd_iocontrol(cmd, exp, size, copy, NULL);
877 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
879 int cmd = qctl->qc_cmd;
880 int type = qctl->qc_type;
881 int id = qctl->qc_id;
882 int valid = qctl->qc_valid;
888 if (!capable(CFS_CAP_SYS_ADMIN))
892 if (((type == USRQUOTA &&
893 !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
895 !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
896 !capable(CFS_CAP_SYS_ADMIN))
902 CERROR("unsupported quotactl op: %#x\n", cmd);
906 if (valid != QC_GENERAL) {
907 if (cmd == Q_GETINFO)
908 qctl->qc_cmd = Q_GETOINFO;
909 else if (cmd == Q_GETQUOTA)
910 qctl->qc_cmd = Q_GETOQUOTA;
916 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
917 sizeof(*qctl), qctl, NULL);
920 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
921 sizeof(*qctl), qctl, NULL);
924 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
925 sizeof(*qctl), qctl, NULL);
927 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
929 sizeof(*qctl), qctl, NULL);
941 struct obd_quotactl *oqctl;
943 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
947 QCTL_COPY(oqctl, qctl);
948 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
953 /* If QIF_SPACE is not set, client should collect the
954 * space usage from OSSs by itself
956 if (cmd == Q_GETQUOTA &&
957 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
958 !oqctl->qc_dqblk.dqb_curspace) {
959 struct obd_quotactl *oqctl_tmp;
961 oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
967 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
968 oqctl_tmp->qc_id = oqctl->qc_id;
969 oqctl_tmp->qc_type = oqctl->qc_type;
971 /* collect space usage from OSTs */
972 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
973 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
974 if (!rc || rc == -EREMOTEIO) {
975 oqctl->qc_dqblk.dqb_curspace =
976 oqctl_tmp->qc_dqblk.dqb_curspace;
977 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
980 /* collect space & inode usage from MDTs */
981 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
982 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
983 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
984 if (!rc || rc == -EREMOTEIO) {
985 oqctl->qc_dqblk.dqb_curspace +=
986 oqctl_tmp->qc_dqblk.dqb_curspace;
987 oqctl->qc_dqblk.dqb_curinodes =
988 oqctl_tmp->qc_dqblk.dqb_curinodes;
989 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
991 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
997 QCTL_COPY(qctl, oqctl);
1004 /* This function tries to get a single name component,
1005 * to send to the server. No actual path traversal involved,
1006 * so we limit to NAME_MAX
1008 static char *ll_getname(const char __user *filename)
1013 tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
1015 return ERR_PTR(-ENOMEM);
1017 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1022 else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
1023 ret = -ENAMETOOLONG;
1032 #define ll_putname(filename) kfree(filename)
1034 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1036 struct inode *inode = file_inode(file);
1037 struct ll_sb_info *sbi = ll_i2sbi(inode);
1038 struct obd_ioctl_data *data;
1041 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), cmd=%#x\n",
1042 PFID(ll_inode2fid(inode)), inode, cmd);
1044 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1045 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1048 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1050 case FSFILT_IOC_GETFLAGS:
1051 case FSFILT_IOC_SETFLAGS:
1052 return ll_iocontrol(inode, file, cmd, arg);
1053 case FSFILT_IOC_GETVERSION_OLD:
1054 case FSFILT_IOC_GETVERSION:
1055 return put_user(inode->i_generation, (int __user *)arg);
1056 /* We need to special case any other ioctls we want to handle,
1057 * to send them to the MDS/OST as appropriate and to properly
1058 * network encode the arg field.
1059 case FSFILT_IOC_SETVERSION_OLD:
1060 case FSFILT_IOC_SETVERSION:
1062 case LL_IOC_GET_MDTIDX: {
1065 mdtidx = ll_get_mdt_idx(inode);
1069 if (put_user((int)mdtidx, (int __user *)arg))
1074 case IOC_MDC_LOOKUP: {
1075 int namelen, len = 0;
1079 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1084 filename = data->ioc_inlbuf1;
1085 namelen = strlen(filename);
1088 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1093 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1095 CERROR("%s: lookup %.*s failed: rc = %d\n",
1096 ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1104 case LL_IOC_LMV_SETSTRIPE: {
1105 struct lmv_user_md *lum;
1114 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1119 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1120 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1125 filename = data->ioc_inlbuf1;
1126 namelen = data->ioc_inllen1;
1129 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1133 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1134 lumlen = data->ioc_inllen2;
1136 if (lum->lum_magic != LMV_USER_MAGIC ||
1137 lumlen != sizeof(*lum)) {
1138 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1139 filename, lum->lum_magic, lumlen, -EFAULT);
1144 #if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1145 mode = data->ioc_type != 0 ? data->ioc_type : 0777;
1147 mode = data->ioc_type;
1149 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1154 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1155 struct lmv_user_md __user *ulump;
1156 struct lmv_user_md lum;
1159 ulump = (struct lmv_user_md __user *)arg;
1160 if (copy_from_user(&lum, ulump, sizeof(lum)))
1163 if (lum.lum_magic != LMV_USER_MAGIC)
1166 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1170 case LL_IOC_LOV_SETSTRIPE: {
1171 struct lov_user_md_v3 lumv3;
1172 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1173 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1174 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1176 int set_default = 0;
1178 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1179 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1180 sizeof(lumv3p->lmm_objects[0]));
1181 /* first try with v1 which is smaller than v3 */
1182 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1185 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1186 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1190 if (is_root_inode(inode))
1193 /* in v1 and v3 cases lumv1 points to data */
1194 rc = ll_dir_setstripe(inode, lumv1, set_default);
1198 case LL_IOC_LMV_GETSTRIPE: {
1199 struct lmv_user_md __user *ulmv;
1200 struct lmv_user_md lum;
1201 struct ptlrpc_request *request = NULL;
1202 struct lmv_user_md *tmp = NULL;
1203 union lmv_mds_md *lmm = NULL;
1205 int max_stripe_count;
1213 ulmv = (struct lmv_user_md __user *)arg;
1214 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1217 max_stripe_count = lum.lum_stripe_count;
1219 * lum_magic will indicate which stripe the ioctl will like
1220 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1221 * is for default LMV stripe
1223 if (lum.lum_magic == LMV_MAGIC_V1)
1224 valid |= OBD_MD_MEA;
1225 else if (lum.lum_magic == LMV_USER_MAGIC)
1226 valid |= OBD_MD_DEFAULT_MEA;
1230 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1235 /* Get default LMV EA */
1236 if (lum.lum_magic == LMV_USER_MAGIC) {
1237 if (lmmsize > sizeof(*ulmv)) {
1242 if (copy_to_user(ulmv, lmm, lmmsize))
1248 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1249 if (max_stripe_count < stripe_count) {
1250 lum.lum_stripe_count = stripe_count;
1251 if (copy_to_user(ulmv, &lum, sizeof(lum))) {
1259 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1260 tmp = kzalloc(lum_size, GFP_NOFS);
1266 mdt_index = ll_get_mdt_idx(inode);
1267 if (mdt_index < 0) {
1271 tmp->lum_magic = LMV_MAGIC_V1;
1272 tmp->lum_stripe_count = 0;
1273 tmp->lum_stripe_offset = mdt_index;
1274 for (i = 0; i < stripe_count; i++) {
1277 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1278 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1279 if (mdt_index < 0) {
1283 tmp->lum_objects[i].lum_mds = mdt_index;
1284 tmp->lum_objects[i].lum_fid = fid;
1285 tmp->lum_stripe_count++;
1288 if (copy_to_user(ulmv, tmp, lum_size)) {
1295 ptlrpc_req_finished(request);
1299 case LL_IOC_LOV_SWAP_LAYOUTS:
1301 case IOC_OBD_STATFS:
1302 return ll_obd_statfs(inode, (void __user *)arg);
1303 case LL_IOC_LOV_GETSTRIPE:
1304 case LL_IOC_MDC_GETINFO:
1305 case IOC_MDC_GETFILEINFO:
1306 case IOC_MDC_GETFILESTRIPE: {
1307 struct ptlrpc_request *request = NULL;
1308 struct lov_user_md __user *lump;
1309 struct lov_mds_md *lmm = NULL;
1310 struct mdt_body *body;
1311 char *filename = NULL;
1314 if (cmd == IOC_MDC_GETFILEINFO ||
1315 cmd == IOC_MDC_GETFILESTRIPE) {
1316 filename = ll_getname((const char __user *)arg);
1317 if (IS_ERR(filename))
1318 return PTR_ERR(filename);
1320 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1321 &lmmsize, &request);
1323 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1328 body = req_capsule_server_get(&request->rq_pill,
1336 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1337 cmd == LL_IOC_MDC_GETINFO)) {
1345 if (cmd == IOC_MDC_GETFILESTRIPE ||
1346 cmd == LL_IOC_LOV_GETSTRIPE) {
1347 lump = (struct lov_user_md __user *)arg;
1349 struct lov_user_mds_data __user *lmdp;
1351 lmdp = (struct lov_user_mds_data __user *)arg;
1352 lump = &lmdp->lmd_lmm;
1354 if (copy_to_user(lump, lmm, lmmsize)) {
1355 if (copy_to_user(lump, lmm, sizeof(*lump))) {
1362 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1363 struct lov_user_mds_data __user *lmdp;
1366 st.st_dev = inode->i_sb->s_dev;
1367 st.st_mode = body->mbo_mode;
1368 st.st_nlink = body->mbo_nlink;
1369 st.st_uid = body->mbo_uid;
1370 st.st_gid = body->mbo_gid;
1371 st.st_rdev = body->mbo_rdev;
1372 st.st_size = body->mbo_size;
1373 st.st_blksize = PAGE_SIZE;
1374 st.st_blocks = body->mbo_blocks;
1375 st.st_atime = body->mbo_atime;
1376 st.st_mtime = body->mbo_mtime;
1377 st.st_ctime = body->mbo_ctime;
1378 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1382 lmdp = (struct lov_user_mds_data __user *)arg;
1383 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1390 ptlrpc_req_finished(request);
1392 ll_putname(filename);
1395 case OBD_IOC_QUOTACTL: {
1396 struct if_quotactl *qctl;
1398 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
1402 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
1407 rc = quotactl_ioctl(sbi, qctl);
1409 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1417 case OBD_IOC_GETDTNAME:
1418 case OBD_IOC_GETMDNAME:
1419 return ll_get_obd_name(inode, cmd, arg);
1420 case LL_IOC_FLUSHCTX:
1421 return ll_flush_ctx(inode);
1422 case LL_IOC_GETOBDCOUNT: {
1424 struct obd_export *exp;
1426 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1429 /* get ost count when count is zero, get mdt count otherwise */
1430 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1431 vallen = sizeof(count);
1432 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1433 KEY_TGT_COUNT, &vallen, &count);
1435 CERROR("get target count failed: %d\n", rc);
1439 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1444 case LL_IOC_PATH2FID:
1445 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1446 sizeof(struct lu_fid)))
1449 case LL_IOC_GET_CONNECT_FLAGS: {
1450 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1451 (void __user *)arg);
1453 case OBD_IOC_CHANGELOG_SEND:
1454 case OBD_IOC_CHANGELOG_CLEAR:
1455 if (!capable(CFS_CAP_SYS_ADMIN))
1458 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1459 sizeof(struct ioc_changelog));
1461 case OBD_IOC_FID2PATH:
1462 return ll_fid2path(inode, (void __user *)arg);
1463 case LL_IOC_GETPARENT:
1464 return ll_getparent(file, (void __user *)arg);
1465 case LL_IOC_FID2MDTIDX: {
1466 struct obd_export *exp = ll_i2mdexp(inode);
1470 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1474 /* Call mdc_iocontrol */
1475 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1482 case LL_IOC_HSM_REQUEST: {
1483 struct hsm_user_request *hur;
1486 hur = memdup_user((void __user *)arg, sizeof(*hur));
1488 return PTR_ERR(hur);
1490 /* Compute the whole struct size */
1491 totalsize = hur_len(hur);
1496 /* Final size will be more than double totalsize */
1497 if (totalsize >= MDS_MAXREQSIZE / 3)
1500 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
1504 /* Copy the whole struct */
1505 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
1510 if (hur->hur_request.hr_action == HUA_RELEASE) {
1511 const struct lu_fid *fid;
1515 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1516 fid = &hur->hur_user_item[i].hui_fid;
1517 f = search_inode_for_lustre(inode->i_sb, fid);
1523 rc = ll_hsm_release(f);
1529 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1537 case LL_IOC_HSM_PROGRESS: {
1538 struct hsm_progress_kernel hpk;
1539 struct hsm_progress hp;
1541 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1544 hpk.hpk_fid = hp.hp_fid;
1545 hpk.hpk_cookie = hp.hp_cookie;
1546 hpk.hpk_extent = hp.hp_extent;
1547 hpk.hpk_flags = hp.hp_flags;
1548 hpk.hpk_errval = hp.hp_errval;
1549 hpk.hpk_data_version = 0;
1551 /* File may not exist in Lustre; all progress
1552 * reported to Lustre root
1554 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1558 case LL_IOC_HSM_CT_START:
1559 if (!capable(CFS_CAP_SYS_ADMIN))
1562 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1563 sizeof(struct lustre_kernelcomm));
1566 case LL_IOC_HSM_COPY_START: {
1567 struct hsm_copy *copy;
1570 copy = memdup_user((char __user *)arg, sizeof(*copy));
1572 return PTR_ERR(copy);
1574 rc = ll_ioc_copy_start(inode->i_sb, copy);
1575 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1581 case LL_IOC_HSM_COPY_END: {
1582 struct hsm_copy *copy;
1585 copy = memdup_user((char __user *)arg, sizeof(*copy));
1587 return PTR_ERR(copy);
1589 rc = ll_ioc_copy_end(inode->i_sb, copy);
1590 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1596 case LL_IOC_MIGRATE: {
1598 const char *filename;
1604 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1608 data = (struct obd_ioctl_data *)buf;
1609 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1610 !data->ioc_inllen1 || !data->ioc_inllen2) {
1615 filename = data->ioc_inlbuf1;
1616 namelen = data->ioc_inllen1;
1617 if (namelen < 1 || namelen != strlen(filename) + 1) {
1622 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1626 mdtidx = *(int *)data->ioc_inlbuf2;
1628 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1636 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1637 (void __user *)arg);
1641 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1643 struct inode *inode = file->f_mapping->host;
1644 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1645 struct ll_sb_info *sbi = ll_i2sbi(inode);
1646 int api32 = ll_need_32bit_api(sbi);
1647 loff_t ret = -EINVAL;
1653 offset += file->f_pos;
1659 offset += LL_DIR_END_OFF_32BIT;
1661 offset += LL_DIR_END_OFF;
1668 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1669 (!api32 && offset <= LL_DIR_END_OFF))) {
1670 if (offset != file->f_pos) {
1671 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1672 (!api32 && offset == LL_DIR_END_OFF))
1673 fd->lfd_pos = MDS_DIR_END_OFF;
1674 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1675 fd->lfd_pos = offset << 32;
1677 fd->lfd_pos = offset;
1678 file->f_pos = offset;
1688 static int ll_dir_open(struct inode *inode, struct file *file)
1690 return ll_file_open(inode, file);
1693 static int ll_dir_release(struct inode *inode, struct file *file)
1695 return ll_file_release(inode, file);
1698 const struct file_operations ll_dir_operations = {
1699 .llseek = ll_dir_seek,
1700 .open = ll_dir_open,
1701 .release = ll_dir_release,
1702 .read = generic_read_dir,
1703 .iterate_shared = ll_readdir,
1704 .unlocked_ioctl = ll_dir_ioctl,