4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Directory code for lustre client.
42 #include <linux/pagemap.h>
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/buffer_head.h> // for wait_on_buffer
47 #include <linux/pagevec.h>
49 #define DEBUG_SUBSYSTEM S_LLITE
51 #include <lustre/lustre_idl.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_lib.h>
55 #include <lustre/lustre_idl.h>
56 #include <lustre_lite.h>
57 #include <lustre_dlm.h>
58 #include <lustre_fid.h>
59 #include "llite_internal.h"
62 * (new) readdir implementation overview.
64 * Original lustre readdir implementation cached exact copy of raw directory
65 * pages on the client. These pages were indexed in client page cache by
66 * logical offset in the directory file. This design, while very simple and
67 * intuitive had some inherent problems:
69 * . it implies that byte offset to the directory entry serves as a
70 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71 * ext3/htree directory entries may move due to splits, and more
74 * . it is incompatible with the design of split directories for cmd3,
75 * that assumes that names are distributed across nodes based on their
76 * hash, and so readdir should be done in hash order.
78 * New readdir implementation does readdir in hash order, and uses hash of a
79 * file name as a telldir/seekdir cookie. This led to number of complications:
81 * . hash is not unique, so it cannot be used to index cached directory
82 * pages on the client (note, that it requires a whole pageful of hash
83 * collided entries to cause two pages to have identical hashes);
85 * . hash is not unique, so it cannot, strictly speaking, be used as an
86 * entry cookie. ext3/htree has the same problem and lustre implementation
87 * mimics their solution: seekdir(hash) positions directory at the first
88 * entry with the given hash.
94 * Client caches directory pages using hash of the first entry as an index. As
95 * noted above hash is not unique, so this solution doesn't work as is:
96 * special processing is needed for "page hash chains" (i.e., sequences of
97 * pages filled with entries all having the same hash value).
99 * First, such chains have to be detected. To this end, server returns to the
100 * client the hash of the first entry on the page next to one returned. When
101 * client detects that this hash is the same as hash of the first entry on the
102 * returned page, page hash collision has to be handled. Pages in the
103 * hash chain, except first one, are termed "overflow pages".
105 * Solution to index uniqueness problem is to not cache overflow
106 * pages. Instead, when page hash collision is detected, all overflow pages
107 * from emerging chain are immediately requested from the server and placed in
108 * a special data structure (struct ll_dir_chain). This data structure is used
109 * by ll_readdir() to process entries from overflow pages. When readdir
110 * invocation finishes, overflow pages are discarded. If page hash collision
111 * chain weren't completely processed, next call to readdir will again detect
112 * page hash collision, again read overflow pages in, process next portion of
113 * entries and again discard the pages. This is not as wasteful as it looks,
114 * because, given reasonable hash, page hash collisions are extremely rare.
116 * 1. directory positioning
118 * When seekdir(hash) is called, original
129 * identification of and access to overflow pages
133 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
134 * a header lu_dirpage which describes the start/end hash, and whether this
135 * page is empty (contains no dir entry) or hash collide with next page.
136 * After client receives reply, several pages will be integrated into dir page
137 * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
138 * lu_dirpage for this integrated page will be adjusted. See
139 * lmv_adjust_dirpages().
143 /* returns the page unlocked, but with a reference */
144 static int ll_dir_filler(void *_hash, struct page *page0)
146 struct inode *inode = page0->mapping->host;
147 int hash64 = ll_i2sbi(inode)->ll_flags & LL_SBI_64BIT_HASH;
148 struct obd_export *exp = ll_i2sbi(inode)->ll_md_exp;
149 struct ptlrpc_request *request;
150 struct mdt_body *body;
151 struct md_op_data *op_data;
152 __u64 hash = *((__u64 *)_hash);
153 struct page **page_pool;
155 struct lu_dirpage *dp;
156 int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> PAGE_CACHE_SHIFT;
157 int nrdpgs = 0; /* number of pages read actually */
163 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
164 inode->i_ino, inode->i_generation, inode, hash);
166 LASSERT(max_pages > 0 && max_pages <= MD_MAX_BRW_PAGES);
168 OBD_ALLOC(page_pool, sizeof(page) * max_pages);
169 if (page_pool != NULL) {
170 page_pool[0] = page0;
175 for (npages = 1; npages < max_pages; npages++) {
176 page = page_cache_alloc_cold(inode->i_mapping);
179 page_pool[npages] = page;
182 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
183 LUSTRE_OPC_ANY, NULL);
184 op_data->op_npages = npages;
185 op_data->op_offset = hash;
186 rc = md_readpage(exp, op_data, page_pool, &request);
187 ll_finish_md_op_data(op_data);
189 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
190 /* Checked by mdc_readpage() */
191 LASSERT(body != NULL);
193 if (body->valid & OBD_MD_FLSIZE)
194 cl_isize_write(inode, body->size);
196 nrdpgs = (request->rq_bulk->bd_nob_transferred +
197 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
198 SetPageUptodate(page0);
201 ptlrpc_req_finished(request);
203 CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
205 for (i = 1; i < npages; i++) {
206 unsigned long offset;
211 if (rc < 0 || i >= nrdpgs) {
212 page_cache_release(page);
216 SetPageUptodate(page);
219 hash = le64_to_cpu(dp->ldp_hash_start);
222 offset = hash_x_index(hash, hash64);
224 prefetchw(&page->flags);
225 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
230 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
231 " %d\n", offset, ret);
232 page_cache_release(page);
235 if (page_pool != &page0)
236 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
241 static void ll_check_page(struct inode *dir, struct page *page)
243 /* XXX: check page format later */
244 SetPageChecked(page);
247 void ll_release_page(struct page *page, int remove)
252 if (likely(page->mapping != NULL))
253 truncate_complete_page(page->mapping, page);
256 page_cache_release(page);
260 * Find, kmap and return page that contains given hash.
262 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
263 __u64 *start, __u64 *end)
265 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
266 struct address_space *mapping = dir->i_mapping;
268 * Complement of hash is used as an index so that
269 * radix_tree_gang_lookup() can be used to find a page with starting
270 * hash _smaller_ than one we are looking for.
272 unsigned long offset = hash_x_index(*hash, hash64);
276 spin_lock_irq(&mapping->tree_lock);
277 found = radix_tree_gang_lookup(&mapping->page_tree,
278 (void **)&page, offset, 1);
280 struct lu_dirpage *dp;
282 page_cache_get(page);
283 spin_unlock_irq(&mapping->tree_lock);
285 * In contrast to find_lock_page() we are sure that directory
286 * page cannot be truncated (while DLM lock is held) and,
287 * hence, can avoid restart.
289 * In fact, page cannot be locked here at all, because
290 * ll_dir_filler() does synchronous io.
292 wait_on_page_locked(page);
293 if (PageUptodate(page)) {
295 if (BITS_PER_LONG == 32 && hash64) {
296 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
297 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
300 *start = le64_to_cpu(dp->ldp_hash_start);
301 *end = le64_to_cpu(dp->ldp_hash_end);
303 LASSERTF(*start <= *hash, "start = "LPX64",end = "
304 LPX64",hash = "LPX64"\n", *start, *end, *hash);
305 CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
306 offset, *start, *end, *hash);
308 ll_release_page(page, 0);
310 } else if (*end != *start && *hash == *end) {
312 * upon hash collision, remove this page,
313 * otherwise put page reference, and
314 * ll_get_dir_page() will issue RPC to fetch
317 ll_release_page(page,
318 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
322 page_cache_release(page);
323 page = ERR_PTR(-EIO);
327 spin_unlock_irq(&mapping->tree_lock);
333 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
334 struct ll_dir_chain *chain)
336 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
337 struct address_space *mapping = dir->i_mapping;
338 struct lustre_handle lockh;
339 struct lu_dirpage *dp;
346 struct ll_inode_info *lli = ll_i2info(dir);
347 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
350 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
351 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
353 struct ldlm_enqueue_info einfo = {
354 .ei_type = LDLM_IBITS,
356 .ei_cb_bl = ll_md_blocking_ast,
357 .ei_cb_cp = ldlm_completion_ast,
359 struct lookup_intent it = { .it_op = IT_READDIR };
360 struct ptlrpc_request *request;
361 struct md_op_data *op_data;
363 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
364 LUSTRE_OPC_ANY, NULL);
366 return (void *)op_data;
368 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
369 op_data, &lockh, NULL, 0, NULL, 0);
371 ll_finish_md_op_data(op_data);
373 request = (struct ptlrpc_request *)it.d.lustre.it_data;
375 ptlrpc_req_finished(request);
377 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
378 PFID(ll_inode2fid(dir)), hash, rc);
382 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
383 dir, dir->i_ino, dir->i_generation);
384 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
385 &it.d.lustre.it_lock_handle, dir, NULL);
387 /* for cross-ref object, l_ast_data of the lock may not be set,
388 * we reset it here */
389 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
392 ldlm_lock_dump_handle(D_OTHER, &lockh);
394 mutex_lock(&lli->lli_readdir_mutex);
395 page = ll_dir_page_locate(dir, &lhash, &start, &end);
397 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
398 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
399 GOTO(out_unlock, page);
400 } else if (page != NULL) {
402 * XXX nikita: not entirely correct handling of a corner case:
403 * suppose hash chain of entries with hash value HASH crosses
404 * border between pages P0 and P1. First both P0 and P1 are
405 * cached, seekdir() is called for some entry from the P0 part
406 * of the chain. Later P0 goes out of cache. telldir(HASH)
407 * happens and finds P1, as it starts with matching hash
408 * value. Remaining entries from P0 part of the chain are
409 * skipped. (Is that really a bug?)
411 * Possible solutions: 0. don't cache P1 is such case, handle
412 * it as an "overflow" page. 1. invalidate all pages at
413 * once. 2. use HASH|1 as an index for P1.
415 GOTO(hash_collision, page);
418 page = read_cache_page(mapping, hash_x_index(hash, hash64),
419 ll_dir_filler, &lhash);
421 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
422 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
423 GOTO(out_unlock, page);
426 wait_on_page_locked(page);
428 if (!PageUptodate(page)) {
429 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
430 PFID(ll_inode2fid(dir)), hash, -5);
433 if (!PageChecked(page))
434 ll_check_page(dir, page);
435 if (PageError(page)) {
436 CERROR("page error: "DFID" at "LPU64": rc %d\n",
437 PFID(ll_inode2fid(dir)), hash, -5);
441 dp = page_address(page);
442 if (BITS_PER_LONG == 32 && hash64) {
443 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
444 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
447 start = le64_to_cpu(dp->ldp_hash_start);
448 end = le64_to_cpu(dp->ldp_hash_end);
452 LASSERT(start == lhash);
453 CWARN("Page-wide hash collision: "LPU64"\n", end);
454 if (BITS_PER_LONG == 32 && hash64)
455 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
456 "] with hash "LPU64"\n",
457 le64_to_cpu(dp->ldp_hash_start),
458 le64_to_cpu(dp->ldp_hash_end), hash);
460 * Fetch whole overflow chain...
467 mutex_unlock(&lli->lli_readdir_mutex);
468 ldlm_lock_decref(&lockh, mode);
472 ll_release_page(page, 1);
473 page = ERR_PTR(-EIO);
477 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
480 struct ll_inode_info *info = ll_i2info(inode);
481 struct ll_sb_info *sbi = ll_i2sbi(inode);
483 int api32 = ll_need_32bit_api(sbi);
484 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
486 struct ll_dir_chain chain;
491 ll_dir_chain_init(&chain);
493 page = ll_get_dir_page(inode, pos, &chain);
495 while (rc == 0 && !done) {
496 struct lu_dirpage *dp;
497 struct lu_dirent *ent;
501 * If page is empty (end of directory is reached),
504 __u64 hash = MDS_DIR_END_OFF;
507 dp = page_address(page);
508 for (ent = lu_dirent_start(dp); ent != NULL && !done;
509 ent = lu_dirent_next(ent)) {
517 * XXX: implement correct swabbing here.
520 hash = le64_to_cpu(ent->lde_hash);
523 * Skip until we find target hash
528 namelen = le16_to_cpu(ent->lde_namelen);
539 fid_le_to_cpu(&fid, &ent->lde_fid);
540 ino = cl_fid_build_ino(&fid, api32);
541 type = ll_dirent_type_get(ent);
542 /* For 'll_nfs_get_name_filldir()', it will try
543 * to access the 'ent' through its 'lde_name',
544 * so the parameter 'name' for 'filldir()' must
545 * be part of the 'ent'. */
546 done = filldir(cookie, ent->lde_name, namelen,
549 next = le64_to_cpu(dp->ldp_hash_end);
552 if (pos == MDS_DIR_END_OFF) {
554 * End of directory reached.
557 ll_release_page(page, 0);
558 } else if (1 /* chain is exhausted*/) {
560 * Normal case: continue to the next
563 ll_release_page(page,
564 le32_to_cpu(dp->ldp_flags) &
567 page = ll_get_dir_page(inode, pos,
571 * go into overflow page.
573 LASSERT(le32_to_cpu(dp->ldp_flags) &
575 ll_release_page(page, 1);
579 ll_release_page(page, 0);
583 CERROR("error reading dir "DFID" at %lu: rc %d\n",
584 PFID(&info->lli_fid), (unsigned long)pos, rc);
589 ll_dir_chain_fini(&chain);
593 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
595 struct inode *inode = filp->f_dentry->d_inode;
596 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
597 struct ll_sb_info *sbi = ll_i2sbi(inode);
599 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
600 int api32 = ll_need_32bit_api(sbi);
602 #ifdef HAVE_TOUCH_ATIME_1ARG
612 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
613 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
614 inode, (unsigned long)pos, i_size_read(inode), api32);
616 if (pos == MDS_DIR_END_OFF)
622 rc = ll_dir_read(inode, &pos, cookie, filldir);
625 if (pos == MDS_DIR_END_OFF) {
627 filp->f_pos = LL_DIR_END_OFF_32BIT;
629 filp->f_pos = LL_DIR_END_OFF;
632 filp->f_pos = pos >> 32;
636 filp->f_version = inode->i_version;
637 #ifdef HAVE_TOUCH_ATIME_1ARG
638 #ifdef HAVE_F_PATH_MNT
639 path.mnt = filp->f_path.mnt;
641 path.mnt = filp->f_vfsmnt;
643 path.dentry = filp->f_dentry;
646 touch_atime(filp->f_vfsmnt, filp->f_dentry);
651 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
656 int ll_send_mgc_param(struct obd_export *mgc, char *string)
658 struct mgs_send_param *msp;
665 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
666 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
667 sizeof(struct mgs_send_param), msp, NULL);
669 CERROR("Failed to set parameter: %d\n", rc);
675 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
678 struct ptlrpc_request *request = NULL;
679 struct md_op_data *op_data;
680 struct ll_sb_info *sbi = ll_i2sbi(dir);
686 mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
687 op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
688 strlen(filename), mode, LUSTRE_OPC_MKDIR,
691 GOTO(err_exit, err = PTR_ERR(op_data));
693 op_data->op_cli_flags |= CLI_SET_MEA;
694 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
695 current_fsuid(), current_fsgid(),
696 cfs_curproc_cap_pack(), 0, &request);
697 ll_finish_md_op_data(op_data);
701 ptlrpc_req_finished(request);
705 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
708 struct ll_sb_info *sbi = ll_i2sbi(inode);
709 struct md_op_data *op_data;
710 struct ptlrpc_request *req = NULL;
712 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
713 struct obd_device *mgc = lsi->lsi_mgc;
719 * This is coming from userspace, so should be in
720 * local endian. But the MDS would like it in little
721 * endian, so we swab it before we send it.
723 switch (lump->lmm_magic) {
724 case LOV_USER_MAGIC_V1: {
725 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
726 lustre_swab_lov_user_md_v1(lump);
727 lum_size = sizeof(struct lov_user_md_v1);
730 case LOV_USER_MAGIC_V3: {
731 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
732 lustre_swab_lov_user_md_v3(
733 (struct lov_user_md_v3 *)lump);
734 lum_size = sizeof(struct lov_user_md_v3);
738 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
739 " %#08x != %#08x nor %#08x\n",
740 lump->lmm_magic, LOV_USER_MAGIC_V1,
746 lum_size = sizeof(struct lov_user_md_v1);
749 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
750 LUSTRE_OPC_ANY, NULL);
752 RETURN(PTR_ERR(op_data));
754 if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
755 op_data->op_cli_flags |= CLI_SET_MEA;
757 /* swabbing is done in lov_setstripe() on server side */
758 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
759 NULL, 0, &req, NULL);
760 ll_finish_md_op_data(op_data);
761 ptlrpc_req_finished(req);
763 if (rc != -EPERM && rc != -EACCES)
764 CERROR("mdc_setattr fails: rc = %d\n", rc);
767 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
768 LOV_USER_MAGIC_V3 have the same initial fields so we do not
769 need the make the distiction between the 2 versions */
770 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
774 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
776 GOTO(end, rc = -ENOMEM);
779 /* Get fsname and assume devname to be -MDT0000. */
780 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
781 strcat(buf, "-MDT0000.lov");
784 /* Set root stripesize */
785 sprintf(buf, ".stripesize=%u",
786 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
787 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
791 /* Set root stripecount */
792 sprintf(buf, ".stripecount=%hd",
793 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
794 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
798 /* Set root stripeoffset */
799 sprintf(buf, ".stripeoffset=%hd",
800 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
801 (typeof(lump->lmm_stripe_offset))(-1));
802 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
806 OBD_FREE(param, MGS_PARAM_MAXLEN);
811 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
812 int *lmm_size, struct ptlrpc_request **request)
814 struct ll_sb_info *sbi = ll_i2sbi(inode);
815 struct mdt_body *body;
816 struct lov_mds_md *lmm = NULL;
817 struct ptlrpc_request *req = NULL;
819 struct md_op_data *op_data;
821 rc = ll_get_max_mdsize(sbi, &lmmsize);
825 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
826 0, lmmsize, LUSTRE_OPC_ANY,
829 RETURN(PTR_ERR(op_data));
831 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
832 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
833 ll_finish_md_op_data(op_data);
835 CDEBUG(D_INFO, "md_getattr failed on inode "
836 "%lu/%u: rc %d\n", inode->i_ino,
837 inode->i_generation, rc);
841 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
842 LASSERT(body != NULL);
844 lmmsize = body->eadatasize;
846 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
848 GOTO(out, rc = -ENODATA);
851 lmm = req_capsule_server_sized_get(&req->rq_pill,
852 &RMF_MDT_MD, lmmsize);
853 LASSERT(lmm != NULL);
856 * This is coming from the MDS, so is probably in
857 * little endian. We convert it to host endian before
858 * passing it to userspace.
860 /* We don't swab objects for directories */
861 switch (le32_to_cpu(lmm->lmm_magic)) {
863 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
864 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
867 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
868 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
871 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
882 * Get MDT index for the inode.
884 int ll_get_mdt_idx(struct inode *inode)
886 struct ll_sb_info *sbi = ll_i2sbi(inode);
887 struct md_op_data *op_data;
891 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
892 0, LUSTRE_OPC_ANY, NULL);
894 RETURN(PTR_ERR(op_data));
896 op_data->op_flags |= MF_GET_MDT_IDX;
897 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
898 mdtidx = op_data->op_mds;
899 ll_finish_md_op_data(op_data);
901 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
908 * Generic handler to do any pre-copy work.
910 * It send a first hsm_progress (with extent length == 0) to coordinator as a
911 * first information for it that real work has started.
913 * Moreover, for a ARCHIVE request, it will sample the file data version and
914 * store it in \a copy.
916 * \return 0 on success.
918 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
920 struct ll_sb_info *sbi = ll_s2sbi(sb);
921 struct hsm_progress_kernel hpk;
925 /* Forge a hsm_progress based on data from copy. */
926 hpk.hpk_fid = copy->hc_hai.hai_fid;
927 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
928 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
929 hpk.hpk_extent.length = 0;
932 hpk.hpk_data_version = 0;
935 /* For archive request, we need to read the current file version. */
936 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
938 __u64 data_version = 0;
940 /* Get inode for this fid */
941 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
943 hpk.hpk_flags |= HP_FLAG_RETRY;
944 /* hpk_errval is >= 0 */
945 hpk.hpk_errval = -PTR_ERR(inode);
946 GOTO(progress, rc = PTR_ERR(inode));
949 /* Read current file data version */
950 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
953 CDEBUG(D_HSM, "Could not read file data version of "
954 DFID" (rc = %d). Archive request ("
955 LPX64") could not be done.\n",
956 PFID(©->hc_hai.hai_fid), rc,
957 copy->hc_hai.hai_cookie);
958 hpk.hpk_flags |= HP_FLAG_RETRY;
959 /* hpk_errval must be >= 0 */
960 hpk.hpk_errval = -rc;
964 /* Store it the hsm_copy for later copytool use.
965 * Always modified even if no lsm. */
966 copy->hc_data_version = data_version;
970 /* On error, the request should be considered as completed */
971 if (hpk.hpk_errval > 0)
972 hpk.hpk_flags |= HP_FLAG_COMPLETED;
973 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
980 * Generic handler to do any post-copy work.
982 * It will send the last hsm_progress update to coordinator to inform it
983 * that copy is finished and whether it was successful or not.
986 * - for ARCHIVE request, it will sample the file data version and compare it
987 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
988 * will be considered as failed.
989 * - for RESTORE request, it will sample the file data version and send it to
990 * coordinator which is useful if the file was imported as 'released'.
992 * \return 0 on success.
994 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
996 struct ll_sb_info *sbi = ll_s2sbi(sb);
997 struct hsm_progress_kernel hpk;
1001 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
1002 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
1003 * initialized if copy_end was called with copy == NULL.
1006 /* Forge a hsm_progress based on data from copy. */
1007 hpk.hpk_fid = copy->hc_hai.hai_fid;
1008 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
1009 hpk.hpk_extent = copy->hc_hai.hai_extent;
1010 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1011 hpk.hpk_errval = copy->hc_errval;
1012 hpk.hpk_data_version = 0;
1014 /* For archive request, we need to check the file data was not changed.
1016 * For restore request, we need to send the file data version, this is
1017 * useful when the file was created using hsm_import.
1019 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1020 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1021 (copy->hc_errval == 0)) {
1022 struct inode *inode;
1023 __u64 data_version = 0;
1025 /* Get lsm for this fid */
1026 inode = search_inode_for_lustre(sb, ©->hc_hai.hai_fid);
1027 if (IS_ERR(inode)) {
1028 hpk.hpk_flags |= HP_FLAG_RETRY;
1029 /* hpk_errval must be >= 0 */
1030 hpk.hpk_errval = -PTR_ERR(inode);
1031 GOTO(progress, rc = PTR_ERR(inode));
1034 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1037 CDEBUG(D_HSM, "Could not read file data version. "
1038 "Request could not be confirmed.\n");
1039 if (hpk.hpk_errval == 0)
1040 hpk.hpk_errval = -rc;
1044 /* Store it the hsm_copy for later copytool use.
1045 * Always modified even if no lsm. */
1046 hpk.hpk_data_version = data_version;
1048 /* File could have been stripped during archiving, so we need
1049 * to check anyway. */
1050 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1051 (copy->hc_data_version != data_version)) {
1052 CDEBUG(D_HSM, "File data version mismatched. "
1053 "File content was changed during archiving. "
1054 DFID", start:"LPX64" current:"LPX64"\n",
1055 PFID(©->hc_hai.hai_fid),
1056 copy->hc_data_version, data_version);
1057 /* File was changed, send error to cdt. Do not ask for
1058 * retry because if a file is modified frequently,
1059 * the cdt will loop on retried archive requests.
1060 * The policy engine will ask for a new archive later
1061 * when the file will not be modified for some tunable
1063 /* we do not notify caller */
1064 hpk.hpk_flags &= ~HP_FLAG_RETRY;
1065 /* hpk_errval must be >= 0 */
1066 hpk.hpk_errval = EBUSY;
1072 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1079 static int copy_and_ioctl(int cmd, struct obd_export *exp,
1080 const void __user *data, size_t size)
1085 OBD_ALLOC(copy, size);
1089 if (copy_from_user(copy, data, size)) {
1094 rc = obd_iocontrol(cmd, exp, size, copy, NULL);
1096 OBD_FREE(copy, size);
1101 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1103 int cmd = qctl->qc_cmd;
1104 int type = qctl->qc_type;
1105 int id = qctl->qc_id;
1106 int valid = qctl->qc_valid;
1111 case LUSTRE_Q_INVALIDATE:
1112 case LUSTRE_Q_FINVALIDATE:
1117 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1118 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1122 if (((type == USRQUOTA && current_euid() != id) ||
1123 (type == GRPQUOTA && !in_egroup_p(id))) &&
1124 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1125 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1131 CERROR("unsupported quotactl op: %#x\n", cmd);
1135 if (valid != QC_GENERAL) {
1136 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1137 RETURN(-EOPNOTSUPP);
1139 if (cmd == Q_GETINFO)
1140 qctl->qc_cmd = Q_GETOINFO;
1141 else if (cmd == Q_GETQUOTA)
1142 qctl->qc_cmd = Q_GETOQUOTA;
1148 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1149 sizeof(*qctl), qctl, NULL);
1152 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1153 sizeof(*qctl), qctl, NULL);
1156 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1157 sizeof(*qctl), qctl, NULL);
1159 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1161 sizeof(*qctl), qctl, NULL);
1173 struct obd_quotactl *oqctl;
1175 OBD_ALLOC_PTR(oqctl);
1179 QCTL_COPY(oqctl, qctl);
1180 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1182 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1183 oqctl->qc_cmd = Q_QUOTAOFF;
1184 obd_quotactl(sbi->ll_md_exp, oqctl);
1186 OBD_FREE_PTR(oqctl);
1189 /* If QIF_SPACE is not set, client should collect the
1190 * space usage from OSSs by itself */
1191 if (cmd == Q_GETQUOTA &&
1192 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1193 !oqctl->qc_dqblk.dqb_curspace) {
1194 struct obd_quotactl *oqctl_tmp;
1196 OBD_ALLOC_PTR(oqctl_tmp);
1197 if (oqctl_tmp == NULL)
1198 GOTO(out, rc = -ENOMEM);
1200 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1201 oqctl_tmp->qc_id = oqctl->qc_id;
1202 oqctl_tmp->qc_type = oqctl->qc_type;
1204 /* collect space usage from OSTs */
1205 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1206 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1207 if (!rc || rc == -EREMOTEIO) {
1208 oqctl->qc_dqblk.dqb_curspace =
1209 oqctl_tmp->qc_dqblk.dqb_curspace;
1210 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1213 /* collect space & inode usage from MDTs */
1214 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1215 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1216 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1217 if (!rc || rc == -EREMOTEIO) {
1218 oqctl->qc_dqblk.dqb_curspace +=
1219 oqctl_tmp->qc_dqblk.dqb_curspace;
1220 oqctl->qc_dqblk.dqb_curinodes =
1221 oqctl_tmp->qc_dqblk.dqb_curinodes;
1222 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1224 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1227 OBD_FREE_PTR(oqctl_tmp);
1230 QCTL_COPY(qctl, oqctl);
1231 OBD_FREE_PTR(oqctl);
1238 ll_getname(const char __user *filename)
1241 char *tmp = __getname();
1244 return ERR_PTR(-ENOMEM);
1246 len = strncpy_from_user(tmp, filename, PATH_MAX);
1249 else if (len > PATH_MAX)
1250 ret = -ENAMETOOLONG;
1259 #define ll_putname(filename) __putname(filename)
1261 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1263 struct inode *inode = file->f_dentry->d_inode;
1264 struct ll_sb_info *sbi = ll_i2sbi(inode);
1265 struct obd_ioctl_data *data;
1269 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1270 inode->i_ino, inode->i_generation, inode, cmd);
1272 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1273 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1276 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1278 case FSFILT_IOC_GETFLAGS:
1279 case FSFILT_IOC_SETFLAGS:
1280 RETURN(ll_iocontrol(inode, file, cmd, arg));
1281 case FSFILT_IOC_GETVERSION_OLD:
1282 case FSFILT_IOC_GETVERSION:
1283 RETURN(put_user(inode->i_generation, (int *)arg));
1284 /* We need to special case any other ioctls we want to handle,
1285 * to send them to the MDS/OST as appropriate and to properly
1286 * network encode the arg field.
1287 case FSFILT_IOC_SETVERSION_OLD:
1288 case FSFILT_IOC_SETVERSION:
1290 case LL_IOC_GET_MDTIDX: {
1293 mdtidx = ll_get_mdt_idx(inode);
1297 if (put_user((int)mdtidx, (int*)arg))
1302 case IOC_MDC_LOOKUP: {
1303 struct ptlrpc_request *request = NULL;
1304 int namelen, len = 0;
1307 struct md_op_data *op_data;
1309 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1314 filename = data->ioc_inlbuf1;
1315 namelen = strlen(filename);
1318 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1319 GOTO(out_free, rc = -EINVAL);
1322 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1323 0, LUSTRE_OPC_ANY, NULL);
1324 if (IS_ERR(op_data))
1325 GOTO(out_free, rc = PTR_ERR(op_data));
1327 op_data->op_valid = OBD_MD_FLID;
1328 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1329 ll_finish_md_op_data(op_data);
1331 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1334 ptlrpc_req_finished(request);
1337 obd_ioctl_freedata(buf, len);
1340 case LL_IOC_LMV_SETSTRIPE: {
1341 struct lmv_user_md *lum;
1349 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1354 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1355 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1356 GOTO(lmv_out_free, rc = -EINVAL);
1358 filename = data->ioc_inlbuf1;
1359 namelen = data->ioc_inllen1;
1362 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1363 GOTO(lmv_out_free, rc = -EINVAL);
1365 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1366 lumlen = data->ioc_inllen2;
1368 if (lum->lum_magic != LMV_USER_MAGIC ||
1369 lumlen != sizeof(*lum)) {
1370 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1371 filename, lum->lum_magic, lumlen, -EFAULT);
1372 GOTO(lmv_out_free, rc = -EINVAL);
1376 * ll_dir_setdirstripe will be used to set dir stripe
1377 * mdc_create--->mdt_reint_create (with dirstripe)
1379 rc = ll_dir_setdirstripe(inode, lum, filename);
1381 obd_ioctl_freedata(buf, len);
1385 case LL_IOC_LOV_SETSTRIPE: {
1386 struct lov_user_md_v3 lumv3;
1387 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1388 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1389 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1391 int set_default = 0;
1393 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1394 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1395 sizeof(lumv3p->lmm_objects[0]));
1396 /* first try with v1 which is smaller than v3 */
1397 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1400 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1401 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1405 if (inode->i_sb->s_root == file->f_dentry)
1408 /* in v1 and v3 cases lumv1 points to data */
1409 rc = ll_dir_setstripe(inode, lumv1, set_default);
1413 case LL_IOC_LMV_GETSTRIPE: {
1414 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1415 struct lmv_user_md lum;
1416 struct lmv_user_md *tmp;
1421 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1424 if (lum.lum_magic != LMV_MAGIC_V1)
1427 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1428 OBD_ALLOC(tmp, lum_size);
1430 GOTO(free_lmv, rc = -ENOMEM);
1432 memcpy(tmp, &lum, sizeof(lum));
1433 tmp->lum_type = LMV_STRIPE_TYPE;
1434 tmp->lum_stripe_count = 1;
1435 mdtindex = ll_get_mdt_idx(inode);
1437 GOTO(free_lmv, rc = -ENOMEM);
1439 tmp->lum_stripe_offset = mdtindex;
1440 tmp->lum_objects[0].lum_mds = mdtindex;
1441 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1442 sizeof(struct lu_fid));
1443 if (copy_to_user((void *)arg, tmp, lum_size))
1444 GOTO(free_lmv, rc = -EFAULT);
1447 OBD_FREE(tmp, lum_size);
1450 case LL_IOC_REMOVE_ENTRY: {
1451 char *filename = NULL;
1455 /* Here is a little hack to avoid sending REINT_RMENTRY to
1456 * unsupported server, which might crash the server(LU-2730),
1457 * Because both LVB_TYPE and REINT_RMENTRY will be supported
1458 * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1459 * server will support REINT_RMENTRY XXX*/
1460 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1463 filename = ll_getname((const char *)arg);
1464 if (IS_ERR(filename))
1465 RETURN(PTR_ERR(filename));
1467 namelen = strlen(filename);
1469 GOTO(out_rmdir, rc = -EINVAL);
1471 rc = ll_rmdir_entry(inode, filename, namelen);
1474 ll_putname(filename);
1477 case LL_IOC_LOV_SWAP_LAYOUTS:
1479 case LL_IOC_OBD_STATFS:
1480 RETURN(ll_obd_statfs(inode, (void *)arg));
1481 case LL_IOC_LOV_GETSTRIPE:
1482 case LL_IOC_MDC_GETINFO:
1483 case IOC_MDC_GETFILEINFO:
1484 case IOC_MDC_GETFILESTRIPE: {
1485 struct ptlrpc_request *request = NULL;
1486 struct lov_user_md *lump;
1487 struct lov_mds_md *lmm = NULL;
1488 struct mdt_body *body;
1489 char *filename = NULL;
1492 if (cmd == IOC_MDC_GETFILEINFO ||
1493 cmd == IOC_MDC_GETFILESTRIPE) {
1494 filename = ll_getname((const char *)arg);
1495 if (IS_ERR(filename))
1496 RETURN(PTR_ERR(filename));
1498 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1499 &lmmsize, &request);
1501 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1505 body = req_capsule_server_get(&request->rq_pill,
1507 LASSERT(body != NULL);
1513 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1514 cmd == LL_IOC_MDC_GETINFO))
1515 GOTO(skip_lmm, rc = 0);
1520 if (cmd == IOC_MDC_GETFILESTRIPE ||
1521 cmd == LL_IOC_LOV_GETSTRIPE) {
1522 lump = (struct lov_user_md *)arg;
1524 struct lov_user_mds_data *lmdp;
1525 lmdp = (struct lov_user_mds_data *)arg;
1526 lump = &lmdp->lmd_lmm;
1528 if (copy_to_user(lump, lmm, lmmsize)) {
1529 if (copy_to_user(lump, lmm, sizeof(*lump)))
1530 GOTO(out_req, rc = -EFAULT);
1534 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1535 struct lov_user_mds_data *lmdp;
1538 st.st_dev = inode->i_sb->s_dev;
1539 st.st_mode = body->mode;
1540 st.st_nlink = body->nlink;
1541 st.st_uid = body->uid;
1542 st.st_gid = body->gid;
1543 st.st_rdev = body->rdev;
1544 st.st_size = body->size;
1545 st.st_blksize = PAGE_CACHE_SIZE;
1546 st.st_blocks = body->blocks;
1547 st.st_atime = body->atime;
1548 st.st_mtime = body->mtime;
1549 st.st_ctime = body->ctime;
1550 st.st_ino = inode->i_ino;
1552 lmdp = (struct lov_user_mds_data *)arg;
1553 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1554 GOTO(out_req, rc = -EFAULT);
1559 ptlrpc_req_finished(request);
1561 ll_putname(filename);
1564 case IOC_LOV_GETINFO: {
1565 struct lov_user_mds_data *lumd;
1566 struct lov_stripe_md *lsm;
1567 struct lov_user_md *lum;
1568 struct lov_mds_md *lmm;
1572 lumd = (struct lov_user_mds_data *)arg;
1573 lum = &lumd->lmd_lmm;
1575 rc = ll_get_max_mdsize(sbi, &lmmsize);
1579 OBD_ALLOC_LARGE(lmm, lmmsize);
1583 if (copy_from_user(lmm, lum, lmmsize))
1584 GOTO(free_lmm, rc = -EFAULT);
1586 switch (lmm->lmm_magic) {
1587 case LOV_USER_MAGIC_V1:
1588 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1590 /* swab objects first so that stripes num will be sane */
1591 lustre_swab_lov_user_md_objects(
1592 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1593 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1594 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1596 case LOV_USER_MAGIC_V3:
1597 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1599 /* swab objects first so that stripes num will be sane */
1600 lustre_swab_lov_user_md_objects(
1601 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1602 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1603 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1606 GOTO(free_lmm, rc = -EINVAL);
1609 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1611 GOTO(free_lmm, rc = -ENOMEM);
1613 /* Perform glimpse_size operation. */
1614 memset(&st, 0, sizeof(st));
1616 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1620 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1621 GOTO(free_lsm, rc = -EFAULT);
1625 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1627 OBD_FREE_LARGE(lmm, lmmsize);
1630 case OBD_IOC_LLOG_CATINFO: {
1631 RETURN(-EOPNOTSUPP);
1633 case OBD_IOC_QUOTACHECK: {
1634 struct obd_quotactl *oqctl;
1637 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1638 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1641 OBD_ALLOC_PTR(oqctl);
1644 oqctl->qc_type = arg;
1645 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1647 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1651 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1653 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1655 OBD_FREE_PTR(oqctl);
1658 case OBD_IOC_POLL_QUOTACHECK: {
1659 struct if_quotacheck *check;
1661 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1662 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1665 OBD_ALLOC_PTR(check);
1669 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1672 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1673 if (copy_to_user((void *)arg, check,
1675 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1679 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1682 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1683 if (copy_to_user((void *)arg, check,
1685 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1689 OBD_FREE_PTR(check);
1692 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1693 case LL_IOC_QUOTACTL_18: {
1694 /* copy the old 1.x quota struct for internal use, then copy
1695 * back into old format struct. For 1.8 compatibility. */
1696 struct if_quotactl_18 *qctl_18;
1697 struct if_quotactl *qctl_20;
1699 OBD_ALLOC_PTR(qctl_18);
1703 OBD_ALLOC_PTR(qctl_20);
1705 GOTO(out_quotactl_18, rc = -ENOMEM);
1707 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1708 GOTO(out_quotactl_20, rc = -ENOMEM);
1710 QCTL_COPY(qctl_20, qctl_18);
1711 qctl_20->qc_idx = 0;
1713 /* XXX: dqb_valid was borrowed as a flag to mark that
1714 * only mds quota is wanted */
1715 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1716 qctl_18->qc_dqblk.dqb_valid) {
1717 qctl_20->qc_valid = QC_MDTIDX;
1718 qctl_20->qc_dqblk.dqb_valid = 0;
1719 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1720 qctl_20->qc_valid = QC_UUID;
1721 qctl_20->obd_uuid = qctl_18->obd_uuid;
1723 qctl_20->qc_valid = QC_GENERAL;
1726 rc = quotactl_ioctl(sbi, qctl_20);
1729 QCTL_COPY(qctl_18, qctl_20);
1730 qctl_18->obd_uuid = qctl_20->obd_uuid;
1732 if (copy_to_user((void *)arg, qctl_18,
1738 OBD_FREE_PTR(qctl_20);
1740 OBD_FREE_PTR(qctl_18);
1744 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1745 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1746 case LL_IOC_QUOTACTL: {
1747 struct if_quotactl *qctl;
1749 OBD_ALLOC_PTR(qctl);
1753 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1754 GOTO(out_quotactl, rc = -EFAULT);
1756 rc = quotactl_ioctl(sbi, qctl);
1758 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1765 case OBD_IOC_GETDTNAME:
1766 case OBD_IOC_GETMDNAME:
1767 RETURN(ll_get_obd_name(inode, cmd, arg));
1768 case LL_IOC_FLUSHCTX:
1769 RETURN(ll_flush_ctx(inode));
1770 #ifdef CONFIG_FS_POSIX_ACL
1771 case LL_IOC_RMTACL: {
1772 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1773 inode == inode->i_sb->s_root->d_inode) {
1774 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1776 LASSERT(fd != NULL);
1777 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1779 fd->fd_flags |= LL_FILE_RMTACL;
1785 case LL_IOC_GETOBDCOUNT: {
1787 struct obd_export *exp;
1789 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1792 /* get ost count when count is zero, get mdt count otherwise */
1793 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1794 vallen = sizeof(count);
1795 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1796 KEY_TGT_COUNT, &vallen, &count, NULL);
1798 CERROR("get target count failed: %d\n", rc);
1802 if (copy_to_user((int *)arg, &count, sizeof(int)))
1807 case LL_IOC_PATH2FID:
1808 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1809 sizeof(struct lu_fid)))
1812 case LL_IOC_GET_CONNECT_FLAGS: {
1813 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1815 case OBD_IOC_CHANGELOG_SEND:
1816 case OBD_IOC_CHANGELOG_CLEAR:
1817 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1818 sizeof(struct ioc_changelog));
1820 case OBD_IOC_FID2PATH:
1821 RETURN(ll_fid2path(inode, (void *)arg));
1822 case LL_IOC_HSM_REQUEST: {
1823 struct hsm_user_request *hur;
1830 /* We don't know the true size yet; copy the fixed-size part */
1831 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1836 /* Compute the whole struct size */
1837 totalsize = hur_len(hur);
1840 /* Make sure the size is reasonable */
1841 if (totalsize >= MDS_MAXREQSIZE)
1844 OBD_ALLOC_LARGE(hur, totalsize);
1848 /* Copy the whole struct */
1849 if (copy_from_user(hur, (void *)arg, totalsize)) {
1850 OBD_FREE_LARGE(hur, totalsize);
1854 if (hur->hur_request.hr_action == HUA_RELEASE) {
1855 const struct lu_fid *fid;
1859 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1860 fid = &hur->hur_user_item[i].hui_fid;
1861 f = search_inode_for_lustre(inode->i_sb, fid);
1867 rc = ll_hsm_release(f);
1873 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1877 OBD_FREE_LARGE(hur, totalsize);
1881 case LL_IOC_HSM_PROGRESS: {
1882 struct hsm_progress_kernel hpk;
1883 struct hsm_progress hp;
1885 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1888 hpk.hpk_fid = hp.hp_fid;
1889 hpk.hpk_cookie = hp.hp_cookie;
1890 hpk.hpk_extent = hp.hp_extent;
1891 hpk.hpk_flags = hp.hp_flags;
1892 hpk.hpk_errval = hp.hp_errval;
1893 hpk.hpk_data_version = 0;
1895 /* File may not exist in Lustre; all progress
1896 * reported to Lustre root */
1897 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1901 case LL_IOC_HSM_CT_START:
1902 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1905 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1906 sizeof(struct lustre_kernelcomm));
1909 case LL_IOC_HSM_COPY_START: {
1910 struct hsm_copy *copy;
1913 OBD_ALLOC_PTR(copy);
1916 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1921 rc = ll_ioc_copy_start(inode->i_sb, copy);
1922 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1928 case LL_IOC_HSM_COPY_END: {
1929 struct hsm_copy *copy;
1932 OBD_ALLOC_PTR(copy);
1935 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1940 rc = ll_ioc_copy_end(inode->i_sb, copy);
1941 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1948 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1953 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1955 struct inode *inode = file->f_mapping->host;
1956 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1957 struct ll_sb_info *sbi = ll_i2sbi(inode);
1958 int api32 = ll_need_32bit_api(sbi);
1959 loff_t ret = -EINVAL;
1962 mutex_lock(&inode->i_mutex);
1967 offset += file->f_pos;
1973 offset += LL_DIR_END_OFF_32BIT;
1975 offset += LL_DIR_END_OFF;
1982 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1983 (!api32 && offset <= LL_DIR_END_OFF))) {
1984 if (offset != file->f_pos) {
1985 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1986 (!api32 && offset == LL_DIR_END_OFF))
1987 fd->lfd_pos = MDS_DIR_END_OFF;
1988 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1989 fd->lfd_pos = offset << 32;
1991 fd->lfd_pos = offset;
1992 file->f_pos = offset;
1993 file->f_version = 0;
2000 mutex_unlock(&inode->i_mutex);
2004 int ll_dir_open(struct inode *inode, struct file *file)
2007 RETURN(ll_file_open(inode, file));
2010 int ll_dir_release(struct inode *inode, struct file *file)
2013 RETURN(ll_file_release(inode, file));
2016 struct file_operations ll_dir_operations = {
2017 .llseek = ll_dir_seek,
2018 .open = ll_dir_open,
2019 .release = ll_dir_release,
2020 .read = generic_read_dir,
2021 .readdir = ll_readdir,
2022 .unlocked_ioctl = ll_dir_ioctl,