4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 <obd_support.h>
52 #include <obd_class.h>
53 #include <lustre_lib.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre_lite.h>
56 #include <lustre_dlm.h>
57 #include <lustre_fid.h>
58 #include "llite_internal.h"
61 * (new) readdir implementation overview.
63 * Original lustre readdir implementation cached exact copy of raw directory
64 * pages on the client. These pages were indexed in client page cache by
65 * logical offset in the directory file. This design, while very simple and
66 * intuitive had some inherent problems:
68 * . it implies that byte offset to the directory entry serves as a
69 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
70 * ext3/htree directory entries may move due to splits, and more
73 * . it is incompatible with the design of split directories for cmd3,
74 * that assumes that names are distributed across nodes based on their
75 * hash, and so readdir should be done in hash order.
77 * New readdir implementation does readdir in hash order, and uses hash of a
78 * file name as a telldir/seekdir cookie. This led to number of complications:
80 * . hash is not unique, so it cannot be used to index cached directory
81 * pages on the client (note, that it requires a whole pageful of hash
82 * collided entries to cause two pages to have identical hashes);
84 * . hash is not unique, so it cannot, strictly speaking, be used as an
85 * entry cookie. ext3/htree has the same problem and lustre implementation
86 * mimics their solution: seekdir(hash) positions directory at the first
87 * entry with the given hash.
93 * Client caches directory pages using hash of the first entry as an index. As
94 * noted above hash is not unique, so this solution doesn't work as is:
95 * special processing is needed for "page hash chains" (i.e., sequences of
96 * pages filled with entries all having the same hash value).
98 * First, such chains have to be detected. To this end, server returns to the
99 * client the hash of the first entry on the page next to one returned. When
100 * client detects that this hash is the same as hash of the first entry on the
101 * returned page, page hash collision has to be handled. Pages in the
102 * hash chain, except first one, are termed "overflow pages".
104 * Solution to index uniqueness problem is to not cache overflow
105 * pages. Instead, when page hash collision is detected, all overflow pages
106 * from emerging chain are immediately requested from the server and placed in
107 * a special data structure (struct ll_dir_chain). This data structure is used
108 * by ll_readdir() to process entries from overflow pages. When readdir
109 * invocation finishes, overflow pages are discarded. If page hash collision
110 * chain weren't completely processed, next call to readdir will again detect
111 * page hash collision, again read overflow pages in, process next portion of
112 * entries and again discard the pages. This is not as wasteful as it looks,
113 * because, given reasonable hash, page hash collisions are extremely rare.
115 * 1. directory positioning
117 * When seekdir(hash) is called, original
128 * identification of and access to overflow pages
132 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
133 * a header lu_dirpage which describes the start/end hash, and whether this
134 * page is empty (contains no dir entry) or hash collide with next page.
135 * After client receives reply, several pages will be integrated into dir page
136 * in CFS_PAGE_SIZE (if CFS_PAGE_SIZE greater than LU_PAGE_SIZE), and the
137 * lu_dirpage for this integrated page will be adjusted.
141 /* returns the page unlocked, but with a reference */
142 static int ll_dir_filler(void *_hash, struct page *page0)
144 struct inode *inode = page0->mapping->host;
145 int hash64 = ll_i2sbi(inode)->ll_flags & LL_SBI_64BIT_HASH;
146 struct obd_export *exp = ll_i2sbi(inode)->ll_md_exp;
147 struct ptlrpc_request *request;
148 struct mdt_body *body;
149 struct md_op_data *op_data;
150 __u64 hash = *((__u64 *)_hash);
151 struct page **page_pool;
153 #ifndef HAVE_ADD_TO_PAGE_CACHE_LRU
154 struct pagevec lru_pvec;
156 struct lu_dirpage *dp;
157 int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> CFS_PAGE_SHIFT;
158 int nrdpgs = 0; /* number of pages read actually */
164 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
165 inode->i_ino, inode->i_generation, inode, hash);
167 LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
169 OBD_ALLOC(page_pool, sizeof(page) * max_pages);
170 if (page_pool != NULL) {
171 page_pool[0] = page0;
176 for (npages = 1; npages < max_pages; npages++) {
177 page = page_cache_alloc_cold(inode->i_mapping);
180 page_pool[npages] = page;
183 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
184 LUSTRE_OPC_ANY, NULL);
185 op_data->op_npages = npages;
186 op_data->op_offset = hash;
187 rc = md_readpage(exp, op_data, page_pool, &request);
188 ll_finish_md_op_data(op_data);
190 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
191 /* Checked by mdc_readpage() */
192 LASSERT(body != NULL);
194 if (body->valid & OBD_MD_FLSIZE)
195 cl_isize_write(inode, body->size);
197 nrdpgs = (request->rq_bulk->bd_nob_transferred+CFS_PAGE_SIZE-1)
199 SetPageUptodate(page0);
202 ptlrpc_req_finished(request);
204 CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
206 ll_pagevec_init(&lru_pvec, 0);
207 for (i = 1; i < npages; i++) {
208 unsigned long offset;
213 if (rc < 0 || i >= nrdpgs) {
214 page_cache_release(page);
218 SetPageUptodate(page);
221 hash = le64_to_cpu(dp->ldp_hash_start);
224 offset = hash_x_index(hash, hash64);
226 prefetchw(&page->flags);
227 ret = ll_add_to_page_cache_lru(page, inode->i_mapping, offset,
231 if (ll_pagevec_add(&lru_pvec, page) == 0)
232 ll_pagevec_lru_add_file(&lru_pvec);
234 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
235 " %d\n", offset, ret);
237 page_cache_release(page);
239 ll_pagevec_lru_add_file(&lru_pvec);
241 if (page_pool != &page0)
242 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
247 static void ll_check_page(struct inode *dir, struct page *page)
249 /* XXX: check page format later */
250 SetPageChecked(page);
253 void ll_release_page(struct page *page, int remove)
258 if (likely(page->mapping != NULL))
259 truncate_complete_page(page->mapping, page);
262 page_cache_release(page);
266 * Find, kmap and return page that contains given hash.
268 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
269 __u64 *start, __u64 *end)
271 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
272 struct address_space *mapping = dir->i_mapping;
274 * Complement of hash is used as an index so that
275 * radix_tree_gang_lookup() can be used to find a page with starting
276 * hash _smaller_ than one we are looking for.
278 unsigned long offset = hash_x_index(*hash, hash64);
282 TREE_READ_LOCK_IRQ(mapping);
283 found = radix_tree_gang_lookup(&mapping->page_tree,
284 (void **)&page, offset, 1);
286 struct lu_dirpage *dp;
288 page_cache_get(page);
289 TREE_READ_UNLOCK_IRQ(mapping);
291 * In contrast to find_lock_page() we are sure that directory
292 * page cannot be truncated (while DLM lock is held) and,
293 * hence, can avoid restart.
295 * In fact, page cannot be locked here at all, because
296 * ll_dir_filler() does synchronous io.
299 if (PageUptodate(page)) {
301 if (BITS_PER_LONG == 32 && hash64) {
302 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
303 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
306 *start = le64_to_cpu(dp->ldp_hash_start);
307 *end = le64_to_cpu(dp->ldp_hash_end);
309 LASSERTF(*start <= *hash, "start = "LPX64",end = "
310 LPX64",hash = "LPX64"\n", *start, *end, *hash);
311 CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
312 offset, *start, *end, *hash);
314 ll_release_page(page, 0);
316 } else if (*end != *start && *hash == *end) {
318 * upon hash collision, remove this page,
319 * otherwise put page reference, and
320 * ll_get_dir_page() will issue RPC to fetch
323 ll_release_page(page,
324 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
328 page_cache_release(page);
329 page = ERR_PTR(-EIO);
333 TREE_READ_UNLOCK_IRQ(mapping);
339 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
340 struct ll_dir_chain *chain)
342 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
343 struct address_space *mapping = dir->i_mapping;
344 struct lustre_handle lockh;
345 struct lu_dirpage *dp;
352 struct ll_inode_info *lli = ll_i2info(dir);
353 int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
356 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
357 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
359 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
360 ll_md_blocking_ast, ldlm_completion_ast,
362 struct lookup_intent it = { .it_op = IT_READDIR };
363 struct ptlrpc_request *request;
364 struct md_op_data *op_data;
366 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
367 LUSTRE_OPC_ANY, NULL);
369 return (void *)op_data;
371 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
372 op_data, &lockh, NULL, 0, NULL, 0);
374 ll_finish_md_op_data(op_data);
376 request = (struct ptlrpc_request *)it.d.lustre.it_data;
378 ptlrpc_req_finished(request);
380 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
381 PFID(ll_inode2fid(dir)), hash, rc);
385 /* for cross-ref object, l_ast_data of the lock may not be set,
386 * we reset it here */
387 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
390 ldlm_lock_dump_handle(D_OTHER, &lockh);
392 cfs_mutex_lock(&lli->lli_readdir_mutex);
393 page = ll_dir_page_locate(dir, &lhash, &start, &end);
395 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
396 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
397 GOTO(out_unlock, page);
398 } else if (page != NULL) {
400 * XXX nikita: not entirely correct handling of a corner case:
401 * suppose hash chain of entries with hash value HASH crosses
402 * border between pages P0 and P1. First both P0 and P1 are
403 * cached, seekdir() is called for some entry from the P0 part
404 * of the chain. Later P0 goes out of cache. telldir(HASH)
405 * happens and finds P1, as it starts with matching hash
406 * value. Remaining entries from P0 part of the chain are
407 * skipped. (Is that really a bug?)
409 * Possible solutions: 0. don't cache P1 is such case, handle
410 * it as an "overflow" page. 1. invalidate all pages at
411 * once. 2. use HASH|1 as an index for P1.
413 GOTO(hash_collision, page);
416 page = read_cache_page(mapping, hash_x_index(hash, hash64),
417 ll_dir_filler, &lhash);
419 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
420 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
421 GOTO(out_unlock, page);
426 if (!PageUptodate(page)) {
427 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
428 PFID(ll_inode2fid(dir)), hash, -5);
431 if (!PageChecked(page))
432 ll_check_page(dir, page);
433 if (PageError(page)) {
434 CERROR("page error: "DFID" at "LPU64": rc %d\n",
435 PFID(ll_inode2fid(dir)), hash, -5);
439 dp = page_address(page);
440 if (BITS_PER_LONG == 32 && hash64) {
441 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
442 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
445 start = le64_to_cpu(dp->ldp_hash_start);
446 end = le64_to_cpu(dp->ldp_hash_end);
450 LASSERT(start == lhash);
451 CWARN("Page-wide hash collision: "LPU64"\n", end);
452 if (BITS_PER_LONG == 32 && hash64)
453 CWARN("Real page-wide hash collision at ["LPU64" "LPU64
454 "] with hash "LPU64"\n",
455 le64_to_cpu(dp->ldp_hash_start),
456 le64_to_cpu(dp->ldp_hash_end), hash);
458 * Fetch whole overflow chain...
465 cfs_mutex_unlock(&lli->lli_readdir_mutex);
466 ldlm_lock_decref(&lockh, mode);
470 ll_release_page(page, 1);
471 page = ERR_PTR(-EIO);
475 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
478 struct ll_inode_info *info = ll_i2info(inode);
479 struct ll_sb_info *sbi = ll_i2sbi(inode);
481 int api32 = ll_need_32bit_api(sbi);
482 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
484 struct ll_dir_chain chain;
489 ll_dir_chain_init(&chain);
491 page = ll_get_dir_page(inode, pos, &chain);
493 while (rc == 0 && !done) {
494 struct lu_dirpage *dp;
495 struct lu_dirent *ent;
499 * If page is empty (end of directory is reached),
502 __u64 hash = MDS_DIR_END_OFF;
505 dp = page_address(page);
506 for (ent = lu_dirent_start(dp); ent != NULL && !done;
507 ent = lu_dirent_next(ent)) {
515 * XXX: implement correct swabbing here.
518 hash = le64_to_cpu(ent->lde_hash);
521 * Skip until we find target hash
526 namelen = le16_to_cpu(ent->lde_namelen);
537 fid_le_to_cpu(&fid, &ent->lde_fid);
538 ino = cl_fid_build_ino(&fid, api32);
539 type = ll_dirent_type_get(ent);
540 /* For 'll_nfs_get_name_filldir()', it will try
541 * to access the 'ent' through its 'lde_name',
542 * so the parameter 'name' for 'filldir()' must
543 * be part of the 'ent'. */
544 done = filldir(cookie, ent->lde_name, namelen,
547 next = le64_to_cpu(dp->ldp_hash_end);
550 if (pos == MDS_DIR_END_OFF) {
552 * End of directory reached.
555 ll_release_page(page, 0);
556 } else if (1 /* chain is exhausted*/) {
558 * Normal case: continue to the next
561 ll_release_page(page,
562 le32_to_cpu(dp->ldp_flags) &
565 page = ll_get_dir_page(inode, pos,
569 * go into overflow page.
571 LASSERT(le32_to_cpu(dp->ldp_flags) &
573 ll_release_page(page, 1);
577 ll_release_page(page, 0);
581 CERROR("error reading dir "DFID" at %lu: rc %d\n",
582 PFID(&info->lli_fid), (unsigned long)pos, rc);
587 ll_dir_chain_fini(&chain);
591 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
593 struct inode *inode = filp->f_dentry->d_inode;
594 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
595 struct ll_sb_info *sbi = ll_i2sbi(inode);
596 __u64 pos = lfd->lfd_pos;
597 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
598 int api32 = ll_need_32bit_api(sbi);
602 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
603 " 32bit_api %d\n", inode->i_ino, inode->i_generation,
604 inode, (unsigned long)pos, i_size_read(inode), api32);
606 if (pos == MDS_DIR_END_OFF)
612 rc = ll_dir_read(inode, &pos, cookie, filldir);
614 if (pos == MDS_DIR_END_OFF) {
616 filp->f_pos = LL_DIR_END_OFF_32BIT;
618 filp->f_pos = LL_DIR_END_OFF;
621 filp->f_pos = pos >> 32;
625 filp->f_version = inode->i_version;
626 touch_atime(filp->f_vfsmnt, filp->f_dentry);
630 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
635 int ll_send_mgc_param(struct obd_export *mgc, char *string)
637 struct mgs_send_param *msp;
644 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
645 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
646 sizeof(struct mgs_send_param), msp, NULL);
648 CERROR("Failed to set parameter: %d\n", rc);
654 char *ll_get_fsname(struct inode *inode)
656 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
660 OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
661 len = strlen(lsi->lsi_lmd->lmd_profile);
662 ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
663 if (ptr && (strcmp(ptr, "-client") == 0))
665 strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
671 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
674 struct ll_sb_info *sbi = ll_i2sbi(inode);
675 struct md_op_data *op_data;
676 struct ptlrpc_request *req = NULL;
678 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
679 struct obd_device *mgc = lsi->lsi_mgc;
680 char *fsname = NULL, *param = NULL;
685 * This is coming from userspace, so should be in
686 * local endian. But the MDS would like it in little
687 * endian, so we swab it before we send it.
689 switch (lump->lmm_magic) {
690 case LOV_USER_MAGIC_V1: {
691 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
692 lustre_swab_lov_user_md_v1(lump);
693 lum_size = sizeof(struct lov_user_md_v1);
696 case LOV_USER_MAGIC_V3: {
697 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
698 lustre_swab_lov_user_md_v3(
699 (struct lov_user_md_v3 *)lump);
700 lum_size = sizeof(struct lov_user_md_v3);
704 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
705 " %#08x != %#08x nor %#08x\n",
706 lump->lmm_magic, LOV_USER_MAGIC_V1,
712 lum_size = sizeof(struct lov_user_md_v1);
715 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
716 LUSTRE_OPC_ANY, NULL);
718 RETURN(PTR_ERR(op_data));
720 /* swabbing is done in lov_setstripe() on server side */
721 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
722 NULL, 0, &req, NULL);
723 ll_finish_md_op_data(op_data);
724 ptlrpc_req_finished(req);
726 if (rc != -EPERM && rc != -EACCES)
727 CERROR("mdc_setattr fails: rc = %d\n", rc);
730 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
731 LOV_USER_MAGIC_V3 have the same initial fields so we do not
732 need the make the distiction between the 2 versions */
733 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
734 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
736 /* Get fsname and assume devname to be -MDT0000. */
737 fsname = ll_get_fsname(inode);
738 /* Set root stripesize */
739 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
740 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
741 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
745 /* Set root stripecount */
746 sprintf(param, "%s-MDT0000.lov.stripecount=%hd", fsname,
747 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
748 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
752 /* Set root stripeoffset */
753 sprintf(param, "%s-MDT0000.lov.stripeoffset=%hd", fsname,
754 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
755 (typeof(lump->lmm_stripe_offset))(-1));
756 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
761 OBD_FREE(fsname, MGS_PARAM_MAXLEN);
763 OBD_FREE(param, MGS_PARAM_MAXLEN);
768 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
769 int *lmm_size, struct ptlrpc_request **request)
771 struct ll_sb_info *sbi = ll_i2sbi(inode);
772 struct mdt_body *body;
773 struct lov_mds_md *lmm = NULL;
774 struct ptlrpc_request *req = NULL;
776 struct md_op_data *op_data;
778 rc = ll_get_max_mdsize(sbi, &lmmsize);
782 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
783 0, lmmsize, LUSTRE_OPC_ANY,
786 RETURN(PTR_ERR(op_data));
788 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
789 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
790 ll_finish_md_op_data(op_data);
792 CDEBUG(D_INFO, "md_getattr failed on inode "
793 "%lu/%u: rc %d\n", inode->i_ino,
794 inode->i_generation, rc);
798 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
799 LASSERT(body != NULL);
801 lmmsize = body->eadatasize;
803 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
805 GOTO(out, rc = -ENODATA);
808 lmm = req_capsule_server_sized_get(&req->rq_pill,
809 &RMF_MDT_MD, lmmsize);
810 LASSERT(lmm != NULL);
813 * This is coming from the MDS, so is probably in
814 * little endian. We convert it to host endian before
815 * passing it to userspace.
817 /* We don't swab objects for directories */
818 switch (le32_to_cpu(lmm->lmm_magic)) {
820 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
821 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
824 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
825 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
828 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
839 * Get MDT index for the inode.
841 int ll_get_mdt_idx(struct inode *inode)
843 struct ll_sb_info *sbi = ll_i2sbi(inode);
844 struct md_op_data *op_data;
848 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
849 0, LUSTRE_OPC_ANY, NULL);
851 RETURN(PTR_ERR(op_data));
853 op_data->op_valid |= OBD_MD_MDTIDX;
854 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
855 mdtidx = op_data->op_mds;
856 ll_finish_md_op_data(op_data);
858 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
864 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
872 if (cfs_copy_from_user(ptr, data, len)) {
876 rc = obd_iocontrol(cmd, exp, len, data, NULL);
881 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
883 int cmd = qctl->qc_cmd;
884 int type = qctl->qc_type;
885 int id = qctl->qc_id;
886 int valid = qctl->qc_valid;
891 case LUSTRE_Q_INVALIDATE:
892 case LUSTRE_Q_FINVALIDATE:
897 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
898 sbi->ll_flags & LL_SBI_RMT_CLIENT)
902 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
903 (type == GRPQUOTA && !in_egroup_p(id))) &&
904 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
905 sbi->ll_flags & LL_SBI_RMT_CLIENT))
911 CERROR("unsupported quotactl op: %#x\n", cmd);
915 if (valid != QC_GENERAL) {
916 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
919 if (cmd == Q_GETINFO)
920 qctl->qc_cmd = Q_GETOINFO;
921 else if (cmd == Q_GETQUOTA)
922 qctl->qc_cmd = Q_GETOQUOTA;
928 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
929 sizeof(*qctl), qctl, NULL);
932 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
933 sizeof(*qctl), qctl, NULL);
936 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
937 sizeof(*qctl), qctl, NULL);
939 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
941 sizeof(*qctl), qctl, NULL);
953 struct obd_quotactl *oqctl;
955 OBD_ALLOC_PTR(oqctl);
959 QCTL_COPY(oqctl, qctl);
960 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
962 if (rc != -EALREADY && cmd == Q_QUOTAON) {
963 oqctl->qc_cmd = Q_QUOTAOFF;
964 obd_quotactl(sbi->ll_md_exp, oqctl);
969 /* If QIF_SPACE is not set, client should collect the
970 * space usage from OSSs by itself */
971 if (cmd == Q_GETQUOTA &&
972 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
973 !oqctl->qc_dqblk.dqb_curspace) {
974 struct obd_quotactl *oqctl_tmp;
976 OBD_ALLOC_PTR(oqctl_tmp);
977 if (oqctl_tmp == NULL)
978 GOTO(out, rc = -ENOMEM);
980 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
981 oqctl_tmp->qc_id = oqctl->qc_id;
982 oqctl_tmp->qc_type = oqctl->qc_type;
984 /* collect space usage from OSTs */
985 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
986 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
987 if (!rc || rc == -EREMOTEIO) {
988 oqctl->qc_dqblk.dqb_curspace =
989 oqctl_tmp->qc_dqblk.dqb_curspace;
990 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
993 /* collect space & inode usage from MDTs */
994 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
995 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
996 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
997 if (!rc || rc == -EREMOTEIO) {
998 oqctl->qc_dqblk.dqb_curspace +=
999 oqctl_tmp->qc_dqblk.dqb_curspace;
1000 oqctl->qc_dqblk.dqb_curinodes =
1001 oqctl_tmp->qc_dqblk.dqb_curinodes;
1002 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1004 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1007 OBD_FREE_PTR(oqctl_tmp);
1010 QCTL_COPY(qctl, oqctl);
1011 OBD_FREE_PTR(oqctl);
1017 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1019 struct inode *inode = file->f_dentry->d_inode;
1020 struct ll_sb_info *sbi = ll_i2sbi(inode);
1021 struct obd_ioctl_data *data;
1025 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1026 inode->i_ino, inode->i_generation, inode, cmd);
1028 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1029 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1032 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1034 case FSFILT_IOC_GETFLAGS:
1035 case FSFILT_IOC_SETFLAGS:
1036 RETURN(ll_iocontrol(inode, file, cmd, arg));
1037 case FSFILT_IOC_GETVERSION_OLD:
1038 case FSFILT_IOC_GETVERSION:
1039 RETURN(put_user(inode->i_generation, (int *)arg));
1040 /* We need to special case any other ioctls we want to handle,
1041 * to send them to the MDS/OST as appropriate and to properly
1042 * network encode the arg field.
1043 case FSFILT_IOC_SETVERSION_OLD:
1044 case FSFILT_IOC_SETVERSION:
1046 case LL_IOC_GET_MDTIDX: {
1049 mdtidx = ll_get_mdt_idx(inode);
1053 if (put_user((int)mdtidx, (int*)arg))
1058 case IOC_MDC_LOOKUP: {
1059 struct ptlrpc_request *request = NULL;
1060 int namelen, len = 0;
1063 struct md_op_data *op_data;
1065 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1070 filename = data->ioc_inlbuf1;
1071 namelen = strlen(filename);
1074 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1075 GOTO(out_free, rc = -EINVAL);
1078 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1079 0, LUSTRE_OPC_ANY, NULL);
1080 if (IS_ERR(op_data))
1081 GOTO(out_free, rc = PTR_ERR(op_data));
1083 op_data->op_valid = OBD_MD_FLID;
1084 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1085 ll_finish_md_op_data(op_data);
1087 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1090 ptlrpc_req_finished(request);
1093 obd_ioctl_freedata(buf, len);
1096 case LL_IOC_LOV_SETSTRIPE: {
1097 struct lov_user_md_v3 lumv3;
1098 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1099 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1100 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1102 int set_default = 0;
1104 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1105 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1106 sizeof(lumv3p->lmm_objects[0]));
1107 /* first try with v1 which is smaller than v3 */
1108 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1111 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1112 if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1116 if (inode->i_sb->s_root == file->f_dentry)
1119 /* in v1 and v3 cases lumv1 points to data */
1120 rc = ll_dir_setstripe(inode, lumv1, set_default);
1124 case LL_IOC_OBD_STATFS:
1125 RETURN(ll_obd_statfs(inode, (void *)arg));
1126 case LL_IOC_LOV_GETSTRIPE:
1127 case LL_IOC_MDC_GETINFO:
1128 case IOC_MDC_GETFILEINFO:
1129 case IOC_MDC_GETFILESTRIPE: {
1130 struct ptlrpc_request *request = NULL;
1131 struct lov_user_md *lump;
1132 struct lov_mds_md *lmm = NULL;
1133 struct mdt_body *body;
1134 char *filename = NULL;
1137 if (cmd == IOC_MDC_GETFILEINFO ||
1138 cmd == IOC_MDC_GETFILESTRIPE) {
1139 filename = getname((const char *)arg);
1140 if (IS_ERR(filename))
1141 RETURN(PTR_ERR(filename));
1143 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1144 &lmmsize, &request);
1146 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1150 body = req_capsule_server_get(&request->rq_pill,
1152 LASSERT(body != NULL);
1158 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1159 cmd == LL_IOC_MDC_GETINFO))
1160 GOTO(skip_lmm, rc = 0);
1165 if (cmd == IOC_MDC_GETFILESTRIPE ||
1166 cmd == LL_IOC_LOV_GETSTRIPE) {
1167 lump = (struct lov_user_md *)arg;
1169 struct lov_user_mds_data *lmdp;
1170 lmdp = (struct lov_user_mds_data *)arg;
1171 lump = &lmdp->lmd_lmm;
1173 if (cfs_copy_to_user(lump, lmm, lmmsize)) {
1174 if (cfs_copy_to_user(lump, lmm, sizeof(*lump)))
1175 GOTO(out_req, rc = -EFAULT);
1179 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1180 struct lov_user_mds_data *lmdp;
1183 st.st_dev = inode->i_sb->s_dev;
1184 st.st_mode = body->mode;
1185 st.st_nlink = body->nlink;
1186 st.st_uid = body->uid;
1187 st.st_gid = body->gid;
1188 st.st_rdev = body->rdev;
1189 st.st_size = body->size;
1190 st.st_blksize = CFS_PAGE_SIZE;
1191 st.st_blocks = body->blocks;
1192 st.st_atime = body->atime;
1193 st.st_mtime = body->mtime;
1194 st.st_ctime = body->ctime;
1195 st.st_ino = inode->i_ino;
1197 lmdp = (struct lov_user_mds_data *)arg;
1198 if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1199 GOTO(out_req, rc = -EFAULT);
1204 ptlrpc_req_finished(request);
1209 case IOC_LOV_GETINFO: {
1210 struct lov_user_mds_data *lumd;
1211 struct lov_stripe_md *lsm;
1212 struct lov_user_md *lum;
1213 struct lov_mds_md *lmm;
1217 lumd = (struct lov_user_mds_data *)arg;
1218 lum = &lumd->lmd_lmm;
1220 rc = ll_get_max_mdsize(sbi, &lmmsize);
1224 OBD_ALLOC_LARGE(lmm, lmmsize);
1225 if (cfs_copy_from_user(lmm, lum, lmmsize))
1226 GOTO(free_lmm, rc = -EFAULT);
1228 switch (lmm->lmm_magic) {
1229 case LOV_USER_MAGIC_V1:
1230 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1232 /* swab objects first so that stripes num will be sane */
1233 lustre_swab_lov_user_md_objects(
1234 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1235 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1236 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1238 case LOV_USER_MAGIC_V3:
1239 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1241 /* swab objects first so that stripes num will be sane */
1242 lustre_swab_lov_user_md_objects(
1243 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1244 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1245 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1248 GOTO(free_lmm, rc = -EINVAL);
1251 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1253 GOTO(free_lmm, rc = -ENOMEM);
1255 /* Perform glimpse_size operation. */
1256 memset(&st, 0, sizeof(st));
1258 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1262 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1263 GOTO(free_lsm, rc = -EFAULT);
1267 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1269 OBD_FREE_LARGE(lmm, lmmsize);
1272 case OBD_IOC_LLOG_CATINFO: {
1273 RETURN(-EOPNOTSUPP);
1275 case OBD_IOC_QUOTACHECK: {
1276 struct obd_quotactl *oqctl;
1279 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1280 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1283 OBD_ALLOC_PTR(oqctl);
1286 oqctl->qc_type = arg;
1287 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1289 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1293 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1295 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1297 OBD_FREE_PTR(oqctl);
1300 case OBD_IOC_POLL_QUOTACHECK: {
1301 struct if_quotacheck *check;
1303 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1304 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1307 OBD_ALLOC_PTR(check);
1311 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1314 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1315 if (cfs_copy_to_user((void *)arg, check,
1317 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1321 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1324 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1325 if (cfs_copy_to_user((void *)arg, check,
1327 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1331 OBD_FREE_PTR(check);
1334 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1335 case LL_IOC_QUOTACTL_18: {
1336 /* copy the old 1.x quota struct for internal use, then copy
1337 * back into old format struct. For 1.8 compatibility. */
1338 struct if_quotactl_18 *qctl_18;
1339 struct if_quotactl *qctl_20;
1341 OBD_ALLOC_PTR(qctl_18);
1345 OBD_ALLOC_PTR(qctl_20);
1347 GOTO(out_quotactl_18, rc = -ENOMEM);
1349 if (cfs_copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1350 GOTO(out_quotactl_20, rc = -ENOMEM);
1352 QCTL_COPY(qctl_20, qctl_18);
1353 qctl_20->qc_idx = 0;
1355 /* XXX: dqb_valid was borrowed as a flag to mark that
1356 * only mds quota is wanted */
1357 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1358 qctl_18->qc_dqblk.dqb_valid) {
1359 qctl_20->qc_valid = QC_MDTIDX;
1360 qctl_20->qc_dqblk.dqb_valid = 0;
1361 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1362 qctl_20->qc_valid = QC_UUID;
1363 qctl_20->obd_uuid = qctl_18->obd_uuid;
1365 qctl_20->qc_valid = QC_GENERAL;
1368 rc = quotactl_ioctl(sbi, qctl_20);
1371 QCTL_COPY(qctl_18, qctl_20);
1372 qctl_18->obd_uuid = qctl_20->obd_uuid;
1374 if (cfs_copy_to_user((void *)arg, qctl_18,
1380 OBD_FREE_PTR(qctl_20);
1382 OBD_FREE_PTR(qctl_18);
1386 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1387 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1388 case LL_IOC_QUOTACTL: {
1389 struct if_quotactl *qctl;
1391 OBD_ALLOC_PTR(qctl);
1395 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1396 GOTO(out_quotactl, rc = -EFAULT);
1398 rc = quotactl_ioctl(sbi, qctl);
1400 if (rc == 0 && cfs_copy_to_user((void *)arg,qctl,sizeof(*qctl)))
1407 case OBD_IOC_GETDTNAME:
1408 case OBD_IOC_GETMDNAME:
1409 RETURN(ll_get_obd_name(inode, cmd, arg));
1410 case LL_IOC_FLUSHCTX:
1411 RETURN(ll_flush_ctx(inode));
1412 #ifdef CONFIG_FS_POSIX_ACL
1413 case LL_IOC_RMTACL: {
1414 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1415 inode == inode->i_sb->s_root->d_inode) {
1416 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1418 LASSERT(fd != NULL);
1419 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1421 fd->fd_flags |= LL_FILE_RMTACL;
1427 case LL_IOC_GETOBDCOUNT: {
1429 struct obd_export *exp;
1431 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1434 /* get ost count when count is zero, get mdt count otherwise */
1435 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1436 vallen = sizeof(count);
1437 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1438 KEY_TGT_COUNT, &vallen, &count, NULL);
1440 CERROR("get target count failed: %d\n", rc);
1444 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1449 case LL_IOC_PATH2FID:
1450 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1451 sizeof(struct lu_fid)))
1454 case LL_IOC_GET_CONNECT_FLAGS: {
1455 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1457 case OBD_IOC_CHANGELOG_SEND:
1458 case OBD_IOC_CHANGELOG_CLEAR:
1459 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1460 sizeof(struct ioc_changelog));
1462 case OBD_IOC_FID2PATH:
1463 RETURN(ll_fid2path(inode, (void *)arg));
1464 case LL_IOC_HSM_CT_START:
1465 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1466 sizeof(struct lustre_kernelcomm));
1470 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1474 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1476 struct inode *inode = file->f_mapping->host;
1477 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1478 struct ll_sb_info *sbi = ll_i2sbi(inode);
1479 int api32 = ll_need_32bit_api(sbi);
1480 loff_t ret = -EINVAL;
1483 cfs_mutex_lock(&inode->i_mutex);
1488 offset += file->f_pos;
1494 offset += LL_DIR_END_OFF_32BIT;
1496 offset += LL_DIR_END_OFF;
1503 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1504 (!api32 && offset <= LL_DIR_END_OFF))) {
1505 if (offset != file->f_pos) {
1506 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1507 (!api32 && offset == LL_DIR_END_OFF))
1508 fd->lfd_pos = MDS_DIR_END_OFF;
1509 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1510 fd->lfd_pos = offset << 32;
1512 fd->lfd_pos = offset;
1513 file->f_pos = offset;
1514 file->f_version = 0;
1521 cfs_mutex_unlock(&inode->i_mutex);
1525 int ll_dir_open(struct inode *inode, struct file *file)
1528 RETURN(ll_file_open(inode, file));
1531 int ll_dir_release(struct inode *inode, struct file *file)
1534 RETURN(ll_file_release(inode, file));
1537 struct file_operations ll_dir_operations = {
1538 .llseek = ll_dir_seek,
1539 .open = ll_dir_open,
1540 .release = ll_dir_release,
1541 .read = generic_read_dir,
1542 .readdir = ll_readdir,
1543 .unlocked_ioctl = ll_dir_ioctl,