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 CFS_PAGE_SIZE (if CFS_PAGE_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 #ifndef HAVE_ADD_TO_PAGE_CACHE_LRU
156 struct pagevec lru_pvec;
158 struct lu_dirpage *dp;
159 int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> CFS_PAGE_SHIFT;
160 int nrdpgs = 0; /* number of pages read actually */
166 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
167 inode->i_ino, inode->i_generation, inode, hash);
169 LASSERT(max_pages > 0 && max_pages <= MD_MAX_BRW_PAGES);
171 OBD_ALLOC(page_pool, sizeof(page) * max_pages);
172 if (page_pool != NULL) {
173 page_pool[0] = page0;
178 for (npages = 1; npages < max_pages; npages++) {
179 page = page_cache_alloc_cold(inode->i_mapping);
182 page_pool[npages] = page;
185 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
186 LUSTRE_OPC_ANY, NULL);
187 op_data->op_npages = npages;
188 op_data->op_offset = hash;
189 rc = md_readpage(exp, op_data, page_pool, &request);
190 ll_finish_md_op_data(op_data);
192 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
193 /* Checked by mdc_readpage() */
194 LASSERT(body != NULL);
196 if (body->valid & OBD_MD_FLSIZE)
197 cl_isize_write(inode, body->size);
199 nrdpgs = (request->rq_bulk->bd_nob_transferred+CFS_PAGE_SIZE-1)
201 SetPageUptodate(page0);
204 ptlrpc_req_finished(request);
206 CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
208 ll_pagevec_init(&lru_pvec, 0);
209 for (i = 1; i < npages; i++) {
210 unsigned long offset;
215 if (rc < 0 || i >= nrdpgs) {
216 page_cache_release(page);
220 SetPageUptodate(page);
223 hash = le64_to_cpu(dp->ldp_hash_start);
226 offset = hash_x_index(hash, hash64);
228 prefetchw(&page->flags);
229 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
233 if (ll_pagevec_add(&lru_pvec, page) == 0)
234 ll_pagevec_lru_add_file(&lru_pvec);
236 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
237 " %d\n", offset, ret);
239 page_cache_release(page);
241 ll_pagevec_lru_add_file(&lru_pvec);
243 if (page_pool != &page0)
244 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
249 static void ll_check_page(struct inode *dir, struct page *page)
251 /* XXX: check page format later */
252 SetPageChecked(page);
255 void ll_release_page(struct page *page, int remove)
260 if (likely(page->mapping != NULL))
261 truncate_complete_page(page->mapping, page);
264 page_cache_release(page);
268 * Find, kmap and return page that contains given hash.
270 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
271 __u64 *start, __u64 *end)
273 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
274 struct address_space *mapping = dir->i_mapping;
276 * Complement of hash is used as an index so that
277 * radix_tree_gang_lookup() can be used to find a page with starting
278 * hash _smaller_ than one we are looking for.
280 unsigned long offset = hash_x_index(*hash, hash64);
284 TREE_READ_LOCK_IRQ(mapping);
285 found = radix_tree_gang_lookup(&mapping->page_tree,
286 (void **)&page, offset, 1);
288 struct lu_dirpage *dp;
290 page_cache_get(page);
291 TREE_READ_UNLOCK_IRQ(mapping);
293 * In contrast to find_lock_page() we are sure that directory
294 * page cannot be truncated (while DLM lock is held) and,
295 * hence, can avoid restart.
297 * In fact, page cannot be locked here at all, because
298 * ll_dir_filler() does synchronous io.
301 if (PageUptodate(page)) {
303 if (BITS_PER_LONG == 32 && hash64) {
304 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
305 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
308 *start = le64_to_cpu(dp->ldp_hash_start);
309 *end = le64_to_cpu(dp->ldp_hash_end);
311 LASSERTF(*start <= *hash, "start = "LPX64",end = "
312 LPX64",hash = "LPX64"\n", *start, *end, *hash);
313 CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
314 offset, *start, *end, *hash);
316 ll_release_page(page, 0);
318 } else if (*end != *start && *hash == *end) {
320 * upon hash collision, remove this page,
321 * otherwise put page reference, and
322 * ll_get_dir_page() will issue RPC to fetch
325 ll_release_page(page,
326 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
330 page_cache_release(page);
331 page = ERR_PTR(-EIO);
335 TREE_READ_UNLOCK_IRQ(mapping);
341 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
342 struct ll_dir_chain *chain)
344 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
345 struct address_space *mapping = dir->i_mapping;
346 struct lustre_handle lockh;
347 struct lu_dirpage *dp;
354 struct ll_inode_info *lli = ll_i2info(dir);
355 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
358 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
359 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
361 struct ldlm_enqueue_info einfo = {.ei_type = LDLM_IBITS,
370 struct lookup_intent it = { .it_op = IT_READDIR };
371 struct ptlrpc_request *request;
372 struct md_op_data *op_data;
374 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
375 LUSTRE_OPC_ANY, NULL);
377 return (void *)op_data;
379 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
380 op_data, &lockh, NULL, 0, NULL, 0);
382 ll_finish_md_op_data(op_data);
384 request = (struct ptlrpc_request *)it.d.lustre.it_data;
386 ptlrpc_req_finished(request);
388 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
389 PFID(ll_inode2fid(dir)), hash, rc);
393 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
394 dir, dir->i_ino, dir->i_generation);
395 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
396 &it.d.lustre.it_lock_handle, dir, NULL);
398 /* for cross-ref object, l_ast_data of the lock may not be set,
399 * we reset it here */
400 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
403 ldlm_lock_dump_handle(D_OTHER, &lockh);
405 mutex_lock(&lli->lli_readdir_mutex);
406 page = ll_dir_page_locate(dir, &lhash, &start, &end);
408 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
409 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
410 GOTO(out_unlock, page);
411 } else if (page != NULL) {
413 * XXX nikita: not entirely correct handling of a corner case:
414 * suppose hash chain of entries with hash value HASH crosses
415 * border between pages P0 and P1. First both P0 and P1 are
416 * cached, seekdir() is called for some entry from the P0 part
417 * of the chain. Later P0 goes out of cache. telldir(HASH)
418 * happens and finds P1, as it starts with matching hash
419 * value. Remaining entries from P0 part of the chain are
420 * skipped. (Is that really a bug?)
422 * Possible solutions: 0. don't cache P1 is such case, handle
423 * it as an "overflow" page. 1. invalidate all pages at
424 * once. 2. use HASH|1 as an index for P1.
426 GOTO(hash_collision, page);
429 page = read_cache_page(mapping, hash_x_index(hash, hash64),
430 ll_dir_filler, &lhash);
432 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
433 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
434 GOTO(out_unlock, page);
439 if (!PageUptodate(page)) {
440 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
441 PFID(ll_inode2fid(dir)), hash, -5);
444 if (!PageChecked(page))
445 ll_check_page(dir, page);
446 if (PageError(page)) {
447 CERROR("page error: "DFID" at "LPU64": rc %d\n",
448 PFID(ll_inode2fid(dir)), hash, -5);
452 dp = page_address(page);
453 if (BITS_PER_LONG == 32 && hash64) {
454 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
455 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
458 start = le64_to_cpu(dp->ldp_hash_start);
459 end = le64_to_cpu(dp->ldp_hash_end);
463 LASSERT(start == lhash);
464 CWARN("Page-wide hash collision: "LPU64"\n", end);
465 if (BITS_PER_LONG == 32 && hash64)
466 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
467 "] with hash "LPU64"\n",
468 le64_to_cpu(dp->ldp_hash_start),
469 le64_to_cpu(dp->ldp_hash_end), hash);
471 * Fetch whole overflow chain...
478 mutex_unlock(&lli->lli_readdir_mutex);
479 ldlm_lock_decref(&lockh, mode);
483 ll_release_page(page, 1);
484 page = ERR_PTR(-EIO);
488 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
491 struct ll_inode_info *info = ll_i2info(inode);
492 struct ll_sb_info *sbi = ll_i2sbi(inode);
494 int api32 = ll_need_32bit_api(sbi);
495 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
497 struct ll_dir_chain chain;
502 ll_dir_chain_init(&chain);
504 page = ll_get_dir_page(inode, pos, &chain);
506 while (rc == 0 && !done) {
507 struct lu_dirpage *dp;
508 struct lu_dirent *ent;
512 * If page is empty (end of directory is reached),
515 __u64 hash = MDS_DIR_END_OFF;
518 dp = page_address(page);
519 for (ent = lu_dirent_start(dp); ent != NULL && !done;
520 ent = lu_dirent_next(ent)) {
528 * XXX: implement correct swabbing here.
531 hash = le64_to_cpu(ent->lde_hash);
534 * Skip until we find target hash
539 namelen = le16_to_cpu(ent->lde_namelen);
550 fid_le_to_cpu(&fid, &ent->lde_fid);
551 ino = cl_fid_build_ino(&fid, api32);
552 type = ll_dirent_type_get(ent);
553 /* For 'll_nfs_get_name_filldir()', it will try
554 * to access the 'ent' through its 'lde_name',
555 * so the parameter 'name' for 'filldir()' must
556 * be part of the 'ent'. */
557 done = filldir(cookie, ent->lde_name, namelen,
560 next = le64_to_cpu(dp->ldp_hash_end);
563 if (pos == MDS_DIR_END_OFF) {
565 * End of directory reached.
568 ll_release_page(page, 0);
569 } else if (1 /* chain is exhausted*/) {
571 * Normal case: continue to the next
574 ll_release_page(page,
575 le32_to_cpu(dp->ldp_flags) &
578 page = ll_get_dir_page(inode, pos,
582 * go into overflow page.
584 LASSERT(le32_to_cpu(dp->ldp_flags) &
586 ll_release_page(page, 1);
590 ll_release_page(page, 0);
594 CERROR("error reading dir "DFID" at %lu: rc %d\n",
595 PFID(&info->lli_fid), (unsigned long)pos, rc);
600 ll_dir_chain_fini(&chain);
604 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
606 struct inode *inode = filp->f_dentry->d_inode;
607 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
608 struct ll_sb_info *sbi = ll_i2sbi(inode);
609 __u64 pos = lfd->lfd_pos;
610 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
611 int api32 = ll_need_32bit_api(sbi);
613 #ifdef HAVE_TOUCH_ATIME_1ARG
618 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
619 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
620 inode, (unsigned long)pos, i_size_read(inode), api32);
622 if (pos == MDS_DIR_END_OFF)
628 rc = ll_dir_read(inode, &pos, cookie, filldir);
630 if (pos == MDS_DIR_END_OFF) {
632 filp->f_pos = LL_DIR_END_OFF_32BIT;
634 filp->f_pos = LL_DIR_END_OFF;
637 filp->f_pos = pos >> 32;
641 filp->f_version = inode->i_version;
642 #ifdef HAVE_TOUCH_ATIME_1ARG
643 path.mnt = filp->f_vfsmnt;
644 path.dentry = filp->f_dentry;
647 touch_atime(filp->f_vfsmnt, filp->f_dentry);
652 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
657 int ll_send_mgc_param(struct obd_export *mgc, char *string)
659 struct mgs_send_param *msp;
666 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
667 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
668 sizeof(struct mgs_send_param), msp, NULL);
670 CERROR("Failed to set parameter: %d\n", rc);
676 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
679 struct ptlrpc_request *request = NULL;
680 struct md_op_data *op_data;
681 struct ll_sb_info *sbi = ll_i2sbi(dir);
687 mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
688 op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
689 strlen(filename), mode, LUSTRE_OPC_MKDIR,
692 GOTO(err_exit, err = PTR_ERR(op_data));
694 op_data->op_cli_flags |= CLI_SET_MEA;
695 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
696 cfs_curproc_fsuid(), cfs_curproc_fsgid(),
697 cfs_curproc_cap_pack(), 0, &request);
698 ll_finish_md_op_data(op_data);
702 ptlrpc_req_finished(request);
706 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
709 struct ll_sb_info *sbi = ll_i2sbi(inode);
710 struct md_op_data *op_data;
711 struct ptlrpc_request *req = NULL;
713 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
714 struct obd_device *mgc = lsi->lsi_mgc;
720 * This is coming from userspace, so should be in
721 * local endian. But the MDS would like it in little
722 * endian, so we swab it before we send it.
724 switch (lump->lmm_magic) {
725 case LOV_USER_MAGIC_V1: {
726 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
727 lustre_swab_lov_user_md_v1(lump);
728 lum_size = sizeof(struct lov_user_md_v1);
731 case LOV_USER_MAGIC_V3: {
732 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
733 lustre_swab_lov_user_md_v3(
734 (struct lov_user_md_v3 *)lump);
735 lum_size = sizeof(struct lov_user_md_v3);
739 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
740 " %#08x != %#08x nor %#08x\n",
741 lump->lmm_magic, LOV_USER_MAGIC_V1,
747 lum_size = sizeof(struct lov_user_md_v1);
750 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
751 LUSTRE_OPC_ANY, NULL);
753 RETURN(PTR_ERR(op_data));
755 if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
756 op_data->op_cli_flags |= CLI_SET_MEA;
758 /* swabbing is done in lov_setstripe() on server side */
759 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
760 NULL, 0, &req, NULL);
761 ll_finish_md_op_data(op_data);
762 ptlrpc_req_finished(req);
764 if (rc != -EPERM && rc != -EACCES)
765 CERROR("mdc_setattr fails: rc = %d\n", rc);
768 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
769 LOV_USER_MAGIC_V3 have the same initial fields so we do not
770 need the make the distiction between the 2 versions */
771 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
775 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
777 GOTO(end, rc = -ENOMEM);
780 /* Get fsname and assume devname to be -MDT0000. */
781 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
782 strcat(buf, "-MDT0000.lov");
785 /* Set root stripesize */
786 sprintf(buf, ".stripesize=%u",
787 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
788 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
792 /* Set root stripecount */
793 sprintf(buf, ".stripecount=%hd",
794 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
795 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
799 /* Set root stripeoffset */
800 sprintf(buf, ".stripeoffset=%hd",
801 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
802 (typeof(lump->lmm_stripe_offset))(-1));
803 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
807 OBD_FREE(param, MGS_PARAM_MAXLEN);
812 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
813 int *lmm_size, struct ptlrpc_request **request)
815 struct ll_sb_info *sbi = ll_i2sbi(inode);
816 struct mdt_body *body;
817 struct lov_mds_md *lmm = NULL;
818 struct ptlrpc_request *req = NULL;
820 struct md_op_data *op_data;
822 rc = ll_get_max_mdsize(sbi, &lmmsize);
826 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
827 0, lmmsize, LUSTRE_OPC_ANY,
830 RETURN(PTR_ERR(op_data));
832 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
833 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
834 ll_finish_md_op_data(op_data);
836 CDEBUG(D_INFO, "md_getattr failed on inode "
837 "%lu/%u: rc %d\n", inode->i_ino,
838 inode->i_generation, rc);
842 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
843 LASSERT(body != NULL);
845 lmmsize = body->eadatasize;
847 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
849 GOTO(out, rc = -ENODATA);
852 lmm = req_capsule_server_sized_get(&req->rq_pill,
853 &RMF_MDT_MD, lmmsize);
854 LASSERT(lmm != NULL);
857 * This is coming from the MDS, so is probably in
858 * little endian. We convert it to host endian before
859 * passing it to userspace.
861 /* We don't swab objects for directories */
862 switch (le32_to_cpu(lmm->lmm_magic)) {
864 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
865 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
868 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
869 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
872 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
883 * Get MDT index for the inode.
885 int ll_get_mdt_idx(struct inode *inode)
887 struct ll_sb_info *sbi = ll_i2sbi(inode);
888 struct md_op_data *op_data;
892 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
893 0, LUSTRE_OPC_ANY, NULL);
895 RETURN(PTR_ERR(op_data));
897 op_data->op_flags |= MF_GET_MDT_IDX;
898 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
899 mdtidx = op_data->op_mds;
900 ll_finish_md_op_data(op_data);
902 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
909 * Generic handler to do any pre-copy work.
911 * It send a first hsm_progress (with extent length == 0) to coordinator as a
912 * first information for it that real work has started.
914 * Moreover, for a ARCHIVE request, it will sample the file data version and
915 * store it in \a copy.
917 * \return 0 on success.
919 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
921 struct ll_sb_info *sbi = ll_s2sbi(sb);
922 struct hsm_progress_kernel hpk;
926 /* Forge a hsm_progress based on data from copy. */
927 hpk.hpk_fid = copy->hc_hai.hai_fid;
928 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
929 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
930 hpk.hpk_extent.length = 0;
933 hpk.hpk_data_version = 0;
936 /* For archive request, we need to read the current file version. */
937 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
939 __u64 data_version = 0;
941 /* Get inode for this fid */
942 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
944 hpk.hpk_flags |= HP_FLAG_RETRY;
945 /* hpk_errval is >= 0 */
946 hpk.hpk_errval = -PTR_ERR(inode);
947 GOTO(progress, rc = PTR_ERR(inode));
950 /* Read current file data version */
951 rc = ll_data_version(inode, &data_version, 1);
954 CDEBUG(D_HSM, "Could not read file data version of "
955 DFID" (rc = %d). Archive request ("
956 LPX64") could not be done.\n",
957 PFID(©->hc_hai.hai_fid), rc,
958 copy->hc_hai.hai_cookie);
959 hpk.hpk_flags |= HP_FLAG_RETRY;
960 /* hpk_errval must be >= 0 */
961 hpk.hpk_errval = -rc;
965 /* Store it the hsm_copy for later copytool use.
966 * Always modified even if no lsm. */
967 copy->hc_data_version = data_version;
971 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
978 * Generic handler to do any post-copy work.
980 * It will send the last hsm_progress update to coordinator to inform it
981 * that copy is finished and whether it was successful or not.
984 * - for ARCHIVE request, it will sample the file data version and compare it
985 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
986 * will be considered as failed.
987 * - for RESTORE request, it will sample the file data version and send it to
988 * coordinator which is useful if the file was imported as 'released'.
990 * \return 0 on success.
992 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
994 struct ll_sb_info *sbi = ll_s2sbi(sb);
995 struct hsm_progress_kernel hpk;
999 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
1000 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
1001 * initialized if copy_end was called with copy == NULL.
1004 /* Forge a hsm_progress based on data from copy. */
1005 hpk.hpk_fid = copy->hc_hai.hai_fid;
1006 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1007 hpk.hpk_extent = copy->hc_hai.hai_extent;
1008 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1009 hpk.hpk_errval = copy->hc_errval;
1010 hpk.hpk_data_version = 0;
1012 /* For archive request, we need to check the file data was not changed.
1014 * For restore request, we need to send the file data version, this is
1015 * useful when the file was created using hsm_import.
1017 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1018 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1019 (copy->hc_errval == 0)) {
1020 struct inode *inode;
1021 __u64 data_version = 0;
1023 /* Get lsm for this fid */
1024 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1025 if (IS_ERR(inode)) {
1026 hpk.hpk_flags |= HP_FLAG_RETRY;
1027 /* hpk_errval must be >= 0 */
1028 hpk.hpk_errval = -PTR_ERR(inode);
1029 GOTO(progress, rc = PTR_ERR(inode));
1032 rc = ll_data_version(inode, &data_version,
1033 copy->hc_hai.hai_action == HSMA_ARCHIVE);
1036 CDEBUG(D_HSM, "Could not read file data version. "
1037 "Request could not be confirmed.\n");
1038 if (hpk.hpk_errval == 0)
1039 hpk.hpk_errval = -rc;
1043 /* Store it the hsm_copy for later copytool use.
1044 * Always modified even if no lsm. */
1045 hpk.hpk_data_version = data_version;
1047 /* File could have been stripped during archiving, so we need
1048 * to check anyway. */
1049 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1050 (copy->hc_data_version != data_version)) {
1051 CDEBUG(D_HSM, "File data version mismatched. "
1052 "File content was changed during archiving. "
1053 DFID", start:"LPX64" current:"LPX64"\n",
1054 PFID(©->hc_hai.hai_fid),
1055 copy->hc_data_version, data_version);
1056 /* File was changed, send error to cdt. Do not ask for
1057 * retry because if a file is modified frequently,
1058 * the cdt will loop on retried archive requests.
1059 * The policy engine will ask for a new archive later
1060 * when the file will not be modified for some tunable
1062 /* we do not notify caller */
1063 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1064 /* hpk_errval must be >= 0 */
1065 hpk.hpk_errval = EBUSY;
1071 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1078 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
1083 OBD_ALLOC(ptr, len);
1086 if (cfs_copy_from_user(ptr, data, len)) {
1090 rc = obd_iocontrol(cmd, exp, len, data, NULL);
1095 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1097 int cmd = qctl->qc_cmd;
1098 int type = qctl->qc_type;
1099 int id = qctl->qc_id;
1100 int valid = qctl->qc_valid;
1105 case LUSTRE_Q_INVALIDATE:
1106 case LUSTRE_Q_FINVALIDATE:
1111 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1112 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1116 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1117 (type == GRPQUOTA && !in_egroup_p(id))) &&
1118 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1119 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1125 CERROR("unsupported quotactl op: %#x\n", cmd);
1129 if (valid != QC_GENERAL) {
1130 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1131 RETURN(-EOPNOTSUPP);
1133 if (cmd == Q_GETINFO)
1134 qctl->qc_cmd = Q_GETOINFO;
1135 else if (cmd == Q_GETQUOTA)
1136 qctl->qc_cmd = Q_GETOQUOTA;
1142 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1143 sizeof(*qctl), qctl, NULL);
1146 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1147 sizeof(*qctl), qctl, NULL);
1150 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1151 sizeof(*qctl), qctl, NULL);
1153 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1155 sizeof(*qctl), qctl, NULL);
1167 struct obd_quotactl *oqctl;
1169 OBD_ALLOC_PTR(oqctl);
1173 QCTL_COPY(oqctl, qctl);
1174 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1176 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1177 oqctl->qc_cmd = Q_QUOTAOFF;
1178 obd_quotactl(sbi->ll_md_exp, oqctl);
1180 OBD_FREE_PTR(oqctl);
1183 /* If QIF_SPACE is not set, client should collect the
1184 * space usage from OSSs by itself */
1185 if (cmd == Q_GETQUOTA &&
1186 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1187 !oqctl->qc_dqblk.dqb_curspace) {
1188 struct obd_quotactl *oqctl_tmp;
1190 OBD_ALLOC_PTR(oqctl_tmp);
1191 if (oqctl_tmp == NULL)
1192 GOTO(out, rc = -ENOMEM);
1194 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1195 oqctl_tmp->qc_id = oqctl->qc_id;
1196 oqctl_tmp->qc_type = oqctl->qc_type;
1198 /* collect space usage from OSTs */
1199 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1200 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1201 if (!rc || rc == -EREMOTEIO) {
1202 oqctl->qc_dqblk.dqb_curspace =
1203 oqctl_tmp->qc_dqblk.dqb_curspace;
1204 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1207 /* collect space & inode usage from MDTs */
1208 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1209 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1210 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1211 if (!rc || rc == -EREMOTEIO) {
1212 oqctl->qc_dqblk.dqb_curspace +=
1213 oqctl_tmp->qc_dqblk.dqb_curspace;
1214 oqctl->qc_dqblk.dqb_curinodes =
1215 oqctl_tmp->qc_dqblk.dqb_curinodes;
1216 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1218 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1221 OBD_FREE_PTR(oqctl_tmp);
1224 QCTL_COPY(qctl, oqctl);
1225 OBD_FREE_PTR(oqctl);
1231 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1233 struct inode *inode = file->f_dentry->d_inode;
1234 struct ll_sb_info *sbi = ll_i2sbi(inode);
1235 struct obd_ioctl_data *data;
1239 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1240 inode->i_ino, inode->i_generation, inode, cmd);
1242 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1243 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1246 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1248 case FSFILT_IOC_GETFLAGS:
1249 case FSFILT_IOC_SETFLAGS:
1250 RETURN(ll_iocontrol(inode, file, cmd, arg));
1251 case FSFILT_IOC_GETVERSION_OLD:
1252 case FSFILT_IOC_GETVERSION:
1253 RETURN(put_user(inode->i_generation, (int *)arg));
1254 /* We need to special case any other ioctls we want to handle,
1255 * to send them to the MDS/OST as appropriate and to properly
1256 * network encode the arg field.
1257 case FSFILT_IOC_SETVERSION_OLD:
1258 case FSFILT_IOC_SETVERSION:
1260 case LL_IOC_GET_MDTIDX: {
1263 mdtidx = ll_get_mdt_idx(inode);
1267 if (put_user((int)mdtidx, (int*)arg))
1272 case IOC_MDC_LOOKUP: {
1273 struct ptlrpc_request *request = NULL;
1274 int namelen, len = 0;
1277 struct md_op_data *op_data;
1279 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1284 filename = data->ioc_inlbuf1;
1285 namelen = strlen(filename);
1288 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1289 GOTO(out_free, rc = -EINVAL);
1292 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1293 0, LUSTRE_OPC_ANY, NULL);
1294 if (IS_ERR(op_data))
1295 GOTO(out_free, rc = PTR_ERR(op_data));
1297 op_data->op_valid = OBD_MD_FLID;
1298 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1299 ll_finish_md_op_data(op_data);
1301 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1304 ptlrpc_req_finished(request);
1307 obd_ioctl_freedata(buf, len);
1310 case LL_IOC_LMV_SETSTRIPE: {
1311 struct lmv_user_md *lum;
1319 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1324 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1325 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1326 GOTO(lmv_out_free, rc = -EINVAL);
1328 filename = data->ioc_inlbuf1;
1329 namelen = data->ioc_inllen1;
1332 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1333 GOTO(lmv_out_free, rc = -EINVAL);
1335 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1336 lumlen = data->ioc_inllen2;
1338 if (lum->lum_magic != LMV_USER_MAGIC ||
1339 lumlen != sizeof(*lum)) {
1340 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1341 filename, lum->lum_magic, lumlen, -EFAULT);
1342 GOTO(lmv_out_free, rc = -EINVAL);
1346 * ll_dir_setdirstripe will be used to set dir stripe
1347 * mdc_create--->mdt_reint_create (with dirstripe)
1349 rc = ll_dir_setdirstripe(inode, lum, filename);
1351 obd_ioctl_freedata(buf, len);
1355 case LL_IOC_LOV_SETSTRIPE: {
1356 struct lov_user_md_v3 lumv3;
1357 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1358 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1359 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1361 int set_default = 0;
1363 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1364 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1365 sizeof(lumv3p->lmm_objects[0]));
1366 /* first try with v1 which is smaller than v3 */
1367 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1370 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1371 if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1375 if (inode->i_sb->s_root == file->f_dentry)
1378 /* in v1 and v3 cases lumv1 points to data */
1379 rc = ll_dir_setstripe(inode, lumv1, set_default);
1383 case LL_IOC_LMV_GETSTRIPE: {
1384 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1385 struct lmv_user_md lum;
1386 struct lmv_user_md *tmp;
1391 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1394 if (lum.lum_magic != LMV_MAGIC_V1)
1397 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1398 OBD_ALLOC(tmp, lum_size);
1400 GOTO(free_lmv, rc = -ENOMEM);
1402 memcpy(tmp, &lum, sizeof(lum));
1403 tmp->lum_type = LMV_STRIPE_TYPE;
1404 tmp->lum_stripe_count = 1;
1405 mdtindex = ll_get_mdt_idx(inode);
1407 GOTO(free_lmv, rc = -ENOMEM);
1409 tmp->lum_stripe_offset = mdtindex;
1410 tmp->lum_objects[0].lum_mds = mdtindex;
1411 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1412 sizeof(struct lu_fid));
1413 if (copy_to_user((void *)arg, tmp, lum_size))
1414 GOTO(free_lmv, rc = -EFAULT);
1417 OBD_FREE(tmp, lum_size);
1420 case LL_IOC_REMOVE_ENTRY: {
1421 char *filename = NULL;
1425 /* Here is a little hack to avoid sending REINT_RMENTRY to
1426 * unsupported server, which might crash the server(LU-2730),
1427 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1428 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1429 * server will support REINT_RMENTRY XXX*/
1430 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1433 filename = getname((const char *)arg);
1434 if (IS_ERR(filename))
1435 RETURN(PTR_ERR(filename));
1437 namelen = strlen(filename);
1439 GOTO(out_rmdir, rc = -EINVAL);
1441 rc = ll_rmdir_entry(inode, filename, namelen);
1447 case LL_IOC_LOV_SWAP_LAYOUTS:
1449 case LL_IOC_OBD_STATFS:
1450 RETURN(ll_obd_statfs(inode, (void *)arg));
1451 case LL_IOC_LOV_GETSTRIPE:
1452 case LL_IOC_MDC_GETINFO:
1453 case IOC_MDC_GETFILEINFO:
1454 case IOC_MDC_GETFILESTRIPE: {
1455 struct ptlrpc_request *request = NULL;
1456 struct lov_user_md *lump;
1457 struct lov_mds_md *lmm = NULL;
1458 struct mdt_body *body;
1459 char *filename = NULL;
1462 if (cmd == IOC_MDC_GETFILEINFO ||
1463 cmd == IOC_MDC_GETFILESTRIPE) {
1464 filename = getname((const char *)arg);
1465 if (IS_ERR(filename))
1466 RETURN(PTR_ERR(filename));
1468 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1469 &lmmsize, &request);
1471 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1475 body = req_capsule_server_get(&request->rq_pill,
1477 LASSERT(body != NULL);
1483 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1484 cmd == LL_IOC_MDC_GETINFO))
1485 GOTO(skip_lmm, rc = 0);
1490 if (cmd == IOC_MDC_GETFILESTRIPE ||
1491 cmd == LL_IOC_LOV_GETSTRIPE) {
1492 lump = (struct lov_user_md *)arg;
1494 struct lov_user_mds_data *lmdp;
1495 lmdp = (struct lov_user_mds_data *)arg;
1496 lump = &lmdp->lmd_lmm;
1498 if (cfs_copy_to_user(lump, lmm, lmmsize)) {
1499 if (cfs_copy_to_user(lump, lmm, sizeof(*lump)))
1500 GOTO(out_req, rc = -EFAULT);
1504 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1505 struct lov_user_mds_data *lmdp;
1508 st.st_dev = inode->i_sb->s_dev;
1509 st.st_mode = body->mode;
1510 st.st_nlink = body->nlink;
1511 st.st_uid = body->uid;
1512 st.st_gid = body->gid;
1513 st.st_rdev = body->rdev;
1514 st.st_size = body->size;
1515 st.st_blksize = CFS_PAGE_SIZE;
1516 st.st_blocks = body->blocks;
1517 st.st_atime = body->atime;
1518 st.st_mtime = body->mtime;
1519 st.st_ctime = body->ctime;
1520 st.st_ino = inode->i_ino;
1522 lmdp = (struct lov_user_mds_data *)arg;
1523 if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1524 GOTO(out_req, rc = -EFAULT);
1529 ptlrpc_req_finished(request);
1534 case IOC_LOV_GETINFO: {
1535 struct lov_user_mds_data *lumd;
1536 struct lov_stripe_md *lsm;
1537 struct lov_user_md *lum;
1538 struct lov_mds_md *lmm;
1542 lumd = (struct lov_user_mds_data *)arg;
1543 lum = &lumd->lmd_lmm;
1545 rc = ll_get_max_mdsize(sbi, &lmmsize);
1549 OBD_ALLOC_LARGE(lmm, lmmsize);
1553 if (cfs_copy_from_user(lmm, lum, lmmsize))
1554 GOTO(free_lmm, rc = -EFAULT);
1556 switch (lmm->lmm_magic) {
1557 case LOV_USER_MAGIC_V1:
1558 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1560 /* swab objects first so that stripes num will be sane */
1561 lustre_swab_lov_user_md_objects(
1562 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1563 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1564 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1566 case LOV_USER_MAGIC_V3:
1567 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1569 /* swab objects first so that stripes num will be sane */
1570 lustre_swab_lov_user_md_objects(
1571 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1572 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1573 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1576 GOTO(free_lmm, rc = -EINVAL);
1579 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1581 GOTO(free_lmm, rc = -ENOMEM);
1583 /* Perform glimpse_size operation. */
1584 memset(&st, 0, sizeof(st));
1586 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1590 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1591 GOTO(free_lsm, rc = -EFAULT);
1595 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1597 OBD_FREE_LARGE(lmm, lmmsize);
1600 case OBD_IOC_LLOG_CATINFO: {
1601 RETURN(-EOPNOTSUPP);
1603 case OBD_IOC_QUOTACHECK: {
1604 struct obd_quotactl *oqctl;
1607 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1608 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1611 OBD_ALLOC_PTR(oqctl);
1614 oqctl->qc_type = arg;
1615 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1617 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1621 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1623 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1625 OBD_FREE_PTR(oqctl);
1628 case OBD_IOC_POLL_QUOTACHECK: {
1629 struct if_quotacheck *check;
1631 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1632 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1635 OBD_ALLOC_PTR(check);
1639 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1642 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1643 if (cfs_copy_to_user((void *)arg, check,
1645 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1649 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1652 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1653 if (cfs_copy_to_user((void *)arg, check,
1655 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1659 OBD_FREE_PTR(check);
1662 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1663 case LL_IOC_QUOTACTL_18: {
1664 /* copy the old 1.x quota struct for internal use, then copy
1665 * back into old format struct. For 1.8 compatibility. */
1666 struct if_quotactl_18 *qctl_18;
1667 struct if_quotactl *qctl_20;
1669 OBD_ALLOC_PTR(qctl_18);
1673 OBD_ALLOC_PTR(qctl_20);
1675 GOTO(out_quotactl_18, rc = -ENOMEM);
1677 if (cfs_copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1678 GOTO(out_quotactl_20, rc = -ENOMEM);
1680 QCTL_COPY(qctl_20, qctl_18);
1681 qctl_20->qc_idx = 0;
1683 /* XXX: dqb_valid was borrowed as a flag to mark that
1684 * only mds quota is wanted */
1685 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1686 qctl_18->qc_dqblk.dqb_valid) {
1687 qctl_20->qc_valid = QC_MDTIDX;
1688 qctl_20->qc_dqblk.dqb_valid = 0;
1689 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1690 qctl_20->qc_valid = QC_UUID;
1691 qctl_20->obd_uuid = qctl_18->obd_uuid;
1693 qctl_20->qc_valid = QC_GENERAL;
1696 rc = quotactl_ioctl(sbi, qctl_20);
1699 QCTL_COPY(qctl_18, qctl_20);
1700 qctl_18->obd_uuid = qctl_20->obd_uuid;
1702 if (cfs_copy_to_user((void *)arg, qctl_18,
1708 OBD_FREE_PTR(qctl_20);
1710 OBD_FREE_PTR(qctl_18);
1714 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1715 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1716 case LL_IOC_QUOTACTL: {
1717 struct if_quotactl *qctl;
1719 OBD_ALLOC_PTR(qctl);
1723 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1724 GOTO(out_quotactl, rc = -EFAULT);
1726 rc = quotactl_ioctl(sbi, qctl);
1728 if (rc == 0 && cfs_copy_to_user((void *)arg,qctl,sizeof(*qctl)))
1735 case OBD_IOC_GETDTNAME:
1736 case OBD_IOC_GETMDNAME:
1737 RETURN(ll_get_obd_name(inode, cmd, arg));
1738 case LL_IOC_FLUSHCTX:
1739 RETURN(ll_flush_ctx(inode));
1740 #ifdef CONFIG_FS_POSIX_ACL
1741 case LL_IOC_RMTACL: {
1742 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1743 inode == inode->i_sb->s_root->d_inode) {
1744 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1746 LASSERT(fd != NULL);
1747 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1749 fd->fd_flags |= LL_FILE_RMTACL;
1755 case LL_IOC_GETOBDCOUNT: {
1757 struct obd_export *exp;
1759 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1762 /* get ost count when count is zero, get mdt count otherwise */
1763 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1764 vallen = sizeof(count);
1765 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1766 KEY_TGT_COUNT, &vallen, &count, NULL);
1768 CERROR("get target count failed: %d\n", rc);
1772 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1777 case LL_IOC_PATH2FID:
1778 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1779 sizeof(struct lu_fid)))
1782 case LL_IOC_GET_CONNECT_FLAGS: {
1783 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1785 case OBD_IOC_CHANGELOG_SEND:
1786 case OBD_IOC_CHANGELOG_CLEAR:
1787 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1788 sizeof(struct ioc_changelog));
1790 case OBD_IOC_FID2PATH:
1791 RETURN(ll_fid2path(inode, (void *)arg));
1792 case LL_IOC_HSM_REQUEST: {
1793 struct hsm_user_request *hur;
1800 /* We don't know the true size yet; copy the fixed-size part */
1801 if (cfs_copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1806 /* Compute the whole struct size */
1807 totalsize = hur_len(hur);
1809 OBD_ALLOC_LARGE(hur, totalsize);
1813 /* Copy the whole struct */
1814 if (cfs_copy_from_user(hur, (void *)arg, totalsize)) {
1815 OBD_FREE_LARGE(hur, totalsize);
1819 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1822 OBD_FREE_LARGE(hur, totalsize);
1826 case LL_IOC_HSM_PROGRESS: {
1827 struct hsm_progress_kernel hpk;
1828 struct hsm_progress hp;
1830 if (cfs_copy_from_user(&hp, (void *)arg, sizeof(hp)))
1833 hpk.hpk_fid = hp.hp_fid;
1834 hpk.hpk_cookie = hp.hp_cookie;
1835 hpk.hpk_extent = hp.hp_extent;
1836 hpk.hpk_flags = hp.hp_flags;
1837 hpk.hpk_errval = hp.hp_errval;
1838 hpk.hpk_data_version = 0;
1840 /* File may not exist in Lustre; all progress
1841 * reported to Lustre root */
1842 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1846 case LL_IOC_HSM_CT_START:
1847 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1848 sizeof(struct lustre_kernelcomm));
1851 case LL_IOC_HSM_COPY_START: {
1852 struct hsm_copy *copy;
1855 OBD_ALLOC_PTR(copy);
1858 if (cfs_copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1863 rc = ll_ioc_copy_start(inode->i_sb, copy);
1864 if (cfs_copy_to_user((char *)arg, copy, sizeof(*copy)))
1870 case LL_IOC_HSM_COPY_END: {
1871 struct hsm_copy *copy;
1874 OBD_ALLOC_PTR(copy);
1877 if (cfs_copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1882 rc = ll_ioc_copy_end(inode->i_sb, copy);
1883 if (cfs_copy_to_user((char *)arg, copy, sizeof(*copy)))
1890 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1895 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1897 struct inode *inode = file->f_mapping->host;
1898 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1899 struct ll_sb_info *sbi = ll_i2sbi(inode);
1900 int api32 = ll_need_32bit_api(sbi);
1901 loff_t ret = -EINVAL;
1904 mutex_lock(&inode->i_mutex);
1909 offset += file->f_pos;
1915 offset += LL_DIR_END_OFF_32BIT;
1917 offset += LL_DIR_END_OFF;
1924 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1925 (!api32 && offset <= LL_DIR_END_OFF))) {
1926 if (offset != file->f_pos) {
1927 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1928 (!api32 && offset == LL_DIR_END_OFF))
1929 fd->lfd_pos = MDS_DIR_END_OFF;
1930 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1931 fd->lfd_pos = offset << 32;
1933 fd->lfd_pos = offset;
1934 file->f_pos = offset;
1935 file->f_version = 0;
1942 mutex_unlock(&inode->i_mutex);
1946 int ll_dir_open(struct inode *inode, struct file *file)
1949 RETURN(ll_file_open(inode, file));
1952 int ll_dir_release(struct inode *inode, struct file *file)
1955 RETURN(ll_file_release(inode, file));
1958 struct file_operations ll_dir_operations = {
1959 .llseek = ll_dir_seek,
1960 .open = ll_dir_open,
1961 .release = ll_dir_release,
1962 .read = generic_read_dir,
1963 .readdir = ll_readdir,
1964 .unlocked_ioctl = ll_dir_ioctl,