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 #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 >> PAGE_CACHE_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 +
200 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
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 = {
362 .ei_type = LDLM_IBITS,
364 .ei_cb_bl = ll_md_blocking_ast,
365 .ei_cb_cp = ldlm_completion_ast,
367 struct lookup_intent it = { .it_op = IT_READDIR };
368 struct ptlrpc_request *request;
369 struct md_op_data *op_data;
371 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
372 LUSTRE_OPC_ANY, NULL);
374 return (void *)op_data;
376 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
377 op_data, &lockh, NULL, 0, NULL, 0);
379 ll_finish_md_op_data(op_data);
381 request = (struct ptlrpc_request *)it.d.lustre.it_data;
383 ptlrpc_req_finished(request);
385 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
386 PFID(ll_inode2fid(dir)), hash, rc);
390 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
391 dir, dir->i_ino, dir->i_generation);
392 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
393 &it.d.lustre.it_lock_handle, dir, NULL);
395 /* for cross-ref object, l_ast_data of the lock may not be set,
396 * we reset it here */
397 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
400 ldlm_lock_dump_handle(D_OTHER, &lockh);
402 mutex_lock(&lli->lli_readdir_mutex);
403 page = ll_dir_page_locate(dir, &lhash, &start, &end);
405 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
406 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
407 GOTO(out_unlock, page);
408 } else if (page != NULL) {
410 * XXX nikita: not entirely correct handling of a corner case:
411 * suppose hash chain of entries with hash value HASH crosses
412 * border between pages P0 and P1. First both P0 and P1 are
413 * cached, seekdir() is called for some entry from the P0 part
414 * of the chain. Later P0 goes out of cache. telldir(HASH)
415 * happens and finds P1, as it starts with matching hash
416 * value. Remaining entries from P0 part of the chain are
417 * skipped. (Is that really a bug?)
419 * Possible solutions: 0. don't cache P1 is such case, handle
420 * it as an "overflow" page. 1. invalidate all pages at
421 * once. 2. use HASH|1 as an index for P1.
423 GOTO(hash_collision, page);
426 page = read_cache_page(mapping, hash_x_index(hash, hash64),
427 ll_dir_filler, &lhash);
429 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
430 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
431 GOTO(out_unlock, page);
436 if (!PageUptodate(page)) {
437 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
438 PFID(ll_inode2fid(dir)), hash, -5);
441 if (!PageChecked(page))
442 ll_check_page(dir, page);
443 if (PageError(page)) {
444 CERROR("page error: "DFID" at "LPU64": rc %d\n",
445 PFID(ll_inode2fid(dir)), hash, -5);
449 dp = page_address(page);
450 if (BITS_PER_LONG == 32 && hash64) {
451 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
452 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
455 start = le64_to_cpu(dp->ldp_hash_start);
456 end = le64_to_cpu(dp->ldp_hash_end);
460 LASSERT(start == lhash);
461 CWARN("Page-wide hash collision: "LPU64"\n", end);
462 if (BITS_PER_LONG == 32 && hash64)
463 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
464 "] with hash "LPU64"\n",
465 le64_to_cpu(dp->ldp_hash_start),
466 le64_to_cpu(dp->ldp_hash_end), hash);
468 * Fetch whole overflow chain...
475 mutex_unlock(&lli->lli_readdir_mutex);
476 ldlm_lock_decref(&lockh, mode);
480 ll_release_page(page, 1);
481 page = ERR_PTR(-EIO);
485 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
488 struct ll_inode_info *info = ll_i2info(inode);
489 struct ll_sb_info *sbi = ll_i2sbi(inode);
491 int api32 = ll_need_32bit_api(sbi);
492 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
494 struct ll_dir_chain chain;
499 ll_dir_chain_init(&chain);
501 page = ll_get_dir_page(inode, pos, &chain);
503 while (rc == 0 && !done) {
504 struct lu_dirpage *dp;
505 struct lu_dirent *ent;
509 * If page is empty (end of directory is reached),
512 __u64 hash = MDS_DIR_END_OFF;
515 dp = page_address(page);
516 for (ent = lu_dirent_start(dp); ent != NULL && !done;
517 ent = lu_dirent_next(ent)) {
525 * XXX: implement correct swabbing here.
528 hash = le64_to_cpu(ent->lde_hash);
531 * Skip until we find target hash
536 namelen = le16_to_cpu(ent->lde_namelen);
547 fid_le_to_cpu(&fid, &ent->lde_fid);
548 ino = cl_fid_build_ino(&fid, api32);
549 type = ll_dirent_type_get(ent);
550 /* For 'll_nfs_get_name_filldir()', it will try
551 * to access the 'ent' through its 'lde_name',
552 * so the parameter 'name' for 'filldir()' must
553 * be part of the 'ent'. */
554 done = filldir(cookie, ent->lde_name, namelen,
557 next = le64_to_cpu(dp->ldp_hash_end);
560 if (pos == MDS_DIR_END_OFF) {
562 * End of directory reached.
565 ll_release_page(page, 0);
566 } else if (1 /* chain is exhausted*/) {
568 * Normal case: continue to the next
571 ll_release_page(page,
572 le32_to_cpu(dp->ldp_flags) &
575 page = ll_get_dir_page(inode, pos,
579 * go into overflow page.
581 LASSERT(le32_to_cpu(dp->ldp_flags) &
583 ll_release_page(page, 1);
587 ll_release_page(page, 0);
591 CERROR("error reading dir "DFID" at %lu: rc %d\n",
592 PFID(&info->lli_fid), (unsigned long)pos, rc);
597 ll_dir_chain_fini(&chain);
601 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
603 struct inode *inode = filp->f_dentry->d_inode;
604 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
605 struct ll_sb_info *sbi = ll_i2sbi(inode);
606 __u64 pos = lfd->lfd_pos;
607 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
608 int api32 = ll_need_32bit_api(sbi);
610 #ifdef HAVE_TOUCH_ATIME_1ARG
615 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
616 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
617 inode, (unsigned long)pos, i_size_read(inode), api32);
619 if (pos == MDS_DIR_END_OFF)
625 rc = ll_dir_read(inode, &pos, cookie, filldir);
627 if (pos == MDS_DIR_END_OFF) {
629 filp->f_pos = LL_DIR_END_OFF_32BIT;
631 filp->f_pos = LL_DIR_END_OFF;
634 filp->f_pos = pos >> 32;
638 filp->f_version = inode->i_version;
639 #ifdef HAVE_TOUCH_ATIME_1ARG
640 path.mnt = filp->f_vfsmnt;
641 path.dentry = filp->f_dentry;
644 touch_atime(filp->f_vfsmnt, filp->f_dentry);
649 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
654 int ll_send_mgc_param(struct obd_export *mgc, char *string)
656 struct mgs_send_param *msp;
663 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
664 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
665 sizeof(struct mgs_send_param), msp, NULL);
667 CERROR("Failed to set parameter: %d\n", rc);
673 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
676 struct ptlrpc_request *request = NULL;
677 struct md_op_data *op_data;
678 struct ll_sb_info *sbi = ll_i2sbi(dir);
684 mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
685 op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
686 strlen(filename), mode, LUSTRE_OPC_MKDIR,
689 GOTO(err_exit, err = PTR_ERR(op_data));
691 op_data->op_cli_flags |= CLI_SET_MEA;
692 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
693 cfs_curproc_fsuid(), cfs_curproc_fsgid(),
694 cfs_curproc_cap_pack(), 0, &request);
695 ll_finish_md_op_data(op_data);
699 ptlrpc_req_finished(request);
703 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
706 struct ll_sb_info *sbi = ll_i2sbi(inode);
707 struct md_op_data *op_data;
708 struct ptlrpc_request *req = NULL;
710 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
711 struct obd_device *mgc = lsi->lsi_mgc;
717 * This is coming from userspace, so should be in
718 * local endian. But the MDS would like it in little
719 * endian, so we swab it before we send it.
721 switch (lump->lmm_magic) {
722 case LOV_USER_MAGIC_V1: {
723 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
724 lustre_swab_lov_user_md_v1(lump);
725 lum_size = sizeof(struct lov_user_md_v1);
728 case LOV_USER_MAGIC_V3: {
729 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
730 lustre_swab_lov_user_md_v3(
731 (struct lov_user_md_v3 *)lump);
732 lum_size = sizeof(struct lov_user_md_v3);
736 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
737 " %#08x != %#08x nor %#08x\n",
738 lump->lmm_magic, LOV_USER_MAGIC_V1,
744 lum_size = sizeof(struct lov_user_md_v1);
747 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
748 LUSTRE_OPC_ANY, NULL);
750 RETURN(PTR_ERR(op_data));
752 if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
753 op_data->op_cli_flags |= CLI_SET_MEA;
755 /* swabbing is done in lov_setstripe() on server side */
756 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
757 NULL, 0, &req, NULL);
758 ll_finish_md_op_data(op_data);
759 ptlrpc_req_finished(req);
761 if (rc != -EPERM && rc != -EACCES)
762 CERROR("mdc_setattr fails: rc = %d\n", rc);
765 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
766 LOV_USER_MAGIC_V3 have the same initial fields so we do not
767 need the make the distiction between the 2 versions */
768 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
772 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
774 GOTO(end, rc = -ENOMEM);
777 /* Get fsname and assume devname to be -MDT0000. */
778 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
779 strcat(buf, "-MDT0000.lov");
782 /* Set root stripesize */
783 sprintf(buf, ".stripesize=%u",
784 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
785 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
789 /* Set root stripecount */
790 sprintf(buf, ".stripecount=%hd",
791 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
792 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
796 /* Set root stripeoffset */
797 sprintf(buf, ".stripeoffset=%hd",
798 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
799 (typeof(lump->lmm_stripe_offset))(-1));
800 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
804 OBD_FREE(param, MGS_PARAM_MAXLEN);
809 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
810 int *lmm_size, struct ptlrpc_request **request)
812 struct ll_sb_info *sbi = ll_i2sbi(inode);
813 struct mdt_body *body;
814 struct lov_mds_md *lmm = NULL;
815 struct ptlrpc_request *req = NULL;
817 struct md_op_data *op_data;
819 rc = ll_get_max_mdsize(sbi, &lmmsize);
823 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
824 0, lmmsize, LUSTRE_OPC_ANY,
827 RETURN(PTR_ERR(op_data));
829 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
830 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
831 ll_finish_md_op_data(op_data);
833 CDEBUG(D_INFO, "md_getattr failed on inode "
834 "%lu/%u: rc %d\n", inode->i_ino,
835 inode->i_generation, rc);
839 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
840 LASSERT(body != NULL);
842 lmmsize = body->eadatasize;
844 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
846 GOTO(out, rc = -ENODATA);
849 lmm = req_capsule_server_sized_get(&req->rq_pill,
850 &RMF_MDT_MD, lmmsize);
851 LASSERT(lmm != NULL);
854 * This is coming from the MDS, so is probably in
855 * little endian. We convert it to host endian before
856 * passing it to userspace.
858 /* We don't swab objects for directories */
859 switch (le32_to_cpu(lmm->lmm_magic)) {
861 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
862 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
865 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
866 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
869 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
880 * Get MDT index for the inode.
882 int ll_get_mdt_idx(struct inode *inode)
884 struct ll_sb_info *sbi = ll_i2sbi(inode);
885 struct md_op_data *op_data;
889 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
890 0, LUSTRE_OPC_ANY, NULL);
892 RETURN(PTR_ERR(op_data));
894 op_data->op_flags |= MF_GET_MDT_IDX;
895 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
896 mdtidx = op_data->op_mds;
897 ll_finish_md_op_data(op_data);
899 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
906 * Generic handler to do any pre-copy work.
908 * It send a first hsm_progress (with extent length == 0) to coordinator as a
909 * first information for it that real work has started.
911 * Moreover, for a ARCHIVE request, it will sample the file data version and
912 * store it in \a copy.
914 * \return 0 on success.
916 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
918 struct ll_sb_info *sbi = ll_s2sbi(sb);
919 struct hsm_progress_kernel hpk;
923 /* Forge a hsm_progress based on data from copy. */
924 hpk.hpk_fid = copy->hc_hai.hai_fid;
925 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
926 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
927 hpk.hpk_extent.length = 0;
930 hpk.hpk_data_version = 0;
933 /* For archive request, we need to read the current file version. */
934 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
936 __u64 data_version = 0;
938 /* Get inode for this fid */
939 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
941 hpk.hpk_flags |= HP_FLAG_RETRY;
942 /* hpk_errval is >= 0 */
943 hpk.hpk_errval = -PTR_ERR(inode);
944 GOTO(progress, rc = PTR_ERR(inode));
947 /* Read current file data version */
948 rc = ll_data_version(inode, &data_version, 1);
951 CDEBUG(D_HSM, "Could not read file data version of "
952 DFID" (rc = %d). Archive request ("
953 LPX64") could not be done.\n",
954 PFID(©->hc_hai.hai_fid), rc,
955 copy->hc_hai.hai_cookie);
956 hpk.hpk_flags |= HP_FLAG_RETRY;
957 /* hpk_errval must be >= 0 */
958 hpk.hpk_errval = -rc;
962 /* Store it the hsm_copy for later copytool use.
963 * Always modified even if no lsm. */
964 copy->hc_data_version = data_version;
968 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
975 * Generic handler to do any post-copy work.
977 * It will send the last hsm_progress update to coordinator to inform it
978 * that copy is finished and whether it was successful or not.
981 * - for ARCHIVE request, it will sample the file data version and compare it
982 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
983 * will be considered as failed.
984 * - for RESTORE request, it will sample the file data version and send it to
985 * coordinator which is useful if the file was imported as 'released'.
987 * \return 0 on success.
989 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
991 struct ll_sb_info *sbi = ll_s2sbi(sb);
992 struct hsm_progress_kernel hpk;
996 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
997 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
998 * initialized if copy_end was called with copy == NULL.
1001 /* Forge a hsm_progress based on data from copy. */
1002 hpk.hpk_fid = copy->hc_hai.hai_fid;
1003 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1004 hpk.hpk_extent = copy->hc_hai.hai_extent;
1005 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1006 hpk.hpk_errval = copy->hc_errval;
1007 hpk.hpk_data_version = 0;
1009 /* For archive request, we need to check the file data was not changed.
1011 * For restore request, we need to send the file data version, this is
1012 * useful when the file was created using hsm_import.
1014 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1015 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1016 (copy->hc_errval == 0)) {
1017 struct inode *inode;
1018 __u64 data_version = 0;
1020 /* Get lsm for this fid */
1021 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1022 if (IS_ERR(inode)) {
1023 hpk.hpk_flags |= HP_FLAG_RETRY;
1024 /* hpk_errval must be >= 0 */
1025 hpk.hpk_errval = -PTR_ERR(inode);
1026 GOTO(progress, rc = PTR_ERR(inode));
1029 rc = ll_data_version(inode, &data_version,
1030 copy->hc_hai.hai_action == HSMA_ARCHIVE);
1033 CDEBUG(D_HSM, "Could not read file data version. "
1034 "Request could not be confirmed.\n");
1035 if (hpk.hpk_errval == 0)
1036 hpk.hpk_errval = -rc;
1040 /* Store it the hsm_copy for later copytool use.
1041 * Always modified even if no lsm. */
1042 hpk.hpk_data_version = data_version;
1044 /* File could have been stripped during archiving, so we need
1045 * to check anyway. */
1046 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1047 (copy->hc_data_version != data_version)) {
1048 CDEBUG(D_HSM, "File data version mismatched. "
1049 "File content was changed during archiving. "
1050 DFID", start:"LPX64" current:"LPX64"\n",
1051 PFID(©->hc_hai.hai_fid),
1052 copy->hc_data_version, data_version);
1053 /* File was changed, send error to cdt. Do not ask for
1054 * retry because if a file is modified frequently,
1055 * the cdt will loop on retried archive requests.
1056 * The policy engine will ask for a new archive later
1057 * when the file will not be modified for some tunable
1059 /* we do not notify caller */
1060 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1061 /* hpk_errval must be >= 0 */
1062 hpk.hpk_errval = EBUSY;
1068 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1075 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
1080 OBD_ALLOC(ptr, len);
1083 if (copy_from_user(ptr, data, len)) {
1087 rc = obd_iocontrol(cmd, exp, len, data, NULL);
1092 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1094 int cmd = qctl->qc_cmd;
1095 int type = qctl->qc_type;
1096 int id = qctl->qc_id;
1097 int valid = qctl->qc_valid;
1102 case LUSTRE_Q_INVALIDATE:
1103 case LUSTRE_Q_FINVALIDATE:
1108 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1109 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1113 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1114 (type == GRPQUOTA && !in_egroup_p(id))) &&
1115 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1116 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1122 CERROR("unsupported quotactl op: %#x\n", cmd);
1126 if (valid != QC_GENERAL) {
1127 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1128 RETURN(-EOPNOTSUPP);
1130 if (cmd == Q_GETINFO)
1131 qctl->qc_cmd = Q_GETOINFO;
1132 else if (cmd == Q_GETQUOTA)
1133 qctl->qc_cmd = Q_GETOQUOTA;
1139 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1140 sizeof(*qctl), qctl, NULL);
1143 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1144 sizeof(*qctl), qctl, NULL);
1147 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1148 sizeof(*qctl), qctl, NULL);
1150 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1152 sizeof(*qctl), qctl, NULL);
1164 struct obd_quotactl *oqctl;
1166 OBD_ALLOC_PTR(oqctl);
1170 QCTL_COPY(oqctl, qctl);
1171 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1173 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1174 oqctl->qc_cmd = Q_QUOTAOFF;
1175 obd_quotactl(sbi->ll_md_exp, oqctl);
1177 OBD_FREE_PTR(oqctl);
1180 /* If QIF_SPACE is not set, client should collect the
1181 * space usage from OSSs by itself */
1182 if (cmd == Q_GETQUOTA &&
1183 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1184 !oqctl->qc_dqblk.dqb_curspace) {
1185 struct obd_quotactl *oqctl_tmp;
1187 OBD_ALLOC_PTR(oqctl_tmp);
1188 if (oqctl_tmp == NULL)
1189 GOTO(out, rc = -ENOMEM);
1191 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1192 oqctl_tmp->qc_id = oqctl->qc_id;
1193 oqctl_tmp->qc_type = oqctl->qc_type;
1195 /* collect space usage from OSTs */
1196 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1197 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1198 if (!rc || rc == -EREMOTEIO) {
1199 oqctl->qc_dqblk.dqb_curspace =
1200 oqctl_tmp->qc_dqblk.dqb_curspace;
1201 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1204 /* collect space & inode usage from MDTs */
1205 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1206 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1207 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1208 if (!rc || rc == -EREMOTEIO) {
1209 oqctl->qc_dqblk.dqb_curspace +=
1210 oqctl_tmp->qc_dqblk.dqb_curspace;
1211 oqctl->qc_dqblk.dqb_curinodes =
1212 oqctl_tmp->qc_dqblk.dqb_curinodes;
1213 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1215 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1218 OBD_FREE_PTR(oqctl_tmp);
1221 QCTL_COPY(qctl, oqctl);
1222 OBD_FREE_PTR(oqctl);
1229 ll_getname(const char __user *filename)
1232 char *tmp = __getname();
1235 return ERR_PTR(-ENOMEM);
1237 len = strncpy_from_user(tmp, filename, PATH_MAX);
1240 else if (len > PATH_MAX)
1241 ret = -ENAMETOOLONG;
1250 #define ll_putname(filename) __putname(filename)
1252 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1254 struct inode *inode = file->f_dentry->d_inode;
1255 struct ll_sb_info *sbi = ll_i2sbi(inode);
1256 struct obd_ioctl_data *data;
1260 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1261 inode->i_ino, inode->i_generation, inode, cmd);
1263 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1264 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1267 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1269 case FSFILT_IOC_GETFLAGS:
1270 case FSFILT_IOC_SETFLAGS:
1271 RETURN(ll_iocontrol(inode, file, cmd, arg));
1272 case FSFILT_IOC_GETVERSION_OLD:
1273 case FSFILT_IOC_GETVERSION:
1274 RETURN(put_user(inode->i_generation, (int *)arg));
1275 /* We need to special case any other ioctls we want to handle,
1276 * to send them to the MDS/OST as appropriate and to properly
1277 * network encode the arg field.
1278 case FSFILT_IOC_SETVERSION_OLD:
1279 case FSFILT_IOC_SETVERSION:
1281 case LL_IOC_GET_MDTIDX: {
1284 mdtidx = ll_get_mdt_idx(inode);
1288 if (put_user((int)mdtidx, (int*)arg))
1293 case IOC_MDC_LOOKUP: {
1294 struct ptlrpc_request *request = NULL;
1295 int namelen, len = 0;
1298 struct md_op_data *op_data;
1300 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1305 filename = data->ioc_inlbuf1;
1306 namelen = strlen(filename);
1309 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1310 GOTO(out_free, rc = -EINVAL);
1313 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1314 0, LUSTRE_OPC_ANY, NULL);
1315 if (IS_ERR(op_data))
1316 GOTO(out_free, rc = PTR_ERR(op_data));
1318 op_data->op_valid = OBD_MD_FLID;
1319 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1320 ll_finish_md_op_data(op_data);
1322 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1325 ptlrpc_req_finished(request);
1328 obd_ioctl_freedata(buf, len);
1331 case LL_IOC_LMV_SETSTRIPE: {
1332 struct lmv_user_md *lum;
1340 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1345 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1346 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1347 GOTO(lmv_out_free, rc = -EINVAL);
1349 filename = data->ioc_inlbuf1;
1350 namelen = data->ioc_inllen1;
1353 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1354 GOTO(lmv_out_free, rc = -EINVAL);
1356 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1357 lumlen = data->ioc_inllen2;
1359 if (lum->lum_magic != LMV_USER_MAGIC ||
1360 lumlen != sizeof(*lum)) {
1361 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1362 filename, lum->lum_magic, lumlen, -EFAULT);
1363 GOTO(lmv_out_free, rc = -EINVAL);
1367 * ll_dir_setdirstripe will be used to set dir stripe
1368 * mdc_create--->mdt_reint_create (with dirstripe)
1370 rc = ll_dir_setdirstripe(inode, lum, filename);
1372 obd_ioctl_freedata(buf, len);
1376 case LL_IOC_LOV_SETSTRIPE: {
1377 struct lov_user_md_v3 lumv3;
1378 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1379 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1380 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1382 int set_default = 0;
1384 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1385 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1386 sizeof(lumv3p->lmm_objects[0]));
1387 /* first try with v1 which is smaller than v3 */
1388 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1391 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1392 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1396 if (inode->i_sb->s_root == file->f_dentry)
1399 /* in v1 and v3 cases lumv1 points to data */
1400 rc = ll_dir_setstripe(inode, lumv1, set_default);
1404 case LL_IOC_LMV_GETSTRIPE: {
1405 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1406 struct lmv_user_md lum;
1407 struct lmv_user_md *tmp;
1412 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1415 if (lum.lum_magic != LMV_MAGIC_V1)
1418 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1419 OBD_ALLOC(tmp, lum_size);
1421 GOTO(free_lmv, rc = -ENOMEM);
1423 memcpy(tmp, &lum, sizeof(lum));
1424 tmp->lum_type = LMV_STRIPE_TYPE;
1425 tmp->lum_stripe_count = 1;
1426 mdtindex = ll_get_mdt_idx(inode);
1428 GOTO(free_lmv, rc = -ENOMEM);
1430 tmp->lum_stripe_offset = mdtindex;
1431 tmp->lum_objects[0].lum_mds = mdtindex;
1432 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1433 sizeof(struct lu_fid));
1434 if (copy_to_user((void *)arg, tmp, lum_size))
1435 GOTO(free_lmv, rc = -EFAULT);
1438 OBD_FREE(tmp, lum_size);
1441 case LL_IOC_REMOVE_ENTRY: {
1442 char *filename = NULL;
1446 /* Here is a little hack to avoid sending REINT_RMENTRY to
1447 * unsupported server, which might crash the server(LU-2730),
1448 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1449 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1450 * server will support REINT_RMENTRY XXX*/
1451 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1454 filename = ll_getname((const char *)arg);
1455 if (IS_ERR(filename))
1456 RETURN(PTR_ERR(filename));
1458 namelen = strlen(filename);
1460 GOTO(out_rmdir, rc = -EINVAL);
1462 rc = ll_rmdir_entry(inode, filename, namelen);
1465 ll_putname(filename);
1468 case LL_IOC_LOV_SWAP_LAYOUTS:
1470 case LL_IOC_OBD_STATFS:
1471 RETURN(ll_obd_statfs(inode, (void *)arg));
1472 case LL_IOC_LOV_GETSTRIPE:
1473 case LL_IOC_MDC_GETINFO:
1474 case IOC_MDC_GETFILEINFO:
1475 case IOC_MDC_GETFILESTRIPE: {
1476 struct ptlrpc_request *request = NULL;
1477 struct lov_user_md *lump;
1478 struct lov_mds_md *lmm = NULL;
1479 struct mdt_body *body;
1480 char *filename = NULL;
1483 if (cmd == IOC_MDC_GETFILEINFO ||
1484 cmd == IOC_MDC_GETFILESTRIPE) {
1485 filename = ll_getname((const char *)arg);
1486 if (IS_ERR(filename))
1487 RETURN(PTR_ERR(filename));
1489 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1490 &lmmsize, &request);
1492 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1496 body = req_capsule_server_get(&request->rq_pill,
1498 LASSERT(body != NULL);
1504 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1505 cmd == LL_IOC_MDC_GETINFO))
1506 GOTO(skip_lmm, rc = 0);
1511 if (cmd == IOC_MDC_GETFILESTRIPE ||
1512 cmd == LL_IOC_LOV_GETSTRIPE) {
1513 lump = (struct lov_user_md *)arg;
1515 struct lov_user_mds_data *lmdp;
1516 lmdp = (struct lov_user_mds_data *)arg;
1517 lump = &lmdp->lmd_lmm;
1519 if (copy_to_user(lump, lmm, lmmsize)) {
1520 if (copy_to_user(lump, lmm, sizeof(*lump)))
1521 GOTO(out_req, rc = -EFAULT);
1525 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1526 struct lov_user_mds_data *lmdp;
1529 st.st_dev = inode->i_sb->s_dev;
1530 st.st_mode = body->mode;
1531 st.st_nlink = body->nlink;
1532 st.st_uid = body->uid;
1533 st.st_gid = body->gid;
1534 st.st_rdev = body->rdev;
1535 st.st_size = body->size;
1536 st.st_blksize = PAGE_CACHE_SIZE;
1537 st.st_blocks = body->blocks;
1538 st.st_atime = body->atime;
1539 st.st_mtime = body->mtime;
1540 st.st_ctime = body->ctime;
1541 st.st_ino = inode->i_ino;
1543 lmdp = (struct lov_user_mds_data *)arg;
1544 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1545 GOTO(out_req, rc = -EFAULT);
1550 ptlrpc_req_finished(request);
1552 ll_putname(filename);
1555 case IOC_LOV_GETINFO: {
1556 struct lov_user_mds_data *lumd;
1557 struct lov_stripe_md *lsm;
1558 struct lov_user_md *lum;
1559 struct lov_mds_md *lmm;
1563 lumd = (struct lov_user_mds_data *)arg;
1564 lum = &lumd->lmd_lmm;
1566 rc = ll_get_max_mdsize(sbi, &lmmsize);
1570 OBD_ALLOC_LARGE(lmm, lmmsize);
1574 if (copy_from_user(lmm, lum, lmmsize))
1575 GOTO(free_lmm, rc = -EFAULT);
1577 switch (lmm->lmm_magic) {
1578 case LOV_USER_MAGIC_V1:
1579 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1581 /* swab objects first so that stripes num will be sane */
1582 lustre_swab_lov_user_md_objects(
1583 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1584 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1585 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1587 case LOV_USER_MAGIC_V3:
1588 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1590 /* swab objects first so that stripes num will be sane */
1591 lustre_swab_lov_user_md_objects(
1592 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1593 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1594 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1597 GOTO(free_lmm, rc = -EINVAL);
1600 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1602 GOTO(free_lmm, rc = -ENOMEM);
1604 /* Perform glimpse_size operation. */
1605 memset(&st, 0, sizeof(st));
1607 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1611 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1612 GOTO(free_lsm, rc = -EFAULT);
1616 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1618 OBD_FREE_LARGE(lmm, lmmsize);
1621 case OBD_IOC_LLOG_CATINFO: {
1622 RETURN(-EOPNOTSUPP);
1624 case OBD_IOC_QUOTACHECK: {
1625 struct obd_quotactl *oqctl;
1628 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1629 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1632 OBD_ALLOC_PTR(oqctl);
1635 oqctl->qc_type = arg;
1636 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1638 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1642 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1644 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1646 OBD_FREE_PTR(oqctl);
1649 case OBD_IOC_POLL_QUOTACHECK: {
1650 struct if_quotacheck *check;
1652 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1653 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1656 OBD_ALLOC_PTR(check);
1660 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1663 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1664 if (copy_to_user((void *)arg, check,
1666 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1670 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1673 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1674 if (copy_to_user((void *)arg, check,
1676 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1680 OBD_FREE_PTR(check);
1683 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1684 case LL_IOC_QUOTACTL_18: {
1685 /* copy the old 1.x quota struct for internal use, then copy
1686 * back into old format struct. For 1.8 compatibility. */
1687 struct if_quotactl_18 *qctl_18;
1688 struct if_quotactl *qctl_20;
1690 OBD_ALLOC_PTR(qctl_18);
1694 OBD_ALLOC_PTR(qctl_20);
1696 GOTO(out_quotactl_18, rc = -ENOMEM);
1698 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1699 GOTO(out_quotactl_20, rc = -ENOMEM);
1701 QCTL_COPY(qctl_20, qctl_18);
1702 qctl_20->qc_idx = 0;
1704 /* XXX: dqb_valid was borrowed as a flag to mark that
1705 * only mds quota is wanted */
1706 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1707 qctl_18->qc_dqblk.dqb_valid) {
1708 qctl_20->qc_valid = QC_MDTIDX;
1709 qctl_20->qc_dqblk.dqb_valid = 0;
1710 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1711 qctl_20->qc_valid = QC_UUID;
1712 qctl_20->obd_uuid = qctl_18->obd_uuid;
1714 qctl_20->qc_valid = QC_GENERAL;
1717 rc = quotactl_ioctl(sbi, qctl_20);
1720 QCTL_COPY(qctl_18, qctl_20);
1721 qctl_18->obd_uuid = qctl_20->obd_uuid;
1723 if (copy_to_user((void *)arg, qctl_18,
1729 OBD_FREE_PTR(qctl_20);
1731 OBD_FREE_PTR(qctl_18);
1735 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1736 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1737 case LL_IOC_QUOTACTL: {
1738 struct if_quotactl *qctl;
1740 OBD_ALLOC_PTR(qctl);
1744 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1745 GOTO(out_quotactl, rc = -EFAULT);
1747 rc = quotactl_ioctl(sbi, qctl);
1749 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1756 case OBD_IOC_GETDTNAME:
1757 case OBD_IOC_GETMDNAME:
1758 RETURN(ll_get_obd_name(inode, cmd, arg));
1759 case LL_IOC_FLUSHCTX:
1760 RETURN(ll_flush_ctx(inode));
1761 #ifdef CONFIG_FS_POSIX_ACL
1762 case LL_IOC_RMTACL: {
1763 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1764 inode == inode->i_sb->s_root->d_inode) {
1765 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1767 LASSERT(fd != NULL);
1768 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1770 fd->fd_flags |= LL_FILE_RMTACL;
1776 case LL_IOC_GETOBDCOUNT: {
1778 struct obd_export *exp;
1780 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1783 /* get ost count when count is zero, get mdt count otherwise */
1784 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1785 vallen = sizeof(count);
1786 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1787 KEY_TGT_COUNT, &vallen, &count, NULL);
1789 CERROR("get target count failed: %d\n", rc);
1793 if (copy_to_user((int *)arg, &count, sizeof(int)))
1798 case LL_IOC_PATH2FID:
1799 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1800 sizeof(struct lu_fid)))
1803 case LL_IOC_GET_CONNECT_FLAGS: {
1804 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1806 case OBD_IOC_CHANGELOG_SEND:
1807 case OBD_IOC_CHANGELOG_CLEAR:
1808 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1809 sizeof(struct ioc_changelog));
1811 case OBD_IOC_FID2PATH:
1812 RETURN(ll_fid2path(inode, (void *)arg));
1813 case LL_IOC_HSM_REQUEST: {
1814 struct hsm_user_request *hur;
1821 /* We don't know the true size yet; copy the fixed-size part */
1822 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1827 /* Compute the whole struct size */
1828 totalsize = hur_len(hur);
1830 OBD_ALLOC_LARGE(hur, totalsize);
1834 /* Copy the whole struct */
1835 if (copy_from_user(hur, (void *)arg, totalsize)) {
1836 OBD_FREE_LARGE(hur, totalsize);
1840 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1843 OBD_FREE_LARGE(hur, totalsize);
1847 case LL_IOC_HSM_PROGRESS: {
1848 struct hsm_progress_kernel hpk;
1849 struct hsm_progress hp;
1851 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1854 hpk.hpk_fid = hp.hp_fid;
1855 hpk.hpk_cookie = hp.hp_cookie;
1856 hpk.hpk_extent = hp.hp_extent;
1857 hpk.hpk_flags = hp.hp_flags;
1858 hpk.hpk_errval = hp.hp_errval;
1859 hpk.hpk_data_version = 0;
1861 /* File may not exist in Lustre; all progress
1862 * reported to Lustre root */
1863 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1867 case LL_IOC_HSM_CT_START:
1868 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1869 sizeof(struct lustre_kernelcomm));
1872 case LL_IOC_HSM_COPY_START: {
1873 struct hsm_copy *copy;
1876 OBD_ALLOC_PTR(copy);
1879 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1884 rc = ll_ioc_copy_start(inode->i_sb, copy);
1885 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1891 case LL_IOC_HSM_COPY_END: {
1892 struct hsm_copy *copy;
1895 OBD_ALLOC_PTR(copy);
1898 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1903 rc = ll_ioc_copy_end(inode->i_sb, copy);
1904 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1911 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1916 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1918 struct inode *inode = file->f_mapping->host;
1919 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1920 struct ll_sb_info *sbi = ll_i2sbi(inode);
1921 int api32 = ll_need_32bit_api(sbi);
1922 loff_t ret = -EINVAL;
1925 mutex_lock(&inode->i_mutex);
1930 offset += file->f_pos;
1936 offset += LL_DIR_END_OFF_32BIT;
1938 offset += LL_DIR_END_OFF;
1945 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1946 (!api32 && offset <= LL_DIR_END_OFF))) {
1947 if (offset != file->f_pos) {
1948 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1949 (!api32 && offset == LL_DIR_END_OFF))
1950 fd->lfd_pos = MDS_DIR_END_OFF;
1951 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1952 fd->lfd_pos = offset << 32;
1954 fd->lfd_pos = offset;
1955 file->f_pos = offset;
1956 file->f_version = 0;
1963 mutex_unlock(&inode->i_mutex);
1967 int ll_dir_open(struct inode *inode, struct file *file)
1970 RETURN(ll_file_open(inode, file));
1973 int ll_dir_release(struct inode *inode, struct file *file)
1976 RETURN(ll_file_release(inode, file));
1979 struct file_operations ll_dir_operations = {
1980 .llseek = ll_dir_seek,
1981 .open = ll_dir_open,
1982 .release = ll_dir_release,
1983 .read = generic_read_dir,
1984 .readdir = ll_readdir,
1985 .unlocked_ioctl = ll_dir_ioctl,