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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Directory code for lustre client.
42 #include <linux/pagemap.h>
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/buffer_head.h> // for wait_on_buffer
47 #include <linux/pagevec.h>
49 #define DEBUG_SUBSYSTEM S_LLITE
51 #include <lustre/lustre_idl.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_lib.h>
55 #include <lustre/lustre_idl.h>
56 #include <lustre_lite.h>
57 #include <lustre_dlm.h>
58 #include <lustre_fid.h>
59 #include "llite_internal.h"
62 * (new) readdir implementation overview.
64 * Original lustre readdir implementation cached exact copy of raw directory
65 * pages on the client. These pages were indexed in client page cache by
66 * logical offset in the directory file. This design, while very simple and
67 * intuitive had some inherent problems:
69 * . it implies that byte offset to the directory entry serves as a
70 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71 * ext3/htree directory entries may move due to splits, and more
74 * . it is incompatible with the design of split directories for cmd3,
75 * that assumes that names are distributed across nodes based on their
76 * hash, and so readdir should be done in hash order.
78 * New readdir implementation does readdir in hash order, and uses hash of a
79 * file name as a telldir/seekdir cookie. This led to number of complications:
81 * . hash is not unique, so it cannot be used to index cached directory
82 * pages on the client (note, that it requires a whole pageful of hash
83 * collided entries to cause two pages to have identical hashes);
85 * . hash is not unique, so it cannot, strictly speaking, be used as an
86 * entry cookie. ext3/htree has the same problem and lustre implementation
87 * mimics their solution: seekdir(hash) positions directory at the first
88 * entry with the given hash.
94 * Client caches directory pages using hash of the first entry as an index. As
95 * noted above hash is not unique, so this solution doesn't work as is:
96 * special processing is needed for "page hash chains" (i.e., sequences of
97 * pages filled with entries all having the same hash value).
99 * First, such chains have to be detected. To this end, server returns to the
100 * client the hash of the first entry on the page next to one returned. When
101 * client detects that this hash is the same as hash of the first entry on the
102 * returned page, page hash collision has to be handled. Pages in the
103 * hash chain, except first one, are termed "overflow pages".
105 * Solution to index uniqueness problem is to not cache overflow
106 * pages. Instead, when page hash collision is detected, all overflow pages
107 * from emerging chain are immediately requested from the server and placed in
108 * a special data structure (struct ll_dir_chain). This data structure is used
109 * by ll_readdir() to process entries from overflow pages. When readdir
110 * invocation finishes, overflow pages are discarded. If page hash collision
111 * chain weren't completely processed, next call to readdir will again detect
112 * page hash collision, again read overflow pages in, process next portion of
113 * entries and again discard the pages. This is not as wasteful as it looks,
114 * because, given reasonable hash, page hash collisions are extremely rare.
116 * 1. directory positioning
118 * When seekdir(hash) is called, original
129 * identification of and access to overflow pages
133 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
134 * a header lu_dirpage which describes the start/end hash, and whether this
135 * page is empty (contains no dir entry) or hash collide with next page.
136 * After client receives reply, several pages will be integrated into dir page
137 * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
138 * lu_dirpage for this integrated page will be adjusted. See
139 * lmv_adjust_dirpages().
143 /* returns the page unlocked, but with a reference */
144 static int ll_dir_filler(void *_hash, struct page *page0)
146 struct inode *inode = page0->mapping->host;
147 int hash64 = ll_i2sbi(inode)->ll_flags & LL_SBI_64BIT_HASH;
148 struct obd_export *exp = ll_i2sbi(inode)->ll_md_exp;
149 struct ptlrpc_request *request;
150 struct mdt_body *body;
151 struct md_op_data *op_data;
152 __u64 hash = *((__u64 *)_hash);
153 struct page **page_pool;
155 struct lu_dirpage *dp;
156 int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> PAGE_CACHE_SHIFT;
157 int nrdpgs = 0; /* number of pages read actually */
163 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
164 inode->i_ino, inode->i_generation, inode, hash);
166 LASSERT(max_pages > 0 && max_pages <= MD_MAX_BRW_PAGES);
168 OBD_ALLOC(page_pool, sizeof(page) * max_pages);
169 if (page_pool != NULL) {
170 page_pool[0] = page0;
175 for (npages = 1; npages < max_pages; npages++) {
176 page = page_cache_alloc_cold(inode->i_mapping);
179 page_pool[npages] = page;
182 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
183 LUSTRE_OPC_ANY, NULL);
184 op_data->op_npages = npages;
185 op_data->op_offset = hash;
186 rc = md_readpage(exp, op_data, page_pool, &request);
187 ll_finish_md_op_data(op_data);
189 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
190 /* Checked by mdc_readpage() */
191 LASSERT(body != NULL);
193 if (body->valid & OBD_MD_FLSIZE)
194 cl_isize_write(inode, body->size);
196 nrdpgs = (request->rq_bulk->bd_nob_transferred +
197 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
198 SetPageUptodate(page0);
201 ptlrpc_req_finished(request);
203 CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
205 for (i = 1; i < npages; i++) {
206 unsigned long offset;
211 if (rc < 0 || i >= nrdpgs) {
212 page_cache_release(page);
216 SetPageUptodate(page);
219 hash = le64_to_cpu(dp->ldp_hash_start);
222 offset = hash_x_index(hash, hash64);
224 prefetchw(&page->flags);
225 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
230 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
231 " %d\n", offset, ret);
232 page_cache_release(page);
235 if (page_pool != &page0)
236 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
241 static void ll_check_page(struct inode *dir, struct page *page)
243 /* XXX: check page format later */
244 SetPageChecked(page);
247 void ll_release_page(struct page *page, int remove)
252 if (likely(page->mapping != NULL))
253 truncate_complete_page(page->mapping, page);
256 page_cache_release(page);
260 * Find, kmap and return page that contains given hash.
262 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
263 __u64 *start, __u64 *end)
265 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
266 struct address_space *mapping = dir->i_mapping;
268 * Complement of hash is used as an index so that
269 * radix_tree_gang_lookup() can be used to find a page with starting
270 * hash _smaller_ than one we are looking for.
272 unsigned long offset = hash_x_index(*hash, hash64);
276 TREE_READ_LOCK_IRQ(mapping);
277 found = radix_tree_gang_lookup(&mapping->page_tree,
278 (void **)&page, offset, 1);
280 struct lu_dirpage *dp;
282 page_cache_get(page);
283 TREE_READ_UNLOCK_IRQ(mapping);
285 * In contrast to find_lock_page() we are sure that directory
286 * page cannot be truncated (while DLM lock is held) and,
287 * hence, can avoid restart.
289 * In fact, page cannot be locked here at all, because
290 * ll_dir_filler() does synchronous io.
293 if (PageUptodate(page)) {
295 if (BITS_PER_LONG == 32 && hash64) {
296 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
297 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
300 *start = le64_to_cpu(dp->ldp_hash_start);
301 *end = le64_to_cpu(dp->ldp_hash_end);
303 LASSERTF(*start <= *hash, "start = "LPX64",end = "
304 LPX64",hash = "LPX64"\n", *start, *end, *hash);
305 CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
306 offset, *start, *end, *hash);
308 ll_release_page(page, 0);
310 } else if (*end != *start && *hash == *end) {
312 * upon hash collision, remove this page,
313 * otherwise put page reference, and
314 * ll_get_dir_page() will issue RPC to fetch
317 ll_release_page(page,
318 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
322 page_cache_release(page);
323 page = ERR_PTR(-EIO);
327 TREE_READ_UNLOCK_IRQ(mapping);
333 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
334 struct ll_dir_chain *chain)
336 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
337 struct address_space *mapping = dir->i_mapping;
338 struct lustre_handle lockh;
339 struct lu_dirpage *dp;
346 struct ll_inode_info *lli = ll_i2info(dir);
347 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
350 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
351 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
353 struct ldlm_enqueue_info einfo = {
354 .ei_type = LDLM_IBITS,
356 .ei_cb_bl = ll_md_blocking_ast,
357 .ei_cb_cp = ldlm_completion_ast,
359 struct lookup_intent it = { .it_op = IT_READDIR };
360 struct ptlrpc_request *request;
361 struct md_op_data *op_data;
363 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
364 LUSTRE_OPC_ANY, NULL);
366 return (void *)op_data;
368 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
369 op_data, &lockh, NULL, 0, NULL, 0);
371 ll_finish_md_op_data(op_data);
373 request = (struct ptlrpc_request *)it.d.lustre.it_data;
375 ptlrpc_req_finished(request);
377 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
378 PFID(ll_inode2fid(dir)), hash, rc);
382 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
383 dir, dir->i_ino, dir->i_generation);
384 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
385 &it.d.lustre.it_lock_handle, dir, NULL);
387 /* for cross-ref object, l_ast_data of the lock may not be set,
388 * we reset it here */
389 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
392 ldlm_lock_dump_handle(D_OTHER, &lockh);
394 mutex_lock(&lli->lli_readdir_mutex);
395 page = ll_dir_page_locate(dir, &lhash, &start, &end);
397 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
398 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
399 GOTO(out_unlock, page);
400 } else if (page != NULL) {
402 * XXX nikita: not entirely correct handling of a corner case:
403 * suppose hash chain of entries with hash value HASH crosses
404 * border between pages P0 and P1. First both P0 and P1 are
405 * cached, seekdir() is called for some entry from the P0 part
406 * of the chain. Later P0 goes out of cache. telldir(HASH)
407 * happens and finds P1, as it starts with matching hash
408 * value. Remaining entries from P0 part of the chain are
409 * skipped. (Is that really a bug?)
411 * Possible solutions: 0. don't cache P1 is such case, handle
412 * it as an "overflow" page. 1. invalidate all pages at
413 * once. 2. use HASH|1 as an index for P1.
415 GOTO(hash_collision, page);
418 page = read_cache_page(mapping, hash_x_index(hash, hash64),
419 ll_dir_filler, &lhash);
421 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
422 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
423 GOTO(out_unlock, page);
428 if (!PageUptodate(page)) {
429 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
430 PFID(ll_inode2fid(dir)), hash, -5);
433 if (!PageChecked(page))
434 ll_check_page(dir, page);
435 if (PageError(page)) {
436 CERROR("page error: "DFID" at "LPU64": rc %d\n",
437 PFID(ll_inode2fid(dir)), hash, -5);
441 dp = page_address(page);
442 if (BITS_PER_LONG == 32 && hash64) {
443 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
444 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
447 start = le64_to_cpu(dp->ldp_hash_start);
448 end = le64_to_cpu(dp->ldp_hash_end);
452 LASSERT(start == lhash);
453 CWARN("Page-wide hash collision: "LPU64"\n", end);
454 if (BITS_PER_LONG == 32 && hash64)
455 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
456 "] with hash "LPU64"\n",
457 le64_to_cpu(dp->ldp_hash_start),
458 le64_to_cpu(dp->ldp_hash_end), hash);
460 * Fetch whole overflow chain...
467 mutex_unlock(&lli->lli_readdir_mutex);
468 ldlm_lock_decref(&lockh, mode);
472 ll_release_page(page, 1);
473 page = ERR_PTR(-EIO);
477 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
480 struct ll_inode_info *info = ll_i2info(inode);
481 struct ll_sb_info *sbi = ll_i2sbi(inode);
483 int api32 = ll_need_32bit_api(sbi);
484 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
486 struct ll_dir_chain chain;
491 ll_dir_chain_init(&chain);
493 page = ll_get_dir_page(inode, pos, &chain);
495 while (rc == 0 && !done) {
496 struct lu_dirpage *dp;
497 struct lu_dirent *ent;
501 * If page is empty (end of directory is reached),
504 __u64 hash = MDS_DIR_END_OFF;
507 dp = page_address(page);
508 for (ent = lu_dirent_start(dp); ent != NULL && !done;
509 ent = lu_dirent_next(ent)) {
517 * XXX: implement correct swabbing here.
520 hash = le64_to_cpu(ent->lde_hash);
523 * Skip until we find target hash
528 namelen = le16_to_cpu(ent->lde_namelen);
539 fid_le_to_cpu(&fid, &ent->lde_fid);
540 ino = cl_fid_build_ino(&fid, api32);
541 type = ll_dirent_type_get(ent);
542 /* For 'll_nfs_get_name_filldir()', it will try
543 * to access the 'ent' through its 'lde_name',
544 * so the parameter 'name' for 'filldir()' must
545 * be part of the 'ent'. */
546 done = filldir(cookie, ent->lde_name, namelen,
549 next = le64_to_cpu(dp->ldp_hash_end);
552 if (pos == MDS_DIR_END_OFF) {
554 * End of directory reached.
557 ll_release_page(page, 0);
558 } else if (1 /* chain is exhausted*/) {
560 * Normal case: continue to the next
563 ll_release_page(page,
564 le32_to_cpu(dp->ldp_flags) &
567 page = ll_get_dir_page(inode, pos,
571 * go into overflow page.
573 LASSERT(le32_to_cpu(dp->ldp_flags) &
575 ll_release_page(page, 1);
579 ll_release_page(page, 0);
583 CERROR("error reading dir "DFID" at %lu: rc %d\n",
584 PFID(&info->lli_fid), (unsigned long)pos, rc);
589 ll_dir_chain_fini(&chain);
593 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
595 struct inode *inode = filp->f_dentry->d_inode;
596 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
597 struct ll_sb_info *sbi = ll_i2sbi(inode);
598 __u64 pos = lfd->lfd_pos;
599 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
600 int api32 = ll_need_32bit_api(sbi);
602 #ifdef HAVE_TOUCH_ATIME_1ARG
607 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
608 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
609 inode, (unsigned long)pos, i_size_read(inode), api32);
611 if (pos == MDS_DIR_END_OFF)
617 rc = ll_dir_read(inode, &pos, cookie, filldir);
619 if (pos == MDS_DIR_END_OFF) {
621 filp->f_pos = LL_DIR_END_OFF_32BIT;
623 filp->f_pos = LL_DIR_END_OFF;
626 filp->f_pos = pos >> 32;
630 filp->f_version = inode->i_version;
631 #ifdef HAVE_TOUCH_ATIME_1ARG
632 #ifdef HAVE_F_PATH_MNT
633 path.mnt = filp->f_path.mnt;
635 path.mnt = filp->f_vfsmnt;
637 path.dentry = filp->f_dentry;
640 touch_atime(filp->f_vfsmnt, filp->f_dentry);
645 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
650 int ll_send_mgc_param(struct obd_export *mgc, char *string)
652 struct mgs_send_param *msp;
659 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
660 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
661 sizeof(struct mgs_send_param), msp, NULL);
663 CERROR("Failed to set parameter: %d\n", rc);
669 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
672 struct ptlrpc_request *request = NULL;
673 struct md_op_data *op_data;
674 struct ll_sb_info *sbi = ll_i2sbi(dir);
680 mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
681 op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
682 strlen(filename), mode, LUSTRE_OPC_MKDIR,
685 GOTO(err_exit, err = PTR_ERR(op_data));
687 op_data->op_cli_flags |= CLI_SET_MEA;
688 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
689 cfs_curproc_fsuid(), cfs_curproc_fsgid(),
690 cfs_curproc_cap_pack(), 0, &request);
691 ll_finish_md_op_data(op_data);
695 ptlrpc_req_finished(request);
699 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
702 struct ll_sb_info *sbi = ll_i2sbi(inode);
703 struct md_op_data *op_data;
704 struct ptlrpc_request *req = NULL;
706 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
707 struct obd_device *mgc = lsi->lsi_mgc;
713 * This is coming from userspace, so should be in
714 * local endian. But the MDS would like it in little
715 * endian, so we swab it before we send it.
717 switch (lump->lmm_magic) {
718 case LOV_USER_MAGIC_V1: {
719 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
720 lustre_swab_lov_user_md_v1(lump);
721 lum_size = sizeof(struct lov_user_md_v1);
724 case LOV_USER_MAGIC_V3: {
725 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
726 lustre_swab_lov_user_md_v3(
727 (struct lov_user_md_v3 *)lump);
728 lum_size = sizeof(struct lov_user_md_v3);
732 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
733 " %#08x != %#08x nor %#08x\n",
734 lump->lmm_magic, LOV_USER_MAGIC_V1,
740 lum_size = sizeof(struct lov_user_md_v1);
743 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
744 LUSTRE_OPC_ANY, NULL);
746 RETURN(PTR_ERR(op_data));
748 if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
749 op_data->op_cli_flags |= CLI_SET_MEA;
751 /* swabbing is done in lov_setstripe() on server side */
752 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
753 NULL, 0, &req, NULL);
754 ll_finish_md_op_data(op_data);
755 ptlrpc_req_finished(req);
757 if (rc != -EPERM && rc != -EACCES)
758 CERROR("mdc_setattr fails: rc = %d\n", rc);
761 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
762 LOV_USER_MAGIC_V3 have the same initial fields so we do not
763 need the make the distiction between the 2 versions */
764 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
768 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
770 GOTO(end, rc = -ENOMEM);
773 /* Get fsname and assume devname to be -MDT0000. */
774 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
775 strcat(buf, "-MDT0000.lov");
778 /* Set root stripesize */
779 sprintf(buf, ".stripesize=%u",
780 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
781 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
785 /* Set root stripecount */
786 sprintf(buf, ".stripecount=%hd",
787 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
788 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
792 /* Set root stripeoffset */
793 sprintf(buf, ".stripeoffset=%hd",
794 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
795 (typeof(lump->lmm_stripe_offset))(-1));
796 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
800 OBD_FREE(param, MGS_PARAM_MAXLEN);
805 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
806 int *lmm_size, struct ptlrpc_request **request)
808 struct ll_sb_info *sbi = ll_i2sbi(inode);
809 struct mdt_body *body;
810 struct lov_mds_md *lmm = NULL;
811 struct ptlrpc_request *req = NULL;
813 struct md_op_data *op_data;
815 rc = ll_get_max_mdsize(sbi, &lmmsize);
819 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
820 0, lmmsize, LUSTRE_OPC_ANY,
823 RETURN(PTR_ERR(op_data));
825 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
826 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
827 ll_finish_md_op_data(op_data);
829 CDEBUG(D_INFO, "md_getattr failed on inode "
830 "%lu/%u: rc %d\n", inode->i_ino,
831 inode->i_generation, rc);
835 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
836 LASSERT(body != NULL);
838 lmmsize = body->eadatasize;
840 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
842 GOTO(out, rc = -ENODATA);
845 lmm = req_capsule_server_sized_get(&req->rq_pill,
846 &RMF_MDT_MD, lmmsize);
847 LASSERT(lmm != NULL);
850 * This is coming from the MDS, so is probably in
851 * little endian. We convert it to host endian before
852 * passing it to userspace.
854 /* We don't swab objects for directories */
855 switch (le32_to_cpu(lmm->lmm_magic)) {
857 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
858 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
861 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
862 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
865 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
876 * Get MDT index for the inode.
878 int ll_get_mdt_idx(struct inode *inode)
880 struct ll_sb_info *sbi = ll_i2sbi(inode);
881 struct md_op_data *op_data;
885 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
886 0, LUSTRE_OPC_ANY, NULL);
888 RETURN(PTR_ERR(op_data));
890 op_data->op_flags |= MF_GET_MDT_IDX;
891 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
892 mdtidx = op_data->op_mds;
893 ll_finish_md_op_data(op_data);
895 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
902 * Generic handler to do any pre-copy work.
904 * It send a first hsm_progress (with extent length == 0) to coordinator as a
905 * first information for it that real work has started.
907 * Moreover, for a ARCHIVE request, it will sample the file data version and
908 * store it in \a copy.
910 * \return 0 on success.
912 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
914 struct ll_sb_info *sbi = ll_s2sbi(sb);
915 struct hsm_progress_kernel hpk;
919 /* Forge a hsm_progress based on data from copy. */
920 hpk.hpk_fid = copy->hc_hai.hai_fid;
921 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
922 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
923 hpk.hpk_extent.length = 0;
926 hpk.hpk_data_version = 0;
929 /* For archive request, we need to read the current file version. */
930 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
932 __u64 data_version = 0;
934 /* Get inode for this fid */
935 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
937 hpk.hpk_flags |= HP_FLAG_RETRY;
938 /* hpk_errval is >= 0 */
939 hpk.hpk_errval = -PTR_ERR(inode);
940 GOTO(progress, rc = PTR_ERR(inode));
943 /* Read current file data version */
944 rc = ll_data_version(inode, &data_version, 1);
947 CDEBUG(D_HSM, "Could not read file data version of "
948 DFID" (rc = %d). Archive request ("
949 LPX64") could not be done.\n",
950 PFID(©->hc_hai.hai_fid), rc,
951 copy->hc_hai.hai_cookie);
952 hpk.hpk_flags |= HP_FLAG_RETRY;
953 /* hpk_errval must be >= 0 */
954 hpk.hpk_errval = -rc;
958 /* Store it the hsm_copy for later copytool use.
959 * Always modified even if no lsm. */
960 copy->hc_data_version = data_version;
964 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
971 * Generic handler to do any post-copy work.
973 * It will send the last hsm_progress update to coordinator to inform it
974 * that copy is finished and whether it was successful or not.
977 * - for ARCHIVE request, it will sample the file data version and compare it
978 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
979 * will be considered as failed.
980 * - for RESTORE request, it will sample the file data version and send it to
981 * coordinator which is useful if the file was imported as 'released'.
983 * \return 0 on success.
985 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
987 struct ll_sb_info *sbi = ll_s2sbi(sb);
988 struct hsm_progress_kernel hpk;
992 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
993 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
994 * initialized if copy_end was called with copy == NULL.
997 /* Forge a hsm_progress based on data from copy. */
998 hpk.hpk_fid = copy->hc_hai.hai_fid;
999 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1000 hpk.hpk_extent = copy->hc_hai.hai_extent;
1001 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1002 hpk.hpk_errval = copy->hc_errval;
1003 hpk.hpk_data_version = 0;
1005 /* For archive request, we need to check the file data was not changed.
1007 * For restore request, we need to send the file data version, this is
1008 * useful when the file was created using hsm_import.
1010 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1011 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1012 (copy->hc_errval == 0)) {
1013 struct inode *inode;
1014 __u64 data_version = 0;
1016 /* Get lsm for this fid */
1017 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1018 if (IS_ERR(inode)) {
1019 hpk.hpk_flags |= HP_FLAG_RETRY;
1020 /* hpk_errval must be >= 0 */
1021 hpk.hpk_errval = -PTR_ERR(inode);
1022 GOTO(progress, rc = PTR_ERR(inode));
1025 rc = ll_data_version(inode, &data_version,
1026 copy->hc_hai.hai_action == HSMA_ARCHIVE);
1029 CDEBUG(D_HSM, "Could not read file data version. "
1030 "Request could not be confirmed.\n");
1031 if (hpk.hpk_errval == 0)
1032 hpk.hpk_errval = -rc;
1036 /* Store it the hsm_copy for later copytool use.
1037 * Always modified even if no lsm. */
1038 hpk.hpk_data_version = data_version;
1040 /* File could have been stripped during archiving, so we need
1041 * to check anyway. */
1042 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1043 (copy->hc_data_version != data_version)) {
1044 CDEBUG(D_HSM, "File data version mismatched. "
1045 "File content was changed during archiving. "
1046 DFID", start:"LPX64" current:"LPX64"\n",
1047 PFID(©->hc_hai.hai_fid),
1048 copy->hc_data_version, data_version);
1049 /* File was changed, send error to cdt. Do not ask for
1050 * retry because if a file is modified frequently,
1051 * the cdt will loop on retried archive requests.
1052 * The policy engine will ask for a new archive later
1053 * when the file will not be modified for some tunable
1055 /* we do not notify caller */
1056 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1057 /* hpk_errval must be >= 0 */
1058 hpk.hpk_errval = EBUSY;
1064 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1071 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
1076 OBD_ALLOC(ptr, len);
1079 if (copy_from_user(ptr, data, len)) {
1083 rc = obd_iocontrol(cmd, exp, len, data, NULL);
1088 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1090 int cmd = qctl->qc_cmd;
1091 int type = qctl->qc_type;
1092 int id = qctl->qc_id;
1093 int valid = qctl->qc_valid;
1098 case LUSTRE_Q_INVALIDATE:
1099 case LUSTRE_Q_FINVALIDATE:
1104 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1105 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1109 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1110 (type == GRPQUOTA && !in_egroup_p(id))) &&
1111 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1112 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1118 CERROR("unsupported quotactl op: %#x\n", cmd);
1122 if (valid != QC_GENERAL) {
1123 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1124 RETURN(-EOPNOTSUPP);
1126 if (cmd == Q_GETINFO)
1127 qctl->qc_cmd = Q_GETOINFO;
1128 else if (cmd == Q_GETQUOTA)
1129 qctl->qc_cmd = Q_GETOQUOTA;
1135 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1136 sizeof(*qctl), qctl, NULL);
1139 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1140 sizeof(*qctl), qctl, NULL);
1143 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1144 sizeof(*qctl), qctl, NULL);
1146 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1148 sizeof(*qctl), qctl, NULL);
1160 struct obd_quotactl *oqctl;
1162 OBD_ALLOC_PTR(oqctl);
1166 QCTL_COPY(oqctl, qctl);
1167 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1169 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1170 oqctl->qc_cmd = Q_QUOTAOFF;
1171 obd_quotactl(sbi->ll_md_exp, oqctl);
1173 OBD_FREE_PTR(oqctl);
1176 /* If QIF_SPACE is not set, client should collect the
1177 * space usage from OSSs by itself */
1178 if (cmd == Q_GETQUOTA &&
1179 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1180 !oqctl->qc_dqblk.dqb_curspace) {
1181 struct obd_quotactl *oqctl_tmp;
1183 OBD_ALLOC_PTR(oqctl_tmp);
1184 if (oqctl_tmp == NULL)
1185 GOTO(out, rc = -ENOMEM);
1187 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1188 oqctl_tmp->qc_id = oqctl->qc_id;
1189 oqctl_tmp->qc_type = oqctl->qc_type;
1191 /* collect space usage from OSTs */
1192 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1193 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1194 if (!rc || rc == -EREMOTEIO) {
1195 oqctl->qc_dqblk.dqb_curspace =
1196 oqctl_tmp->qc_dqblk.dqb_curspace;
1197 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1200 /* collect space & inode usage from MDTs */
1201 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1202 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1203 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1204 if (!rc || rc == -EREMOTEIO) {
1205 oqctl->qc_dqblk.dqb_curspace +=
1206 oqctl_tmp->qc_dqblk.dqb_curspace;
1207 oqctl->qc_dqblk.dqb_curinodes =
1208 oqctl_tmp->qc_dqblk.dqb_curinodes;
1209 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1211 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1214 OBD_FREE_PTR(oqctl_tmp);
1217 QCTL_COPY(qctl, oqctl);
1218 OBD_FREE_PTR(oqctl);
1225 ll_getname(const char __user *filename)
1228 char *tmp = __getname();
1231 return ERR_PTR(-ENOMEM);
1233 len = strncpy_from_user(tmp, filename, PATH_MAX);
1236 else if (len > PATH_MAX)
1237 ret = -ENAMETOOLONG;
1246 #define ll_putname(filename) __putname(filename)
1248 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1250 struct inode *inode = file->f_dentry->d_inode;
1251 struct ll_sb_info *sbi = ll_i2sbi(inode);
1252 struct obd_ioctl_data *data;
1256 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1257 inode->i_ino, inode->i_generation, inode, cmd);
1259 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1260 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1263 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1265 case FSFILT_IOC_GETFLAGS:
1266 case FSFILT_IOC_SETFLAGS:
1267 RETURN(ll_iocontrol(inode, file, cmd, arg));
1268 case FSFILT_IOC_GETVERSION_OLD:
1269 case FSFILT_IOC_GETVERSION:
1270 RETURN(put_user(inode->i_generation, (int *)arg));
1271 /* We need to special case any other ioctls we want to handle,
1272 * to send them to the MDS/OST as appropriate and to properly
1273 * network encode the arg field.
1274 case FSFILT_IOC_SETVERSION_OLD:
1275 case FSFILT_IOC_SETVERSION:
1277 case LL_IOC_GET_MDTIDX: {
1280 mdtidx = ll_get_mdt_idx(inode);
1284 if (put_user((int)mdtidx, (int*)arg))
1289 case IOC_MDC_LOOKUP: {
1290 struct ptlrpc_request *request = NULL;
1291 int namelen, len = 0;
1294 struct md_op_data *op_data;
1296 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1301 filename = data->ioc_inlbuf1;
1302 namelen = strlen(filename);
1305 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1306 GOTO(out_free, rc = -EINVAL);
1309 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1310 0, LUSTRE_OPC_ANY, NULL);
1311 if (IS_ERR(op_data))
1312 GOTO(out_free, rc = PTR_ERR(op_data));
1314 op_data->op_valid = OBD_MD_FLID;
1315 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1316 ll_finish_md_op_data(op_data);
1318 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1321 ptlrpc_req_finished(request);
1324 obd_ioctl_freedata(buf, len);
1327 case LL_IOC_LMV_SETSTRIPE: {
1328 struct lmv_user_md *lum;
1336 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1341 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1342 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1343 GOTO(lmv_out_free, rc = -EINVAL);
1345 filename = data->ioc_inlbuf1;
1346 namelen = data->ioc_inllen1;
1349 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1350 GOTO(lmv_out_free, rc = -EINVAL);
1352 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1353 lumlen = data->ioc_inllen2;
1355 if (lum->lum_magic != LMV_USER_MAGIC ||
1356 lumlen != sizeof(*lum)) {
1357 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1358 filename, lum->lum_magic, lumlen, -EFAULT);
1359 GOTO(lmv_out_free, rc = -EINVAL);
1363 * ll_dir_setdirstripe will be used to set dir stripe
1364 * mdc_create--->mdt_reint_create (with dirstripe)
1366 rc = ll_dir_setdirstripe(inode, lum, filename);
1368 obd_ioctl_freedata(buf, len);
1372 case LL_IOC_LOV_SETSTRIPE: {
1373 struct lov_user_md_v3 lumv3;
1374 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1375 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1376 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1378 int set_default = 0;
1380 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1381 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1382 sizeof(lumv3p->lmm_objects[0]));
1383 /* first try with v1 which is smaller than v3 */
1384 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1387 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1388 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1392 if (inode->i_sb->s_root == file->f_dentry)
1395 /* in v1 and v3 cases lumv1 points to data */
1396 rc = ll_dir_setstripe(inode, lumv1, set_default);
1400 case LL_IOC_LMV_GETSTRIPE: {
1401 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1402 struct lmv_user_md lum;
1403 struct lmv_user_md *tmp;
1408 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1411 if (lum.lum_magic != LMV_MAGIC_V1)
1414 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1415 OBD_ALLOC(tmp, lum_size);
1417 GOTO(free_lmv, rc = -ENOMEM);
1419 memcpy(tmp, &lum, sizeof(lum));
1420 tmp->lum_type = LMV_STRIPE_TYPE;
1421 tmp->lum_stripe_count = 1;
1422 mdtindex = ll_get_mdt_idx(inode);
1424 GOTO(free_lmv, rc = -ENOMEM);
1426 tmp->lum_stripe_offset = mdtindex;
1427 tmp->lum_objects[0].lum_mds = mdtindex;
1428 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1429 sizeof(struct lu_fid));
1430 if (copy_to_user((void *)arg, tmp, lum_size))
1431 GOTO(free_lmv, rc = -EFAULT);
1434 OBD_FREE(tmp, lum_size);
1437 case LL_IOC_REMOVE_ENTRY: {
1438 char *filename = NULL;
1442 /* Here is a little hack to avoid sending REINT_RMENTRY to
1443 * unsupported server, which might crash the server(LU-2730),
1444 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1445 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1446 * server will support REINT_RMENTRY XXX*/
1447 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1450 filename = ll_getname((const char *)arg);
1451 if (IS_ERR(filename))
1452 RETURN(PTR_ERR(filename));
1454 namelen = strlen(filename);
1456 GOTO(out_rmdir, rc = -EINVAL);
1458 rc = ll_rmdir_entry(inode, filename, namelen);
1461 ll_putname(filename);
1464 case LL_IOC_LOV_SWAP_LAYOUTS:
1466 case LL_IOC_OBD_STATFS:
1467 RETURN(ll_obd_statfs(inode, (void *)arg));
1468 case LL_IOC_LOV_GETSTRIPE:
1469 case LL_IOC_MDC_GETINFO:
1470 case IOC_MDC_GETFILEINFO:
1471 case IOC_MDC_GETFILESTRIPE: {
1472 struct ptlrpc_request *request = NULL;
1473 struct lov_user_md *lump;
1474 struct lov_mds_md *lmm = NULL;
1475 struct mdt_body *body;
1476 char *filename = NULL;
1479 if (cmd == IOC_MDC_GETFILEINFO ||
1480 cmd == IOC_MDC_GETFILESTRIPE) {
1481 filename = ll_getname((const char *)arg);
1482 if (IS_ERR(filename))
1483 RETURN(PTR_ERR(filename));
1485 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1486 &lmmsize, &request);
1488 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1492 body = req_capsule_server_get(&request->rq_pill,
1494 LASSERT(body != NULL);
1500 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1501 cmd == LL_IOC_MDC_GETINFO))
1502 GOTO(skip_lmm, rc = 0);
1507 if (cmd == IOC_MDC_GETFILESTRIPE ||
1508 cmd == LL_IOC_LOV_GETSTRIPE) {
1509 lump = (struct lov_user_md *)arg;
1511 struct lov_user_mds_data *lmdp;
1512 lmdp = (struct lov_user_mds_data *)arg;
1513 lump = &lmdp->lmd_lmm;
1515 if (copy_to_user(lump, lmm, lmmsize)) {
1516 if (copy_to_user(lump, lmm, sizeof(*lump)))
1517 GOTO(out_req, rc = -EFAULT);
1521 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1522 struct lov_user_mds_data *lmdp;
1525 st.st_dev = inode->i_sb->s_dev;
1526 st.st_mode = body->mode;
1527 st.st_nlink = body->nlink;
1528 st.st_uid = body->uid;
1529 st.st_gid = body->gid;
1530 st.st_rdev = body->rdev;
1531 st.st_size = body->size;
1532 st.st_blksize = PAGE_CACHE_SIZE;
1533 st.st_blocks = body->blocks;
1534 st.st_atime = body->atime;
1535 st.st_mtime = body->mtime;
1536 st.st_ctime = body->ctime;
1537 st.st_ino = inode->i_ino;
1539 lmdp = (struct lov_user_mds_data *)arg;
1540 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1541 GOTO(out_req, rc = -EFAULT);
1546 ptlrpc_req_finished(request);
1548 ll_putname(filename);
1551 case IOC_LOV_GETINFO: {
1552 struct lov_user_mds_data *lumd;
1553 struct lov_stripe_md *lsm;
1554 struct lov_user_md *lum;
1555 struct lov_mds_md *lmm;
1559 lumd = (struct lov_user_mds_data *)arg;
1560 lum = &lumd->lmd_lmm;
1562 rc = ll_get_max_mdsize(sbi, &lmmsize);
1566 OBD_ALLOC_LARGE(lmm, lmmsize);
1570 if (copy_from_user(lmm, lum, lmmsize))
1571 GOTO(free_lmm, rc = -EFAULT);
1573 switch (lmm->lmm_magic) {
1574 case LOV_USER_MAGIC_V1:
1575 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1577 /* swab objects first so that stripes num will be sane */
1578 lustre_swab_lov_user_md_objects(
1579 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1580 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1581 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1583 case LOV_USER_MAGIC_V3:
1584 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1586 /* swab objects first so that stripes num will be sane */
1587 lustre_swab_lov_user_md_objects(
1588 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1589 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1590 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1593 GOTO(free_lmm, rc = -EINVAL);
1596 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1598 GOTO(free_lmm, rc = -ENOMEM);
1600 /* Perform glimpse_size operation. */
1601 memset(&st, 0, sizeof(st));
1603 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1607 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1608 GOTO(free_lsm, rc = -EFAULT);
1612 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1614 OBD_FREE_LARGE(lmm, lmmsize);
1617 case OBD_IOC_LLOG_CATINFO: {
1618 RETURN(-EOPNOTSUPP);
1620 case OBD_IOC_QUOTACHECK: {
1621 struct obd_quotactl *oqctl;
1624 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1625 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1628 OBD_ALLOC_PTR(oqctl);
1631 oqctl->qc_type = arg;
1632 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1634 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1638 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1640 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1642 OBD_FREE_PTR(oqctl);
1645 case OBD_IOC_POLL_QUOTACHECK: {
1646 struct if_quotacheck *check;
1648 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1649 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1652 OBD_ALLOC_PTR(check);
1656 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1659 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1660 if (copy_to_user((void *)arg, check,
1662 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1666 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1669 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1670 if (copy_to_user((void *)arg, check,
1672 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1676 OBD_FREE_PTR(check);
1679 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1680 case LL_IOC_QUOTACTL_18: {
1681 /* copy the old 1.x quota struct for internal use, then copy
1682 * back into old format struct. For 1.8 compatibility. */
1683 struct if_quotactl_18 *qctl_18;
1684 struct if_quotactl *qctl_20;
1686 OBD_ALLOC_PTR(qctl_18);
1690 OBD_ALLOC_PTR(qctl_20);
1692 GOTO(out_quotactl_18, rc = -ENOMEM);
1694 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1695 GOTO(out_quotactl_20, rc = -ENOMEM);
1697 QCTL_COPY(qctl_20, qctl_18);
1698 qctl_20->qc_idx = 0;
1700 /* XXX: dqb_valid was borrowed as a flag to mark that
1701 * only mds quota is wanted */
1702 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1703 qctl_18->qc_dqblk.dqb_valid) {
1704 qctl_20->qc_valid = QC_MDTIDX;
1705 qctl_20->qc_dqblk.dqb_valid = 0;
1706 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1707 qctl_20->qc_valid = QC_UUID;
1708 qctl_20->obd_uuid = qctl_18->obd_uuid;
1710 qctl_20->qc_valid = QC_GENERAL;
1713 rc = quotactl_ioctl(sbi, qctl_20);
1716 QCTL_COPY(qctl_18, qctl_20);
1717 qctl_18->obd_uuid = qctl_20->obd_uuid;
1719 if (copy_to_user((void *)arg, qctl_18,
1725 OBD_FREE_PTR(qctl_20);
1727 OBD_FREE_PTR(qctl_18);
1731 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1732 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1733 case LL_IOC_QUOTACTL: {
1734 struct if_quotactl *qctl;
1736 OBD_ALLOC_PTR(qctl);
1740 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1741 GOTO(out_quotactl, rc = -EFAULT);
1743 rc = quotactl_ioctl(sbi, qctl);
1745 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1752 case OBD_IOC_GETDTNAME:
1753 case OBD_IOC_GETMDNAME:
1754 RETURN(ll_get_obd_name(inode, cmd, arg));
1755 case LL_IOC_FLUSHCTX:
1756 RETURN(ll_flush_ctx(inode));
1757 #ifdef CONFIG_FS_POSIX_ACL
1758 case LL_IOC_RMTACL: {
1759 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1760 inode == inode->i_sb->s_root->d_inode) {
1761 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1763 LASSERT(fd != NULL);
1764 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1766 fd->fd_flags |= LL_FILE_RMTACL;
1772 case LL_IOC_GETOBDCOUNT: {
1774 struct obd_export *exp;
1776 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1779 /* get ost count when count is zero, get mdt count otherwise */
1780 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1781 vallen = sizeof(count);
1782 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1783 KEY_TGT_COUNT, &vallen, &count, NULL);
1785 CERROR("get target count failed: %d\n", rc);
1789 if (copy_to_user((int *)arg, &count, sizeof(int)))
1794 case LL_IOC_PATH2FID:
1795 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1796 sizeof(struct lu_fid)))
1799 case LL_IOC_GET_CONNECT_FLAGS: {
1800 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1802 case OBD_IOC_CHANGELOG_SEND:
1803 case OBD_IOC_CHANGELOG_CLEAR:
1804 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1805 sizeof(struct ioc_changelog));
1807 case OBD_IOC_FID2PATH:
1808 RETURN(ll_fid2path(inode, (void *)arg));
1809 case LL_IOC_HSM_REQUEST: {
1810 struct hsm_user_request *hur;
1817 /* We don't know the true size yet; copy the fixed-size part */
1818 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1823 /* Compute the whole struct size */
1824 totalsize = hur_len(hur);
1826 OBD_ALLOC_LARGE(hur, totalsize);
1830 /* Copy the whole struct */
1831 if (copy_from_user(hur, (void *)arg, totalsize)) {
1832 OBD_FREE_LARGE(hur, totalsize);
1836 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1839 OBD_FREE_LARGE(hur, totalsize);
1843 case LL_IOC_HSM_PROGRESS: {
1844 struct hsm_progress_kernel hpk;
1845 struct hsm_progress hp;
1847 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1850 hpk.hpk_fid = hp.hp_fid;
1851 hpk.hpk_cookie = hp.hp_cookie;
1852 hpk.hpk_extent = hp.hp_extent;
1853 hpk.hpk_flags = hp.hp_flags;
1854 hpk.hpk_errval = hp.hp_errval;
1855 hpk.hpk_data_version = 0;
1857 /* File may not exist in Lustre; all progress
1858 * reported to Lustre root */
1859 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1863 case LL_IOC_HSM_CT_START:
1864 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1865 sizeof(struct lustre_kernelcomm));
1868 case LL_IOC_HSM_COPY_START: {
1869 struct hsm_copy *copy;
1872 OBD_ALLOC_PTR(copy);
1875 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1880 rc = ll_ioc_copy_start(inode->i_sb, copy);
1881 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1887 case LL_IOC_HSM_COPY_END: {
1888 struct hsm_copy *copy;
1891 OBD_ALLOC_PTR(copy);
1894 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1899 rc = ll_ioc_copy_end(inode->i_sb, copy);
1900 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1907 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1912 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1914 struct inode *inode = file->f_mapping->host;
1915 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1916 struct ll_sb_info *sbi = ll_i2sbi(inode);
1917 int api32 = ll_need_32bit_api(sbi);
1918 loff_t ret = -EINVAL;
1921 mutex_lock(&inode->i_mutex);
1926 offset += file->f_pos;
1932 offset += LL_DIR_END_OFF_32BIT;
1934 offset += LL_DIR_END_OFF;
1941 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1942 (!api32 && offset <= LL_DIR_END_OFF))) {
1943 if (offset != file->f_pos) {
1944 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1945 (!api32 && offset == LL_DIR_END_OFF))
1946 fd->lfd_pos = MDS_DIR_END_OFF;
1947 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1948 fd->lfd_pos = offset << 32;
1950 fd->lfd_pos = offset;
1951 file->f_pos = offset;
1952 file->f_version = 0;
1959 mutex_unlock(&inode->i_mutex);
1963 int ll_dir_open(struct inode *inode, struct file *file)
1966 RETURN(ll_file_open(inode, file));
1969 int ll_dir_release(struct inode *inode, struct file *file)
1972 RETURN(ll_file_release(inode, file));
1975 struct file_operations ll_dir_operations = {
1976 .llseek = ll_dir_seek,
1977 .open = ll_dir_open,
1978 .release = ll_dir_release,
1979 .read = generic_read_dir,
1980 .readdir = ll_readdir,
1981 .unlocked_ioctl = ll_dir_ioctl,