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, 2012, 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>
48 #include <linux/prefetch.h>
50 #define DEBUG_SUBSYSTEM S_LLITE
52 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <obd_class.h>
55 #include <lustre_lib.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_lite.h>
58 #include <lustre_dlm.h>
59 #include <lustre_fid.h>
60 #include "llite_internal.h"
63 * (new) readdir implementation overview.
65 * Original lustre readdir implementation cached exact copy of raw directory
66 * pages on the client. These pages were indexed in client page cache by
67 * logical offset in the directory file. This design, while very simple and
68 * intuitive had some inherent problems:
70 * . it implies that byte offset to the directory entry serves as a
71 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72 * ext3/htree directory entries may move due to splits, and more
75 * . it is incompatible with the design of split directories for cmd3,
76 * that assumes that names are distributed across nodes based on their
77 * hash, and so readdir should be done in hash order.
79 * New readdir implementation does readdir in hash order, and uses hash of a
80 * file name as a telldir/seekdir cookie. This led to number of complications:
82 * . hash is not unique, so it cannot be used to index cached directory
83 * pages on the client (note, that it requires a whole pageful of hash
84 * collided entries to cause two pages to have identical hashes);
86 * . hash is not unique, so it cannot, strictly speaking, be used as an
87 * entry cookie. ext3/htree has the same problem and lustre implementation
88 * mimics their solution: seekdir(hash) positions directory at the first
89 * entry with the given hash.
95 * Client caches directory pages using hash of the first entry as an index. As
96 * noted above hash is not unique, so this solution doesn't work as is:
97 * special processing is needed for "page hash chains" (i.e., sequences of
98 * pages filled with entries all having the same hash value).
100 * First, such chains have to be detected. To this end, server returns to the
101 * client the hash of the first entry on the page next to one returned. When
102 * client detects that this hash is the same as hash of the first entry on the
103 * returned page, page hash collision has to be handled. Pages in the
104 * hash chain, except first one, are termed "overflow pages".
106 * Solution to index uniqueness problem is to not cache overflow
107 * pages. Instead, when page hash collision is detected, all overflow pages
108 * from emerging chain are immediately requested from the server and placed in
109 * a special data structure (struct ll_dir_chain). This data structure is used
110 * by ll_readdir() to process entries from overflow pages. When readdir
111 * invocation finishes, overflow pages are discarded. If page hash collision
112 * chain weren't completely processed, next call to readdir will again detect
113 * page hash collision, again read overflow pages in, process next portion of
114 * entries and again discard the pages. This is not as wasteful as it looks,
115 * because, given reasonable hash, page hash collisions are extremely rare.
117 * 1. directory positioning
119 * When seekdir(hash) is called, original
130 * identification of and access to overflow pages
134 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
135 * a header lu_dirpage which describes the start/end hash, and whether this
136 * page is empty (contains no dir entry) or hash collide with next page.
137 * After client receives reply, several pages will be integrated into dir page
138 * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
139 * lu_dirpage for this integrated page will be adjusted. See
140 * lmv_adjust_dirpages().
144 /* returns the page unlocked, but with a reference */
145 static int ll_dir_filler(void *_hash, struct page *page0)
147 struct inode *inode = page0->mapping->host;
148 int hash64 = ll_i2sbi(inode)->ll_flags & LL_SBI_64BIT_HASH;
149 struct obd_export *exp = ll_i2sbi(inode)->ll_md_exp;
150 struct ptlrpc_request *request;
151 struct mdt_body *body;
152 struct md_op_data *op_data;
153 __u64 hash = *((__u64 *)_hash);
154 struct page **page_pool;
156 struct lu_dirpage *dp;
157 int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> PAGE_CACHE_SHIFT;
158 int nrdpgs = 0; /* number of pages read actually */
164 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
165 inode->i_ino, inode->i_generation, inode, hash);
167 LASSERT(max_pages > 0 && max_pages <= MD_MAX_BRW_PAGES);
169 OBD_ALLOC(page_pool, sizeof(page) * max_pages);
170 if (page_pool != NULL) {
171 page_pool[0] = page0;
176 for (npages = 1; npages < max_pages; npages++) {
177 page = page_cache_alloc_cold(inode->i_mapping);
180 page_pool[npages] = page;
183 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
184 LUSTRE_OPC_ANY, NULL);
185 op_data->op_npages = npages;
186 op_data->op_offset = hash;
187 rc = md_readpage(exp, op_data, page_pool, &request);
188 ll_finish_md_op_data(op_data);
190 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
191 /* Checked by mdc_readpage() */
192 LASSERT(body != NULL);
194 if (body->valid & OBD_MD_FLSIZE)
195 cl_isize_write(inode, body->size);
197 nrdpgs = (request->rq_bulk->bd_nob_transferred+PAGE_CACHE_SIZE-1)
199 SetPageUptodate(page0);
202 ptlrpc_req_finished(request);
204 CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
206 ll_pagevec_init(&lru_pvec, 0);
207 for (i = 1; i < npages; i++) {
208 unsigned long offset;
213 if (rc < 0 || i >= nrdpgs) {
214 page_cache_release(page);
218 SetPageUptodate(page);
221 hash = le64_to_cpu(dp->ldp_hash_start);
224 offset = hash_x_index(hash, hash64);
226 prefetchw(&page->flags);
227 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
231 if (ll_pagevec_add(&lru_pvec, page) == 0)
232 ll_pagevec_lru_add_file(&lru_pvec);
234 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
235 " %d\n", offset, ret);
237 page_cache_release(page);
239 ll_pagevec_lru_add_file(&lru_pvec);
241 if (page_pool != &page0)
242 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
247 static void ll_check_page(struct inode *dir, struct page *page)
249 /* XXX: check page format later */
250 SetPageChecked(page);
253 void ll_release_page(struct page *page, int remove)
258 if (likely(page->mapping != NULL))
259 truncate_complete_page(page->mapping, page);
262 page_cache_release(page);
266 * Find, kmap and return page that contains given hash.
268 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
269 __u64 *start, __u64 *end)
271 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
272 struct address_space *mapping = dir->i_mapping;
274 * Complement of hash is used as an index so that
275 * radix_tree_gang_lookup() can be used to find a page with starting
276 * hash _smaller_ than one we are looking for.
278 unsigned long offset = hash_x_index(*hash, hash64);
282 TREE_READ_LOCK_IRQ(mapping);
283 found = radix_tree_gang_lookup(&mapping->page_tree,
284 (void **)&page, offset, 1);
286 struct lu_dirpage *dp;
288 page_cache_get(page);
289 TREE_READ_UNLOCK_IRQ(mapping);
291 * In contrast to find_lock_page() we are sure that directory
292 * page cannot be truncated (while DLM lock is held) and,
293 * hence, can avoid restart.
295 * In fact, page cannot be locked here at all, because
296 * ll_dir_filler() does synchronous io.
298 wait_on_page_locked(page);
299 if (PageUptodate(page)) {
301 if (BITS_PER_LONG == 32 && hash64) {
302 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
303 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
306 *start = le64_to_cpu(dp->ldp_hash_start);
307 *end = le64_to_cpu(dp->ldp_hash_end);
309 LASSERTF(*start <= *hash, "start = "LPX64",end = "
310 LPX64",hash = "LPX64"\n", *start, *end, *hash);
311 CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
312 offset, *start, *end, *hash);
314 ll_release_page(page, 0);
316 } else if (*end != *start && *hash == *end) {
318 * upon hash collision, remove this page,
319 * otherwise put page reference, and
320 * ll_get_dir_page() will issue RPC to fetch
323 ll_release_page(page,
324 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
328 page_cache_release(page);
329 page = ERR_PTR(-EIO);
333 TREE_READ_UNLOCK_IRQ(mapping);
339 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
340 struct ll_dir_chain *chain)
342 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
343 struct address_space *mapping = dir->i_mapping;
344 struct lustre_handle lockh;
345 struct lu_dirpage *dp;
352 struct ll_inode_info *lli = ll_i2info(dir);
353 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
356 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
357 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
359 struct ldlm_enqueue_info einfo = {.ei_type = LDLM_IBITS,
368 struct lookup_intent it = { .it_op = IT_READDIR };
369 struct ptlrpc_request *request;
370 struct md_op_data *op_data;
372 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
373 LUSTRE_OPC_ANY, NULL);
375 return (void *)op_data;
377 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
378 op_data, &lockh, NULL, 0, NULL, 0);
380 ll_finish_md_op_data(op_data);
382 request = (struct ptlrpc_request *)it.d.lustre.it_data;
384 ptlrpc_req_finished(request);
386 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
387 PFID(ll_inode2fid(dir)), hash, rc);
391 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
392 dir, dir->i_ino, dir->i_generation);
393 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
394 &it.d.lustre.it_lock_handle, dir, NULL);
396 /* for cross-ref object, l_ast_data of the lock may not be set,
397 * we reset it here */
398 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
401 ldlm_lock_dump_handle(D_OTHER, &lockh);
403 mutex_lock(&lli->lli_readdir_mutex);
404 page = ll_dir_page_locate(dir, &lhash, &start, &end);
406 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
407 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
408 GOTO(out_unlock, page);
409 } else if (page != NULL) {
411 * XXX nikita: not entirely correct handling of a corner case:
412 * suppose hash chain of entries with hash value HASH crosses
413 * border between pages P0 and P1. First both P0 and P1 are
414 * cached, seekdir() is called for some entry from the P0 part
415 * of the chain. Later P0 goes out of cache. telldir(HASH)
416 * happens and finds P1, as it starts with matching hash
417 * value. Remaining entries from P0 part of the chain are
418 * skipped. (Is that really a bug?)
420 * Possible solutions: 0. don't cache P1 is such case, handle
421 * it as an "overflow" page. 1. invalidate all pages at
422 * once. 2. use HASH|1 as an index for P1.
424 GOTO(hash_collision, page);
427 page = read_cache_page(mapping, hash_x_index(hash, hash64),
428 ll_dir_filler, &lhash);
430 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
431 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
432 GOTO(out_unlock, page);
435 wait_on_page_locked(page);
437 if (!PageUptodate(page)) {
438 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
439 PFID(ll_inode2fid(dir)), hash, -5);
442 if (!PageChecked(page))
443 ll_check_page(dir, page);
444 if (PageError(page)) {
445 CERROR("page error: "DFID" at "LPU64": rc %d\n",
446 PFID(ll_inode2fid(dir)), hash, -5);
450 dp = page_address(page);
451 if (BITS_PER_LONG == 32 && hash64) {
452 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
453 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
456 start = le64_to_cpu(dp->ldp_hash_start);
457 end = le64_to_cpu(dp->ldp_hash_end);
461 LASSERT(start == lhash);
462 CWARN("Page-wide hash collision: "LPU64"\n", end);
463 if (BITS_PER_LONG == 32 && hash64)
464 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
465 "] with hash "LPU64"\n",
466 le64_to_cpu(dp->ldp_hash_start),
467 le64_to_cpu(dp->ldp_hash_end), hash);
469 * Fetch whole overflow chain...
476 mutex_unlock(&lli->lli_readdir_mutex);
477 ldlm_lock_decref(&lockh, mode);
481 ll_release_page(page, 1);
482 page = ERR_PTR(-EIO);
486 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
489 struct ll_inode_info *info = ll_i2info(inode);
490 struct ll_sb_info *sbi = ll_i2sbi(inode);
492 int api32 = ll_need_32bit_api(sbi);
493 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
495 struct ll_dir_chain chain;
500 ll_dir_chain_init(&chain);
502 page = ll_get_dir_page(inode, pos, &chain);
504 while (rc == 0 && !done) {
505 struct lu_dirpage *dp;
506 struct lu_dirent *ent;
510 * If page is empty (end of directory is reached),
513 __u64 hash = MDS_DIR_END_OFF;
516 dp = page_address(page);
517 for (ent = lu_dirent_start(dp); ent != NULL && !done;
518 ent = lu_dirent_next(ent)) {
526 * XXX: implement correct swabbing here.
529 hash = le64_to_cpu(ent->lde_hash);
532 * Skip until we find target hash
537 namelen = le16_to_cpu(ent->lde_namelen);
548 fid_le_to_cpu(&fid, &ent->lde_fid);
549 ino = cl_fid_build_ino(&fid, api32);
550 type = ll_dirent_type_get(ent);
551 /* For 'll_nfs_get_name_filldir()', it will try
552 * to access the 'ent' through its 'lde_name',
553 * so the parameter 'name' for 'filldir()' must
554 * be part of the 'ent'. */
555 done = filldir(cookie, ent->lde_name, namelen,
558 next = le64_to_cpu(dp->ldp_hash_end);
561 if (pos == MDS_DIR_END_OFF) {
563 * End of directory reached.
566 ll_release_page(page, 0);
567 } else if (1 /* chain is exhausted*/) {
569 * Normal case: continue to the next
572 ll_release_page(page,
573 le32_to_cpu(dp->ldp_flags) &
576 page = ll_get_dir_page(inode, pos,
580 * go into overflow page.
582 LASSERT(le32_to_cpu(dp->ldp_flags) &
584 ll_release_page(page, 1);
588 ll_release_page(page, 0);
592 CERROR("error reading dir "DFID" at %lu: rc %d\n",
593 PFID(&info->lli_fid), (unsigned long)pos, rc);
598 ll_dir_chain_fini(&chain);
602 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
604 struct inode *inode = filp->f_dentry->d_inode;
605 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
606 struct ll_sb_info *sbi = ll_i2sbi(inode);
607 __u64 pos = lfd->lfd_pos;
608 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
609 int api32 = ll_need_32bit_api(sbi);
614 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
615 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
616 inode, (unsigned long)pos, i_size_read(inode), api32);
618 if (pos == MDS_DIR_END_OFF)
624 rc = ll_dir_read(inode, &pos, cookie, filldir);
626 if (pos == MDS_DIR_END_OFF) {
628 filp->f_pos = LL_DIR_END_OFF_32BIT;
630 filp->f_pos = LL_DIR_END_OFF;
633 filp->f_pos = pos >> 32;
637 filp->f_version = inode->i_version;
638 path.mnt = filp->f_path.mnt;
639 path.dentry = filp->f_dentry;
644 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
649 int ll_send_mgc_param(struct obd_export *mgc, char *string)
651 struct mgs_send_param *msp;
658 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
659 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
660 sizeof(struct mgs_send_param), msp, NULL);
662 CERROR("Failed to set parameter: %d\n", rc);
668 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
671 struct ptlrpc_request *request = NULL;
672 struct md_op_data *op_data;
673 struct ll_sb_info *sbi = ll_i2sbi(dir);
679 mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
680 op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
681 strlen(filename), mode, LUSTRE_OPC_MKDIR,
684 GOTO(err_exit, err = PTR_ERR(op_data));
686 op_data->op_cli_flags |= CLI_SET_MEA;
687 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
688 from_kuid(&init_user_ns, current_fsuid()),
689 from_kgid(&init_user_ns, current_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 &&
1110 uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
1111 (type == GRPQUOTA &&
1112 !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
1113 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1114 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1120 CERROR("unsupported quotactl op: %#x\n", cmd);
1124 if (valid != QC_GENERAL) {
1125 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1126 RETURN(-EOPNOTSUPP);
1128 if (cmd == Q_GETINFO)
1129 qctl->qc_cmd = Q_GETOINFO;
1130 else if (cmd == Q_GETQUOTA)
1131 qctl->qc_cmd = Q_GETOQUOTA;
1137 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1138 sizeof(*qctl), qctl, NULL);
1141 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1142 sizeof(*qctl), qctl, NULL);
1145 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1146 sizeof(*qctl), qctl, NULL);
1148 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1150 sizeof(*qctl), qctl, NULL);
1162 struct obd_quotactl *oqctl;
1164 OBD_ALLOC_PTR(oqctl);
1168 QCTL_COPY(oqctl, qctl);
1169 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1171 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1172 oqctl->qc_cmd = Q_QUOTAOFF;
1173 obd_quotactl(sbi->ll_md_exp, oqctl);
1175 OBD_FREE_PTR(oqctl);
1178 /* If QIF_SPACE is not set, client should collect the
1179 * space usage from OSSs by itself */
1180 if (cmd == Q_GETQUOTA &&
1181 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1182 !oqctl->qc_dqblk.dqb_curspace) {
1183 struct obd_quotactl *oqctl_tmp;
1185 OBD_ALLOC_PTR(oqctl_tmp);
1186 if (oqctl_tmp == NULL)
1187 GOTO(out, rc = -ENOMEM);
1189 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1190 oqctl_tmp->qc_id = oqctl->qc_id;
1191 oqctl_tmp->qc_type = oqctl->qc_type;
1193 /* collect space usage from OSTs */
1194 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1195 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1196 if (!rc || rc == -EREMOTEIO) {
1197 oqctl->qc_dqblk.dqb_curspace =
1198 oqctl_tmp->qc_dqblk.dqb_curspace;
1199 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1202 /* collect space & inode usage from MDTs */
1203 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1204 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1205 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1206 if (!rc || rc == -EREMOTEIO) {
1207 oqctl->qc_dqblk.dqb_curspace +=
1208 oqctl_tmp->qc_dqblk.dqb_curspace;
1209 oqctl->qc_dqblk.dqb_curinodes =
1210 oqctl_tmp->qc_dqblk.dqb_curinodes;
1211 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1213 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1216 OBD_FREE_PTR(oqctl_tmp);
1219 QCTL_COPY(qctl, oqctl);
1220 OBD_FREE_PTR(oqctl);
1227 ll_getname(const char __user *filename)
1230 char *tmp = __getname();
1233 return ERR_PTR(-ENOMEM);
1235 len = strncpy_from_user(tmp, filename, PATH_MAX);
1238 else if (len > PATH_MAX)
1239 ret = -ENAMETOOLONG;
1248 #define ll_putname(filename) __putname(filename)
1250 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1252 struct inode *inode = file->f_dentry->d_inode;
1253 struct ll_sb_info *sbi = ll_i2sbi(inode);
1254 struct obd_ioctl_data *data;
1258 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1259 inode->i_ino, inode->i_generation, inode, cmd);
1261 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1262 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1265 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1267 case FSFILT_IOC_GETFLAGS:
1268 case FSFILT_IOC_SETFLAGS:
1269 RETURN(ll_iocontrol(inode, file, cmd, arg));
1270 case FSFILT_IOC_GETVERSION_OLD:
1271 case FSFILT_IOC_GETVERSION:
1272 RETURN(put_user(inode->i_generation, (int *)arg));
1273 /* We need to special case any other ioctls we want to handle,
1274 * to send them to the MDS/OST as appropriate and to properly
1275 * network encode the arg field.
1276 case FSFILT_IOC_SETVERSION_OLD:
1277 case FSFILT_IOC_SETVERSION:
1279 case LL_IOC_GET_MDTIDX: {
1282 mdtidx = ll_get_mdt_idx(inode);
1286 if (put_user((int)mdtidx, (int*)arg))
1291 case IOC_MDC_LOOKUP: {
1292 struct ptlrpc_request *request = NULL;
1293 int namelen, len = 0;
1296 struct md_op_data *op_data;
1298 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1303 filename = data->ioc_inlbuf1;
1304 namelen = strlen(filename);
1307 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1308 GOTO(out_free, rc = -EINVAL);
1311 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1312 0, LUSTRE_OPC_ANY, NULL);
1313 if (IS_ERR(op_data))
1314 GOTO(out_free, rc = PTR_ERR(op_data));
1316 op_data->op_valid = OBD_MD_FLID;
1317 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1318 ll_finish_md_op_data(op_data);
1320 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1323 ptlrpc_req_finished(request);
1326 obd_ioctl_freedata(buf, len);
1329 case LL_IOC_LMV_SETSTRIPE: {
1330 struct lmv_user_md *lum;
1338 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1343 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1344 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1345 GOTO(lmv_out_free, rc = -EINVAL);
1347 filename = data->ioc_inlbuf1;
1348 namelen = data->ioc_inllen1;
1351 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1352 GOTO(lmv_out_free, rc = -EINVAL);
1354 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1355 lumlen = data->ioc_inllen2;
1357 if (lum->lum_magic != LMV_USER_MAGIC ||
1358 lumlen != sizeof(*lum)) {
1359 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1360 filename, lum->lum_magic, lumlen, -EFAULT);
1361 GOTO(lmv_out_free, rc = -EINVAL);
1365 * ll_dir_setdirstripe will be used to set dir stripe
1366 * mdc_create--->mdt_reint_create (with dirstripe)
1368 rc = ll_dir_setdirstripe(inode, lum, filename);
1370 obd_ioctl_freedata(buf, len);
1374 case LL_IOC_LOV_SETSTRIPE: {
1375 struct lov_user_md_v3 lumv3;
1376 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1377 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1378 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1380 int set_default = 0;
1382 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1383 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1384 sizeof(lumv3p->lmm_objects[0]));
1385 /* first try with v1 which is smaller than v3 */
1386 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1389 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1390 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1394 if (inode->i_sb->s_root == file->f_dentry)
1397 /* in v1 and v3 cases lumv1 points to data */
1398 rc = ll_dir_setstripe(inode, lumv1, set_default);
1402 case LL_IOC_LMV_GETSTRIPE: {
1403 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1404 struct lmv_user_md lum;
1405 struct lmv_user_md *tmp;
1410 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1413 if (lum.lum_magic != LMV_MAGIC_V1)
1416 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1417 OBD_ALLOC(tmp, lum_size);
1419 GOTO(free_lmv, rc = -ENOMEM);
1421 memcpy(tmp, &lum, sizeof(lum));
1422 tmp->lum_type = LMV_STRIPE_TYPE;
1423 tmp->lum_stripe_count = 1;
1424 mdtindex = ll_get_mdt_idx(inode);
1426 GOTO(free_lmv, rc = -ENOMEM);
1428 tmp->lum_stripe_offset = mdtindex;
1429 tmp->lum_objects[0].lum_mds = mdtindex;
1430 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1431 sizeof(struct lu_fid));
1432 if (copy_to_user((void *)arg, tmp, lum_size))
1433 GOTO(free_lmv, rc = -EFAULT);
1436 OBD_FREE(tmp, lum_size);
1439 case LL_IOC_REMOVE_ENTRY: {
1440 char *filename = NULL;
1444 /* Here is a little hack to avoid sending REINT_RMENTRY to
1445 * unsupported server, which might crash the server(LU-2730),
1446 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1447 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1448 * server will support REINT_RMENTRY XXX*/
1449 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1452 filename = ll_getname((const char *)arg);
1453 if (IS_ERR(filename))
1454 RETURN(PTR_ERR(filename));
1456 namelen = strlen(filename);
1458 GOTO(out_rmdir, rc = -EINVAL);
1460 rc = ll_rmdir_entry(inode, filename, namelen);
1463 ll_putname(filename);
1466 case LL_IOC_LOV_SWAP_LAYOUTS:
1468 case LL_IOC_OBD_STATFS:
1469 RETURN(ll_obd_statfs(inode, (void *)arg));
1470 case LL_IOC_LOV_GETSTRIPE:
1471 case LL_IOC_MDC_GETINFO:
1472 case IOC_MDC_GETFILEINFO:
1473 case IOC_MDC_GETFILESTRIPE: {
1474 struct ptlrpc_request *request = NULL;
1475 struct lov_user_md *lump;
1476 struct lov_mds_md *lmm = NULL;
1477 struct mdt_body *body;
1478 char *filename = NULL;
1481 if (cmd == IOC_MDC_GETFILEINFO ||
1482 cmd == IOC_MDC_GETFILESTRIPE) {
1483 filename = ll_getname((const char *)arg);
1484 if (IS_ERR(filename))
1485 RETURN(PTR_ERR(filename));
1487 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1488 &lmmsize, &request);
1490 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1494 body = req_capsule_server_get(&request->rq_pill,
1496 LASSERT(body != NULL);
1502 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1503 cmd == LL_IOC_MDC_GETINFO))
1504 GOTO(skip_lmm, rc = 0);
1509 if (cmd == IOC_MDC_GETFILESTRIPE ||
1510 cmd == LL_IOC_LOV_GETSTRIPE) {
1511 lump = (struct lov_user_md *)arg;
1513 struct lov_user_mds_data *lmdp;
1514 lmdp = (struct lov_user_mds_data *)arg;
1515 lump = &lmdp->lmd_lmm;
1517 if (copy_to_user(lump, lmm, lmmsize)) {
1518 if (copy_to_user(lump, lmm, sizeof(*lump)))
1519 GOTO(out_req, rc = -EFAULT);
1523 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1524 struct lov_user_mds_data *lmdp;
1527 st.st_dev = inode->i_sb->s_dev;
1528 st.st_mode = body->mode;
1529 st.st_nlink = body->nlink;
1530 st.st_uid = body->uid;
1531 st.st_gid = body->gid;
1532 st.st_rdev = body->rdev;
1533 st.st_size = body->size;
1534 st.st_blksize = PAGE_CACHE_SIZE;
1535 st.st_blocks = body->blocks;
1536 st.st_atime = body->atime;
1537 st.st_mtime = body->mtime;
1538 st.st_ctime = body->ctime;
1539 st.st_ino = inode->i_ino;
1541 lmdp = (struct lov_user_mds_data *)arg;
1542 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1543 GOTO(out_req, rc = -EFAULT);
1548 ptlrpc_req_finished(request);
1550 ll_putname(filename);
1553 case IOC_LOV_GETINFO: {
1554 struct lov_user_mds_data *lumd;
1555 struct lov_stripe_md *lsm;
1556 struct lov_user_md *lum;
1557 struct lov_mds_md *lmm;
1561 lumd = (struct lov_user_mds_data *)arg;
1562 lum = &lumd->lmd_lmm;
1564 rc = ll_get_max_mdsize(sbi, &lmmsize);
1568 OBD_ALLOC_LARGE(lmm, lmmsize);
1569 if (copy_from_user(lmm, lum, lmmsize))
1570 GOTO(free_lmm, rc = -EFAULT);
1572 switch (lmm->lmm_magic) {
1573 case LOV_USER_MAGIC_V1:
1574 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1576 /* swab objects first so that stripes num will be sane */
1577 lustre_swab_lov_user_md_objects(
1578 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1579 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1580 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1582 case LOV_USER_MAGIC_V3:
1583 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1585 /* swab objects first so that stripes num will be sane */
1586 lustre_swab_lov_user_md_objects(
1587 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1588 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1589 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1592 GOTO(free_lmm, rc = -EINVAL);
1595 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1597 GOTO(free_lmm, rc = -ENOMEM);
1599 /* Perform glimpse_size operation. */
1600 memset(&st, 0, sizeof(st));
1602 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1606 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1607 GOTO(free_lsm, rc = -EFAULT);
1611 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1613 OBD_FREE_LARGE(lmm, lmmsize);
1616 case OBD_IOC_LLOG_CATINFO: {
1617 RETURN(-EOPNOTSUPP);
1619 case OBD_IOC_QUOTACHECK: {
1620 struct obd_quotactl *oqctl;
1623 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1624 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1627 OBD_ALLOC_PTR(oqctl);
1630 oqctl->qc_type = arg;
1631 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1633 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1637 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1639 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1641 OBD_FREE_PTR(oqctl);
1644 case OBD_IOC_POLL_QUOTACHECK: {
1645 struct if_quotacheck *check;
1647 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1648 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1651 OBD_ALLOC_PTR(check);
1655 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1658 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1659 if (copy_to_user((void *)arg, check,
1661 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1665 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1668 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1669 if (copy_to_user((void *)arg, check,
1671 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1675 OBD_FREE_PTR(check);
1678 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1679 case LL_IOC_QUOTACTL_18: {
1680 /* copy the old 1.x quota struct for internal use, then copy
1681 * back into old format struct. For 1.8 compatibility. */
1682 struct if_quotactl_18 *qctl_18;
1683 struct if_quotactl *qctl_20;
1685 OBD_ALLOC_PTR(qctl_18);
1689 OBD_ALLOC_PTR(qctl_20);
1691 GOTO(out_quotactl_18, rc = -ENOMEM);
1693 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1694 GOTO(out_quotactl_20, rc = -ENOMEM);
1696 QCTL_COPY(qctl_20, qctl_18);
1697 qctl_20->qc_idx = 0;
1699 /* XXX: dqb_valid was borrowed as a flag to mark that
1700 * only mds quota is wanted */
1701 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1702 qctl_18->qc_dqblk.dqb_valid) {
1703 qctl_20->qc_valid = QC_MDTIDX;
1704 qctl_20->qc_dqblk.dqb_valid = 0;
1705 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1706 qctl_20->qc_valid = QC_UUID;
1707 qctl_20->obd_uuid = qctl_18->obd_uuid;
1709 qctl_20->qc_valid = QC_GENERAL;
1712 rc = quotactl_ioctl(sbi, qctl_20);
1715 QCTL_COPY(qctl_18, qctl_20);
1716 qctl_18->obd_uuid = qctl_20->obd_uuid;
1718 if (copy_to_user((void *)arg, qctl_18,
1724 OBD_FREE_PTR(qctl_20);
1726 OBD_FREE_PTR(qctl_18);
1730 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1731 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1732 case LL_IOC_QUOTACTL: {
1733 struct if_quotactl *qctl;
1735 OBD_ALLOC_PTR(qctl);
1739 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1740 GOTO(out_quotactl, rc = -EFAULT);
1742 rc = quotactl_ioctl(sbi, qctl);
1744 if (rc == 0 && copy_to_user((void *)arg,qctl,sizeof(*qctl)))
1751 case OBD_IOC_GETDTNAME:
1752 case OBD_IOC_GETMDNAME:
1753 RETURN(ll_get_obd_name(inode, cmd, arg));
1754 case LL_IOC_FLUSHCTX:
1755 RETURN(ll_flush_ctx(inode));
1756 #ifdef CONFIG_FS_POSIX_ACL
1757 case LL_IOC_RMTACL: {
1758 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1759 inode == inode->i_sb->s_root->d_inode) {
1760 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1762 LASSERT(fd != NULL);
1763 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1765 fd->fd_flags |= LL_FILE_RMTACL;
1771 case LL_IOC_GETOBDCOUNT: {
1773 struct obd_export *exp;
1775 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1778 /* get ost count when count is zero, get mdt count otherwise */
1779 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1780 vallen = sizeof(count);
1781 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1782 KEY_TGT_COUNT, &vallen, &count, NULL);
1784 CERROR("get target count failed: %d\n", rc);
1788 if (copy_to_user((int *)arg, &count, sizeof(int)))
1793 case LL_IOC_PATH2FID:
1794 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1795 sizeof(struct lu_fid)))
1798 case LL_IOC_GET_CONNECT_FLAGS: {
1799 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1801 case OBD_IOC_CHANGELOG_SEND:
1802 case OBD_IOC_CHANGELOG_CLEAR:
1803 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1804 sizeof(struct ioc_changelog));
1806 case OBD_IOC_FID2PATH:
1807 RETURN(ll_fid2path(inode, (void *)arg));
1808 case LL_IOC_HSM_REQUEST: {
1809 struct hsm_user_request *hur;
1816 /* We don't know the true size yet; copy the fixed-size part */
1817 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1822 /* Compute the whole struct size */
1823 totalsize = hur_len(hur);
1825 OBD_ALLOC_LARGE(hur, totalsize);
1829 /* Copy the whole struct */
1830 if (copy_from_user(hur, (void *)arg, totalsize)) {
1831 OBD_FREE_LARGE(hur, totalsize);
1835 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1838 OBD_FREE_LARGE(hur, totalsize);
1842 case LL_IOC_HSM_PROGRESS: {
1843 struct hsm_progress_kernel hpk;
1844 struct hsm_progress hp;
1846 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1849 hpk.hpk_fid = hp.hp_fid;
1850 hpk.hpk_cookie = hp.hp_cookie;
1851 hpk.hpk_extent = hp.hp_extent;
1852 hpk.hpk_flags = hp.hp_flags;
1853 hpk.hpk_errval = hp.hp_errval;
1854 hpk.hpk_data_version = 0;
1856 /* File may not exist in Lustre; all progress
1857 * reported to Lustre root */
1858 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1862 case LL_IOC_HSM_CT_START:
1863 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1864 sizeof(struct lustre_kernelcomm));
1867 case LL_IOC_HSM_COPY_START: {
1868 struct hsm_copy *copy;
1871 OBD_ALLOC_PTR(copy);
1874 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1879 rc = ll_ioc_copy_start(inode->i_sb, copy);
1880 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1886 case LL_IOC_HSM_COPY_END: {
1887 struct hsm_copy *copy;
1890 OBD_ALLOC_PTR(copy);
1893 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1898 rc = ll_ioc_copy_end(inode->i_sb, copy);
1899 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1906 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1911 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1913 struct inode *inode = file->f_mapping->host;
1914 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1915 struct ll_sb_info *sbi = ll_i2sbi(inode);
1916 int api32 = ll_need_32bit_api(sbi);
1917 loff_t ret = -EINVAL;
1920 mutex_lock(&inode->i_mutex);
1925 offset += file->f_pos;
1931 offset += LL_DIR_END_OFF_32BIT;
1933 offset += LL_DIR_END_OFF;
1940 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1941 (!api32 && offset <= LL_DIR_END_OFF))) {
1942 if (offset != file->f_pos) {
1943 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1944 (!api32 && offset == LL_DIR_END_OFF))
1945 fd->lfd_pos = MDS_DIR_END_OFF;
1946 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1947 fd->lfd_pos = offset << 32;
1949 fd->lfd_pos = offset;
1950 file->f_pos = offset;
1951 file->f_version = 0;
1958 mutex_unlock(&inode->i_mutex);
1962 int ll_dir_open(struct inode *inode, struct file *file)
1965 RETURN(ll_file_open(inode, file));
1968 int ll_dir_release(struct inode *inode, struct file *file)
1971 RETURN(ll_file_release(inode, file));
1974 struct file_operations ll_dir_operations = {
1975 .llseek = ll_dir_seek,
1976 .open = ll_dir_open,
1977 .release = ll_dir_release,
1978 .read = generic_read_dir,
1979 .readdir = ll_readdir,
1980 .unlocked_ioctl = ll_dir_ioctl,