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, 2015, 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/uaccess.h>
41 #include <linux/buffer_head.h> /* for wait_on_buffer */
42 #include <linux/pagevec.h>
43 #include <linux/prefetch.h>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include <uapi/linux/lustre/lustre_ioctl.h>
50 #include <lustre_lib.h>
51 #include <lustre_dlm.h>
52 #include <lustre_fid.h>
53 #include <lustre_kernelcomm.h>
54 #include <lustre_swab.h>
56 #include "llite_internal.h"
59 * (new) readdir implementation overview.
61 * Original lustre readdir implementation cached exact copy of raw directory
62 * pages on the client. These pages were indexed in client page cache by
63 * logical offset in the directory file. This design, while very simple and
64 * intuitive had some inherent problems:
66 * . it implies that byte offset to the directory entry serves as a
67 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
68 * ext3/htree directory entries may move due to splits, and more
71 * . it is incompatible with the design of split directories for cmd3,
72 * that assumes that names are distributed across nodes based on their
73 * hash, and so readdir should be done in hash order.
75 * New readdir implementation does readdir in hash order, and uses hash of a
76 * file name as a telldir/seekdir cookie. This led to number of complications:
78 * . hash is not unique, so it cannot be used to index cached directory
79 * pages on the client (note, that it requires a whole pageful of hash
80 * collided entries to cause two pages to have identical hashes);
82 * . hash is not unique, so it cannot, strictly speaking, be used as an
83 * entry cookie. ext3/htree has the same problem and lustre implementation
84 * mimics their solution: seekdir(hash) positions directory at the first
85 * entry with the given hash.
91 * Client caches directory pages using hash of the first entry as an index. As
92 * noted above hash is not unique, so this solution doesn't work as is:
93 * special processing is needed for "page hash chains" (i.e., sequences of
94 * pages filled with entries all having the same hash value).
96 * First, such chains have to be detected. To this end, server returns to the
97 * client the hash of the first entry on the page next to one returned. When
98 * client detects that this hash is the same as hash of the first entry on the
99 * returned page, page hash collision has to be handled. Pages in the
100 * hash chain, except first one, are termed "overflow pages".
102 * Solution to index uniqueness problem is to not cache overflow
103 * pages. Instead, when page hash collision is detected, all overflow pages
104 * from emerging chain are immediately requested from the server and placed in
105 * a special data structure (struct ll_dir_chain). This data structure is used
106 * by ll_readdir() to process entries from overflow pages. When readdir
107 * invocation finishes, overflow pages are discarded. If page hash collision
108 * chain weren't completely processed, next call to readdir will again detect
109 * page hash collision, again read overflow pages in, process next portion of
110 * entries and again discard the pages. This is not as wasteful as it looks,
111 * because, given reasonable hash, page hash collisions are extremely rare.
113 * 1. directory positioning
115 * When seekdir(hash) is called, original
126 * identification of and access to overflow pages
130 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
131 * a header lu_dirpage which describes the start/end hash, and whether this
132 * page is empty (contains no dir entry) or hash collide with next page.
133 * After client receives reply, several pages will be integrated into dir page
134 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the lu_dirpage
135 * for this integrated page will be adjusted. See lmv_adjust_dirpages().
138 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
141 struct md_callback cb_op;
145 cb_op.md_blocking_ast = ll_md_blocking_ast;
146 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
153 void ll_release_page(struct inode *inode, struct page *page, bool remove)
158 * Always remove the page for striped dir, because the page is
159 * built from temporarily in LMV layer
161 if (inode && S_ISDIR(inode->i_mode) &&
162 ll_i2info(inode)->lli_lsm_md) {
169 if (likely(page->mapping))
170 truncate_complete_page(page->mapping, page);
177 * return IF_* type for given lu_dirent entry.
178 * IF_* flag shld be converted to particular OS file type in
179 * platform llite module.
181 static __u16 ll_dirent_type_get(struct lu_dirent *ent)
184 struct luda_type *lt;
187 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
188 const unsigned int align = sizeof(struct luda_type) - 1;
190 len = le16_to_cpu(ent->lde_namelen);
191 len = (len + align) & ~align;
192 lt = (void *)ent->lde_name + len;
193 type = IFTODT(le16_to_cpu(lt->lt_type));
198 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
199 struct dir_context *ctx)
201 struct ll_sb_info *sbi = ll_i2sbi(inode);
203 int is_api32 = ll_need_32bit_api(sbi);
204 int is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
209 page = ll_get_dir_page(inode, op_data, pos);
211 while (rc == 0 && !done) {
212 struct lu_dirpage *dp;
213 struct lu_dirent *ent;
222 hash = MDS_DIR_END_OFF;
223 dp = page_address(page);
224 for (ent = lu_dirent_start(dp); ent && !done;
225 ent = lu_dirent_next(ent)) {
232 hash = le64_to_cpu(ent->lde_hash);
235 * Skip until we find target hash
240 namelen = le16_to_cpu(ent->lde_namelen);
247 if (is_api32 && is_hash64)
251 fid_le_to_cpu(&fid, &ent->lde_fid);
252 ino = cl_fid_build_ino(&fid, is_api32);
253 type = ll_dirent_type_get(ent);
255 /* For 'll_nfs_get_name_filldir()', it will try
256 * to access the 'ent' through its 'lde_name',
257 * so the parameter 'name' for 'ctx->actor()'
258 * must be part of the 'ent'.
260 done = !dir_emit(ctx, ent->lde_name,
266 ll_release_page(inode, page, false);
270 next = le64_to_cpu(dp->ldp_hash_end);
272 if (pos == MDS_DIR_END_OFF) {
274 * End of directory reached.
277 ll_release_page(inode, page, false);
280 * Normal case: continue to the next
283 ll_release_page(inode, page,
284 le32_to_cpu(dp->ldp_flags) &
287 page = ll_get_dir_page(inode, op_data, pos);
295 static int ll_readdir(struct file *filp, struct dir_context *ctx)
297 struct inode *inode = file_inode(filp);
298 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
299 struct ll_sb_info *sbi = ll_i2sbi(inode);
300 __u64 pos = lfd ? lfd->lfd_pos : 0;
301 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
302 int api32 = ll_need_32bit_api(sbi);
303 struct md_op_data *op_data;
306 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) pos/size %lu/%llu 32bit_api %d\n",
307 PFID(ll_inode2fid(inode)), inode, (unsigned long)pos,
308 i_size_read(inode), api32);
310 if (pos == MDS_DIR_END_OFF) {
318 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
319 LUSTRE_OPC_ANY, inode);
320 if (IS_ERR(op_data)) {
321 rc = PTR_ERR(op_data);
325 if (unlikely(op_data->op_mea1)) {
327 * This is only needed for striped dir to fill ..,
330 if (file_dentry(filp)->d_parent &&
331 file_dentry(filp)->d_parent->d_inode) {
332 __u64 ibits = MDS_INODELOCK_UPDATE;
333 struct inode *parent;
335 parent = file_dentry(filp)->d_parent->d_inode;
336 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
337 op_data->op_fid3 = *ll_inode2fid(parent);
341 * If it can not find in cache, do lookup .. on the master
344 if (fid_is_zero(&op_data->op_fid3)) {
345 rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
347 ll_finish_md_op_data(op_data);
352 op_data->op_max_pages = sbi->ll_md_brw_pages;
354 rc = ll_dir_read(inode, &pos, op_data, ctx);
359 if (pos == MDS_DIR_END_OFF) {
361 pos = LL_DIR_END_OFF_32BIT;
363 pos = LL_DIR_END_OFF;
369 ll_finish_md_op_data(op_data);
370 filp->f_version = inode->i_version;
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);
505 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
506 lump->lmm_magic, LOV_USER_MAGIC_V1,
512 lum_size = sizeof(struct lov_user_md_v1);
515 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
516 LUSTRE_OPC_ANY, NULL);
518 return PTR_ERR(op_data);
520 /* swabbing is done in lov_setstripe() on server side */
521 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
522 ll_finish_md_op_data(op_data);
523 ptlrpc_req_finished(req);
527 #if OBD_OCD_VERSION(2, 13, 53, 0) > LUSTRE_VERSION_CODE
529 * 2.9 server has stored filesystem default stripe in ROOT xattr,
530 * and it's stored into system config for backward compatibility.
532 * In the following we use the fact that LOV_USER_MAGIC_V1 and
533 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
534 * need to make the distinction between the 2 versions
536 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
540 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
545 /* Get fsname and assume devname to be -MDT0000. */
546 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
547 strcat(buf, "-MDT0000.lov");
550 /* Set root stripesize */
551 sprintf(buf, ".stripesize=%u",
552 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
553 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
557 /* Set root stripecount */
558 sprintf(buf, ".stripecount=%hd",
559 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
560 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
564 /* Set root stripeoffset */
565 sprintf(buf, ".stripeoffset=%hd",
566 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
567 (typeof(lump->lmm_stripe_offset))(-1));
568 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
578 * This function will be used to get default LOV/LMV/Default LMV
579 * @valid will be used to indicate which stripe it will retrieve
580 * OBD_MD_MEA LMV stripe EA
581 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
582 * otherwise Default LOV EA.
583 * Each time, it can only retrieve 1 stripe EA
585 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
586 struct ptlrpc_request **request, u64 valid)
588 struct ll_sb_info *sbi = ll_i2sbi(inode);
589 struct mdt_body *body;
590 struct lov_mds_md *lmm = NULL;
591 struct ptlrpc_request *req = NULL;
593 struct md_op_data *op_data;
595 rc = ll_get_max_mdsize(sbi, &lmmsize);
599 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
600 0, lmmsize, LUSTRE_OPC_ANY,
603 return PTR_ERR(op_data);
605 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
606 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
607 ll_finish_md_op_data(op_data);
609 CDEBUG(D_INFO, "md_getattr failed on inode " DFID ": rc %d\n",
610 PFID(ll_inode2fid(inode)), rc);
614 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
616 lmmsize = body->mbo_eadatasize;
618 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
624 lmm = req_capsule_server_sized_get(&req->rq_pill,
625 &RMF_MDT_MD, lmmsize);
629 * This is coming from the MDS, so is probably in
630 * little endian. We convert it to host endian before
631 * passing it to userspace.
633 /* We don't swab objects for directories */
634 switch (le32_to_cpu(lmm->lmm_magic)) {
636 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
637 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
640 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
641 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
644 if (cpu_to_le32(LMV_MAGIC) != LMV_MAGIC)
645 lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
648 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
649 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
652 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
657 *plmm_size = lmmsize;
662 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
664 struct md_op_data *op_data;
667 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
671 op_data->op_flags |= MF_GET_MDT_IDX;
672 op_data->op_fid1 = *fid;
673 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
674 mdt_index = op_data->op_mds;
683 * Get MDT index for the inode.
685 int ll_get_mdt_idx(struct inode *inode)
687 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
691 * Generic handler to do any pre-copy work.
693 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
694 * first information for it that real work has started.
696 * Moreover, for a ARCHIVE request, it will sample the file data version and
697 * store it in \a copy.
699 * \return 0 on success.
701 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
703 struct ll_sb_info *sbi = ll_s2sbi(sb);
704 struct hsm_progress_kernel hpk;
707 /* Forge a hsm_progress based on data from copy. */
708 hpk.hpk_fid = copy->hc_hai.hai_fid;
709 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
710 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
711 hpk.hpk_extent.length = 0;
714 hpk.hpk_data_version = 0;
716 /* For archive request, we need to read the current file version. */
717 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
719 __u64 data_version = 0;
721 /* Get inode for this fid */
722 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
724 hpk.hpk_flags |= HP_FLAG_RETRY;
725 /* hpk_errval is >= 0 */
726 hpk.hpk_errval = -PTR_ERR(inode);
731 /* Read current file data version */
732 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
735 CDEBUG(D_HSM, "Could not read file data version of "
736 DFID " (rc = %d). Archive request (%#llx) could not be done.\n",
737 PFID(©->hc_hai.hai_fid), rc,
738 copy->hc_hai.hai_cookie);
739 hpk.hpk_flags |= HP_FLAG_RETRY;
740 /* hpk_errval must be >= 0 */
741 hpk.hpk_errval = -rc;
745 /* Store in the hsm_copy for later copytool use.
746 * Always modified even if no lsm.
748 copy->hc_data_version = data_version;
752 /* On error, the request should be considered as completed */
753 if (hpk.hpk_errval > 0)
754 hpk.hpk_flags |= HP_FLAG_COMPLETED;
755 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
758 return rc ? rc : rc2;
762 * Generic handler to do any post-copy work.
764 * It will send the last hsm_progress update to coordinator to inform it
765 * that copy is finished and whether it was successful or not.
768 * - for ARCHIVE request, it will sample the file data version and compare it
769 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
770 * will be considered as failed.
771 * - for RESTORE request, it will sample the file data version and send it to
772 * coordinator which is useful if the file was imported as 'released'.
774 * \return 0 on success.
776 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
778 struct ll_sb_info *sbi = ll_s2sbi(sb);
779 struct hsm_progress_kernel hpk;
782 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
783 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
784 * initialized if copy_end was called with copy == NULL.
787 /* Forge a hsm_progress based on data from copy. */
788 hpk.hpk_fid = copy->hc_hai.hai_fid;
789 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
790 hpk.hpk_extent = copy->hc_hai.hai_extent;
791 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
792 hpk.hpk_errval = copy->hc_errval;
793 hpk.hpk_data_version = 0;
795 /* For archive request, we need to check the file data was not changed.
797 * For restore request, we need to send the file data version, this is
798 * useful when the file was created using hsm_import.
800 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
801 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
802 (copy->hc_errval == 0)) {
804 __u64 data_version = 0;
806 /* Get lsm for this fid */
807 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
809 hpk.hpk_flags |= HP_FLAG_RETRY;
810 /* hpk_errval must be >= 0 */
811 hpk.hpk_errval = -PTR_ERR(inode);
816 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
819 CDEBUG(D_HSM, "Could not read file data version. Request could not be confirmed.\n");
820 if (hpk.hpk_errval == 0)
821 hpk.hpk_errval = -rc;
825 /* Store in the hsm_copy for later copytool use.
826 * Always modified even if no lsm.
828 hpk.hpk_data_version = data_version;
830 /* File could have been stripped during archiving, so we need
833 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
834 (copy->hc_data_version != data_version)) {
835 CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. "
836 DFID ", start:%#llx current:%#llx\n",
837 PFID(©->hc_hai.hai_fid),
838 copy->hc_data_version, data_version);
839 /* File was changed, send error to cdt. Do not ask for
840 * retry because if a file is modified frequently,
841 * the cdt will loop on retried archive requests.
842 * The policy engine will ask for a new archive later
843 * when the file will not be modified for some tunable
846 hpk.hpk_flags &= ~HP_FLAG_RETRY;
848 /* hpk_errval must be >= 0 */
849 hpk.hpk_errval = -rc;
854 rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
857 return rc ? rc : rc2;
860 static int copy_and_ioctl(int cmd, struct obd_export *exp,
861 const void __user *data, size_t size)
866 copy = memdup_user(data, size);
868 return PTR_ERR(copy);
870 rc = obd_iocontrol(cmd, exp, size, copy, NULL);
876 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
878 int cmd = qctl->qc_cmd;
879 int type = qctl->qc_type;
880 int id = qctl->qc_id;
881 int valid = qctl->qc_valid;
887 if (!capable(CFS_CAP_SYS_ADMIN))
891 if (((type == USRQUOTA &&
892 !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
894 !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
895 !capable(CFS_CAP_SYS_ADMIN))
901 CERROR("unsupported quotactl op: %#x\n", cmd);
905 if (valid != QC_GENERAL) {
906 if (cmd == Q_GETINFO)
907 qctl->qc_cmd = Q_GETOINFO;
908 else if (cmd == Q_GETQUOTA)
909 qctl->qc_cmd = Q_GETOQUOTA;
915 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
916 sizeof(*qctl), qctl, NULL);
919 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
920 sizeof(*qctl), qctl, NULL);
923 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
924 sizeof(*qctl), qctl, NULL);
926 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
928 sizeof(*qctl), qctl, NULL);
940 struct obd_quotactl *oqctl;
942 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
946 QCTL_COPY(oqctl, qctl);
947 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
952 /* If QIF_SPACE is not set, client should collect the
953 * space usage from OSSs by itself
955 if (cmd == Q_GETQUOTA &&
956 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
957 !oqctl->qc_dqblk.dqb_curspace) {
958 struct obd_quotactl *oqctl_tmp;
960 oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
966 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
967 oqctl_tmp->qc_id = oqctl->qc_id;
968 oqctl_tmp->qc_type = oqctl->qc_type;
970 /* collect space usage from OSTs */
971 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
972 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
973 if (!rc || rc == -EREMOTEIO) {
974 oqctl->qc_dqblk.dqb_curspace =
975 oqctl_tmp->qc_dqblk.dqb_curspace;
976 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
979 /* collect space & inode usage from MDTs */
980 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
981 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
982 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
983 if (!rc || rc == -EREMOTEIO) {
984 oqctl->qc_dqblk.dqb_curspace +=
985 oqctl_tmp->qc_dqblk.dqb_curspace;
986 oqctl->qc_dqblk.dqb_curinodes =
987 oqctl_tmp->qc_dqblk.dqb_curinodes;
988 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
990 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
996 QCTL_COPY(qctl, oqctl);
1003 /* This function tries to get a single name component,
1004 * to send to the server. No actual path traversal involved,
1005 * so we limit to NAME_MAX
1007 static char *ll_getname(const char __user *filename)
1012 tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
1014 return ERR_PTR(-ENOMEM);
1016 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1021 else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
1022 ret = -ENAMETOOLONG;
1031 #define ll_putname(filename) kfree(filename)
1033 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1035 struct inode *inode = file_inode(file);
1036 struct ll_sb_info *sbi = ll_i2sbi(inode);
1037 struct obd_ioctl_data *data;
1040 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), cmd=%#x\n",
1041 PFID(ll_inode2fid(inode)), inode, cmd);
1043 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1044 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1047 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1049 case FSFILT_IOC_GETFLAGS:
1050 case FSFILT_IOC_SETFLAGS:
1051 return ll_iocontrol(inode, file, cmd, arg);
1052 case FSFILT_IOC_GETVERSION_OLD:
1053 case FSFILT_IOC_GETVERSION:
1054 return put_user(inode->i_generation, (int __user *)arg);
1055 /* We need to special case any other ioctls we want to handle,
1056 * to send them to the MDS/OST as appropriate and to properly
1057 * network encode the arg field.
1058 case FSFILT_IOC_SETVERSION_OLD:
1059 case FSFILT_IOC_SETVERSION:
1061 case LL_IOC_GET_MDTIDX: {
1064 mdtidx = ll_get_mdt_idx(inode);
1068 if (put_user((int)mdtidx, (int __user *)arg))
1073 case IOC_MDC_LOOKUP: {
1074 int namelen, len = 0;
1078 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1083 filename = data->ioc_inlbuf1;
1084 namelen = strlen(filename);
1087 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1092 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1094 CERROR("%s: lookup %.*s failed: rc = %d\n",
1095 ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1103 case LL_IOC_LMV_SETSTRIPE: {
1104 struct lmv_user_md *lum;
1113 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1118 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1119 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1124 filename = data->ioc_inlbuf1;
1125 namelen = data->ioc_inllen1;
1128 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1132 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1133 lumlen = data->ioc_inllen2;
1135 if (lum->lum_magic != LMV_USER_MAGIC ||
1136 lumlen != sizeof(*lum)) {
1137 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1138 filename, lum->lum_magic, lumlen, -EFAULT);
1143 #if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1144 mode = data->ioc_type != 0 ? data->ioc_type : 0777;
1146 mode = data->ioc_type;
1148 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1153 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1154 struct lmv_user_md __user *ulump;
1155 struct lmv_user_md lum;
1158 ulump = (struct lmv_user_md __user *)arg;
1159 if (copy_from_user(&lum, ulump, sizeof(lum)))
1162 if (lum.lum_magic != LMV_USER_MAGIC)
1165 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1169 case LL_IOC_LOV_SETSTRIPE: {
1170 struct lov_user_md_v3 lumv3;
1171 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1172 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1173 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1175 int set_default = 0;
1177 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1178 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1179 sizeof(lumv3p->lmm_objects[0]));
1180 /* first try with v1 which is smaller than v3 */
1181 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1184 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1185 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1189 if (is_root_inode(inode))
1192 /* in v1 and v3 cases lumv1 points to data */
1193 rc = ll_dir_setstripe(inode, lumv1, set_default);
1197 case LL_IOC_LMV_GETSTRIPE: {
1198 struct lmv_user_md __user *ulmv;
1199 struct lmv_user_md lum;
1200 struct ptlrpc_request *request = NULL;
1201 struct lmv_user_md *tmp = NULL;
1202 union lmv_mds_md *lmm = NULL;
1204 int max_stripe_count;
1212 ulmv = (struct lmv_user_md __user *)arg;
1213 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1216 max_stripe_count = lum.lum_stripe_count;
1218 * lum_magic will indicate which stripe the ioctl will like
1219 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1220 * is for default LMV stripe
1222 if (lum.lum_magic == LMV_MAGIC_V1)
1223 valid |= OBD_MD_MEA;
1224 else if (lum.lum_magic == LMV_USER_MAGIC)
1225 valid |= OBD_MD_DEFAULT_MEA;
1229 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1234 /* Get default LMV EA */
1235 if (lum.lum_magic == LMV_USER_MAGIC) {
1236 if (lmmsize > sizeof(*ulmv)) {
1241 if (copy_to_user(ulmv, lmm, lmmsize))
1247 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1248 if (max_stripe_count < stripe_count) {
1249 lum.lum_stripe_count = stripe_count;
1250 if (copy_to_user(ulmv, &lum, sizeof(lum))) {
1258 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1259 tmp = kzalloc(lum_size, GFP_NOFS);
1265 mdt_index = ll_get_mdt_idx(inode);
1266 if (mdt_index < 0) {
1270 tmp->lum_magic = LMV_MAGIC_V1;
1271 tmp->lum_stripe_count = 0;
1272 tmp->lum_stripe_offset = mdt_index;
1273 for (i = 0; i < stripe_count; i++) {
1276 fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1277 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1278 if (mdt_index < 0) {
1282 tmp->lum_objects[i].lum_mds = mdt_index;
1283 tmp->lum_objects[i].lum_fid = fid;
1284 tmp->lum_stripe_count++;
1287 if (copy_to_user(ulmv, tmp, lum_size)) {
1294 ptlrpc_req_finished(request);
1298 case LL_IOC_LOV_SWAP_LAYOUTS:
1300 case IOC_OBD_STATFS:
1301 return ll_obd_statfs(inode, (void __user *)arg);
1302 case LL_IOC_LOV_GETSTRIPE:
1303 case LL_IOC_MDC_GETINFO:
1304 case IOC_MDC_GETFILEINFO:
1305 case IOC_MDC_GETFILESTRIPE: {
1306 struct ptlrpc_request *request = NULL;
1307 struct lov_user_md __user *lump;
1308 struct lov_mds_md *lmm = NULL;
1309 struct mdt_body *body;
1310 char *filename = NULL;
1313 if (cmd == IOC_MDC_GETFILEINFO ||
1314 cmd == IOC_MDC_GETFILESTRIPE) {
1315 filename = ll_getname((const char __user *)arg);
1316 if (IS_ERR(filename))
1317 return PTR_ERR(filename);
1319 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1320 &lmmsize, &request);
1322 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1327 body = req_capsule_server_get(&request->rq_pill,
1335 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1336 cmd == LL_IOC_MDC_GETINFO)) {
1344 if (cmd == IOC_MDC_GETFILESTRIPE ||
1345 cmd == LL_IOC_LOV_GETSTRIPE) {
1346 lump = (struct lov_user_md __user *)arg;
1348 struct lov_user_mds_data __user *lmdp;
1350 lmdp = (struct lov_user_mds_data __user *)arg;
1351 lump = &lmdp->lmd_lmm;
1353 if (copy_to_user(lump, lmm, lmmsize)) {
1354 if (copy_to_user(lump, lmm, sizeof(*lump))) {
1361 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1362 struct lov_user_mds_data __user *lmdp;
1365 st.st_dev = inode->i_sb->s_dev;
1366 st.st_mode = body->mbo_mode;
1367 st.st_nlink = body->mbo_nlink;
1368 st.st_uid = body->mbo_uid;
1369 st.st_gid = body->mbo_gid;
1370 st.st_rdev = body->mbo_rdev;
1371 st.st_size = body->mbo_size;
1372 st.st_blksize = PAGE_SIZE;
1373 st.st_blocks = body->mbo_blocks;
1374 st.st_atime = body->mbo_atime;
1375 st.st_mtime = body->mbo_mtime;
1376 st.st_ctime = body->mbo_ctime;
1377 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
1381 lmdp = (struct lov_user_mds_data __user *)arg;
1382 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1389 ptlrpc_req_finished(request);
1391 ll_putname(filename);
1394 case OBD_IOC_QUOTACTL: {
1395 struct if_quotactl *qctl;
1397 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
1401 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
1406 rc = quotactl_ioctl(sbi, qctl);
1408 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1416 case OBD_IOC_GETDTNAME:
1417 case OBD_IOC_GETMDNAME:
1418 return ll_get_obd_name(inode, cmd, arg);
1419 case LL_IOC_FLUSHCTX:
1420 return ll_flush_ctx(inode);
1421 case LL_IOC_GETOBDCOUNT: {
1423 struct obd_export *exp;
1425 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1428 /* get ost count when count is zero, get mdt count otherwise */
1429 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1430 vallen = sizeof(count);
1431 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1432 KEY_TGT_COUNT, &vallen, &count);
1434 CERROR("get target count failed: %d\n", rc);
1438 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1443 case LL_IOC_PATH2FID:
1444 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1445 sizeof(struct lu_fid)))
1448 case LL_IOC_GET_CONNECT_FLAGS: {
1449 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1450 (void __user *)arg);
1452 case OBD_IOC_CHANGELOG_SEND:
1453 case OBD_IOC_CHANGELOG_CLEAR:
1454 if (!capable(CFS_CAP_SYS_ADMIN))
1457 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1458 sizeof(struct ioc_changelog));
1460 case OBD_IOC_FID2PATH:
1461 return ll_fid2path(inode, (void __user *)arg);
1462 case LL_IOC_GETPARENT:
1463 return ll_getparent(file, (void __user *)arg);
1464 case LL_IOC_FID2MDTIDX: {
1465 struct obd_export *exp = ll_i2mdexp(inode);
1469 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1473 /* Call mdc_iocontrol */
1474 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1481 case LL_IOC_HSM_REQUEST: {
1482 struct hsm_user_request *hur;
1485 hur = memdup_user((void __user *)arg, sizeof(*hur));
1487 return PTR_ERR(hur);
1489 /* Compute the whole struct size */
1490 totalsize = hur_len(hur);
1495 /* Final size will be more than double totalsize */
1496 if (totalsize >= MDS_MAXREQSIZE / 3)
1499 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
1503 /* Copy the whole struct */
1504 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
1509 if (hur->hur_request.hr_action == HUA_RELEASE) {
1510 const struct lu_fid *fid;
1514 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1515 fid = &hur->hur_user_item[i].hui_fid;
1516 f = search_inode_for_lustre(inode->i_sb, fid);
1522 rc = ll_hsm_release(f);
1528 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1536 case LL_IOC_HSM_PROGRESS: {
1537 struct hsm_progress_kernel hpk;
1538 struct hsm_progress hp;
1540 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1543 hpk.hpk_fid = hp.hp_fid;
1544 hpk.hpk_cookie = hp.hp_cookie;
1545 hpk.hpk_extent = hp.hp_extent;
1546 hpk.hpk_flags = hp.hp_flags;
1547 hpk.hpk_errval = hp.hp_errval;
1548 hpk.hpk_data_version = 0;
1550 /* File may not exist in Lustre; all progress
1551 * reported to Lustre root
1553 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1557 case LL_IOC_HSM_CT_START:
1558 if (!capable(CFS_CAP_SYS_ADMIN))
1561 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1562 sizeof(struct lustre_kernelcomm));
1565 case LL_IOC_HSM_COPY_START: {
1566 struct hsm_copy *copy;
1569 copy = memdup_user((char __user *)arg, sizeof(*copy));
1571 return PTR_ERR(copy);
1573 rc = ll_ioc_copy_start(inode->i_sb, copy);
1574 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1580 case LL_IOC_HSM_COPY_END: {
1581 struct hsm_copy *copy;
1584 copy = memdup_user((char __user *)arg, sizeof(*copy));
1586 return PTR_ERR(copy);
1588 rc = ll_ioc_copy_end(inode->i_sb, copy);
1589 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1595 case LL_IOC_MIGRATE: {
1597 const char *filename;
1603 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1607 data = (struct obd_ioctl_data *)buf;
1608 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1609 !data->ioc_inllen1 || !data->ioc_inllen2) {
1614 filename = data->ioc_inlbuf1;
1615 namelen = data->ioc_inllen1;
1616 if (namelen < 1 || namelen != strlen(filename) + 1) {
1621 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1625 mdtidx = *(int *)data->ioc_inlbuf2;
1627 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1635 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1636 (void __user *)arg);
1640 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1642 struct inode *inode = file->f_mapping->host;
1643 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1644 struct ll_sb_info *sbi = ll_i2sbi(inode);
1645 int api32 = ll_need_32bit_api(sbi);
1646 loff_t ret = -EINVAL;
1652 offset += file->f_pos;
1658 offset += LL_DIR_END_OFF_32BIT;
1660 offset += LL_DIR_END_OFF;
1667 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1668 (!api32 && offset <= LL_DIR_END_OFF))) {
1669 if (offset != file->f_pos) {
1670 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1671 (!api32 && offset == LL_DIR_END_OFF))
1672 fd->lfd_pos = MDS_DIR_END_OFF;
1673 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1674 fd->lfd_pos = offset << 32;
1676 fd->lfd_pos = offset;
1677 file->f_pos = offset;
1678 file->f_version = 0;
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,