1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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 <linux/smp_lock.h>
46 #include <asm/uaccess.h>
47 #include <linux/buffer_head.h> // for wait_on_buffer
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"
60 #ifndef HAVE_PAGE_CHECKED
61 #ifdef HAVE_PG_FS_MISC
62 #define PageChecked(page) test_bit(PG_fs_misc, &(page)->flags)
63 #define SetPageChecked(page) set_bit(PG_fs_misc, &(page)->flags)
65 #error PageChecked or PageFsMisc not defined in kernel
70 * (new) readdir implementation overview.
72 * Original lustre readdir implementation cached exact copy of raw directory
73 * pages on the client. These pages were indexed in client page cache by
74 * logical offset in the directory file. This design, while very simple and
75 * intuitive had some inherent problems:
77 * . it implies that byte offset to the directory entry serves as a
78 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
79 * ext3/htree directory entries may move due to splits, and more
82 * . it is incompatible with the design of split directories for cmd3,
83 * that assumes that names are distributed across nodes based on their
84 * hash, and so readdir should be done in hash order.
86 * New readdir implementation does readdir in hash order, and uses hash of a
87 * file name as a telldir/seekdir cookie. This led to number of complications:
89 * . hash is not unique, so it cannot be used to index cached directory
90 * pages on the client (note, that it requires a whole pageful of hash
91 * collided entries to cause two pages to have identical hashes);
93 * . hash is not unique, so it cannot, strictly speaking, be used as an
94 * entry cookie. ext3/htree has the same problem and lustre implementation
95 * mimics their solution: seekdir(hash) positions directory at the first
96 * entry with the given hash.
102 * Client caches directory pages using hash of the first entry as an index. As
103 * noted above hash is not unique, so this solution doesn't work as is:
104 * special processing is needed for "page hash chains" (i.e., sequences of
105 * pages filled with entries all having the same hash value).
107 * First, such chains have to be detected. To this end, server returns to the
108 * client the hash of the first entry on the page next to one returned. When
109 * client detects that this hash is the same as hash of the first entry on the
110 * returned page, page hash collision has to be handled. Pages in the
111 * hash chain, except first one, are termed "overflow pages".
113 * Solution to index uniqueness problem is to not cache overflow
114 * pages. Instead, when page hash collision is detected, all overflow pages
115 * from emerging chain are immediately requested from the server and placed in
116 * a special data structure (struct ll_dir_chain). This data structure is used
117 * by ll_readdir() to process entries from overflow pages. When readdir
118 * invocation finishes, overflow pages are discarded. If page hash collision
119 * chain weren't completely processed, next call to readdir will again detect
120 * page hash collision, again read overflow pages in, process next portion of
121 * entries and again discard the pages. This is not as wasteful as it looks,
122 * because, given reasonable hash, page hash collisions are extremely rare.
124 * 1. directory positioning
126 * When seekdir(hash) is called, original
137 * identification of and access to overflow pages
147 /* returns the page unlocked, but with a reference */
148 static int ll_dir_readpage(struct file *file, struct page *page)
150 struct inode *inode = page->mapping->host;
151 struct ptlrpc_request *request;
152 struct mdt_body *body;
159 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
161 hash = fd->fd_dir.lfd_next;
163 struct ll_inode_info *lli = ll_i2info(inode);
165 cfs_spin_lock(&lli->lli_sa_lock);
167 LASSERT(lli->lli_sai->sai_pid == cfs_curproc_pid());
169 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
170 hash = lli->lli_sa_pos;
171 cfs_spin_unlock(&lli->lli_sa_lock);
173 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off %lu\n",
174 inode->i_ino, inode->i_generation, inode, (unsigned long)hash);
176 oc = ll_mdscapa_get(inode);
177 rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
178 oc, hash, page, &request);
181 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
182 /* Checked by mdc_readpage() */
183 LASSERT(body != NULL);
185 if (body->valid & OBD_MD_FLSIZE)
186 cl_isize_write(inode, body->size);
187 SetPageUptodate(page);
189 ptlrpc_req_finished(request);
196 #ifndef MS_HAS_NEW_AOPS
197 struct address_space_operations ll_dir_aops = {
198 .readpage = ll_dir_readpage,
201 struct address_space_operations_ext ll_dir_aops = {
202 .orig_aops.readpage = ll_dir_readpage,
206 static void ll_check_page(struct inode *dir, struct page *page)
208 /* XXX: check page format later */
209 SetPageChecked(page);
212 static void ll_release_page(struct page *page, __u64 hash,
213 __u64 start, __u64 end)
217 if (likely(page->mapping != NULL)) {
218 ll_truncate_complete_page(page);
222 CWARN("NULL mapping page %p, truncated by others: "
223 "hash("LPX64") | start("LPX64") | end("LPX64")\n",
224 page, hash, start, end);
226 page_cache_release(page);
230 * Find, kmap and return page that contains given hash.
232 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
233 __u64 *start, __u64 *end)
235 struct address_space *mapping = dir->i_mapping;
237 * Complement of hash is used as an index so that
238 * radix_tree_gang_lookup() can be used to find a page with starting
239 * hash _smaller_ than one we are looking for.
241 unsigned long offset = hash_x_index(*hash);
245 TREE_READ_LOCK_IRQ(mapping);
246 found = radix_tree_gang_lookup(&mapping->page_tree,
247 (void **)&page, offset, 1);
249 struct lu_dirpage *dp;
251 page_cache_get(page);
252 TREE_READ_UNLOCK_IRQ(mapping);
254 * In contrast to find_lock_page() we are sure that directory
255 * page cannot be truncated (while DLM lock is held) and,
256 * hence, can avoid restart.
258 * In fact, page cannot be locked here at all, because
259 * ll_dir_readpage() does synchronous io.
262 if (PageUptodate(page)) {
264 #if BITS_PER_LONG == 32
265 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
266 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
269 *start = le64_to_cpu(dp->ldp_hash_start);
270 *end = le64_to_cpu(dp->ldp_hash_end);
272 LASSERTF(*start <= *hash, "start = "LPX64",end = "
273 LPX64",hash = "LPX64"\n", *start, *end, *hash);
274 if (*hash > *end || (*end != *start && *hash == *end)) {
275 ll_release_page(page, *hash, *start, *end);
279 page_cache_release(page);
280 page = ERR_PTR(-EIO);
284 TREE_READ_UNLOCK_IRQ(mapping);
290 struct page *ll_get_dir_page(struct file *filp, struct inode *dir, __u64 hash,
291 int exact, struct ll_dir_chain *chain)
293 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
294 struct address_space *mapping = dir->i_mapping;
295 struct lustre_handle lockh;
296 struct lu_dirpage *dp;
303 struct ll_inode_info *lli = ll_i2info(dir);
306 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
307 ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
309 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
310 ll_md_blocking_ast, ldlm_completion_ast,
312 struct lookup_intent it = { .it_op = IT_READDIR };
313 struct ptlrpc_request *request;
314 struct md_op_data *op_data;
316 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
317 LUSTRE_OPC_ANY, NULL);
319 return (void *)op_data;
321 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
322 op_data, &lockh, NULL, 0, NULL, 0);
324 ll_finish_md_op_data(op_data);
326 request = (struct ptlrpc_request *)it.d.lustre.it_data;
328 ptlrpc_req_finished(request);
330 CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
331 PFID(ll_inode2fid(dir)), hash, rc);
335 /* for cross-ref object, l_ast_data of the lock may not be set,
336 * we reset it here */
337 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
340 ldlm_lock_dump_handle(D_OTHER, &lockh);
342 cfs_down(&lli->lli_readdir_sem);
343 page = ll_dir_page_locate(dir, &lhash, &start, &end);
345 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
346 PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
347 GOTO(out_unlock, page);
352 * XXX nikita: not entirely correct handling of a corner case:
353 * suppose hash chain of entries with hash value HASH crosses
354 * border between pages P0 and P1. First both P0 and P1 are
355 * cached, seekdir() is called for some entry from the P0 part
356 * of the chain. Later P0 goes out of cache. telldir(HASH)
357 * happens and finds P1, as it starts with matching hash
358 * value. Remaining entries from P0 part of the chain are
359 * skipped. (Is that really a bug?)
361 * Possible solutions: 0. don't cache P1 is such case, handle
362 * it as an "overflow" page. 1. invalidate all pages at
363 * once. 2. use HASH|1 as an index for P1.
365 if (exact && lhash != start) {
367 * readdir asked for a page starting _exactly_ from
368 * given hash, but cache contains stale page, with
369 * entries with smaller hash values. Stale page should
370 * be invalidated, and new one fetched.
372 CDEBUG(D_OTHER, "Stale readpage page %p: "
373 "start = "LPX64",end = "LPX64"hash ="LPX64"\n",
374 page, start, end, lhash);
375 ll_release_page(page, lhash, start, end);
377 GOTO(hash_collision, page);
381 page = read_cache_page(mapping, hash_x_index(hash),
382 (filler_t*)mapping->a_ops->readpage, filp);
384 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
385 PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
386 GOTO(out_unlock, page);
391 if (!PageUptodate(page)) {
392 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
393 PFID(ll_inode2fid(dir)), hash, -5);
396 if (!PageChecked(page))
397 ll_check_page(dir, page);
398 if (PageError(page)) {
399 CERROR("page error: "DFID" at "LPU64": rc %d\n",
400 PFID(ll_inode2fid(dir)), hash, -5);
404 dp = page_address(page);
405 #if BITS_PER_LONG == 32
406 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
407 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
410 start = le64_to_cpu(dp->ldp_hash_start);
411 end = le64_to_cpu(dp->ldp_hash_end);
415 LASSERT(start == lhash);
416 CWARN("Page-wide hash collision: "LPU64"\n", end);
417 #if BITS_PER_LONG == 32
418 CWARN("Real page-wide hash collision at ["LPU64" "LPU64"] with "
419 "hash "LPU64"\n", le64_to_cpu(dp->ldp_hash_start),
420 le64_to_cpu(dp->ldp_hash_end), hash);
423 * Fetch whole overflow chain...
430 cfs_up(&lli->lli_readdir_sem);
431 ldlm_lock_decref(&lockh, mode);
436 page = ERR_PTR(-EIO);
440 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
442 struct inode *inode = filp->f_dentry->d_inode;
443 struct ll_inode_info *info = ll_i2info(inode);
444 struct ll_sb_info *sbi = ll_i2sbi(inode);
445 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
446 __u64 pos = fd->fd_dir.lfd_pos;
447 int need_32bit = ll_need_32bit_api(sbi);
449 struct ll_dir_chain chain;
455 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu 32bit_api %d\n",
456 inode->i_ino, inode->i_generation, inode,
457 (unsigned long)pos, i_size_read(inode), need_32bit);
459 if (pos == DIR_END_OFF)
468 ll_dir_chain_init(&chain);
470 fd->fd_dir.lfd_next = pos;
471 page = ll_get_dir_page(filp, inode, pos, 0, &chain);
473 while (rc == 0 && !done) {
474 struct lu_dirpage *dp;
475 struct lu_dirent *ent;
479 * If page is empty (end of directory is reached),
482 __u64 hash = DIR_END_OFF;
485 dp = page_address(page);
486 for (ent = lu_dirent_start(dp); ent != NULL && !done;
487 ent = lu_dirent_next(ent)) {
495 * XXX: implement correct swabbing here.
498 hash = le64_to_cpu(ent->lde_hash);
501 * Skip until we find target hash
506 namelen = le16_to_cpu(ent->lde_namelen);
517 fid_le_to_cpu(&fid, &ent->lde_fid);
518 ino = cl_fid_build_ino(&fid,need_32bit);
519 type = ll_dirent_type_get(ent);
520 /* For 'll_nfs_get_name_filldir()', it will try
521 * to access the 'ent' through its 'lde_name',
522 * so the parameter 'name' for 'filldir()' must
523 * be part of the 'ent'. */
524 done = filldir(cookie, ent->lde_name, namelen,
527 next = le64_to_cpu(dp->ldp_hash_end);
531 if (pos == DIR_END_OFF) {
533 * End of directory reached.
536 } else if (1 /* chain is exhausted*/) {
538 * Normal case: continue to the next
541 fd->fd_dir.lfd_next = pos;
542 page = ll_get_dir_page(filp, inode, pos,
546 * go into overflow page.
554 CERROR("error reading dir "DFID" at %lu: rc %d\n",
555 PFID(&info->lli_fid), (unsigned long)pos, rc);
559 fd->fd_dir.lfd_pos = pos;
561 if (pos == DIR_END_OFF)
562 filp->f_pos = DIR_END_OFF_32BIT;
564 filp->f_pos = pos >> 32;
568 filp->f_version = inode->i_version;
569 touch_atime(filp->f_vfsmnt, filp->f_dentry);
571 ll_dir_chain_fini(&chain);
576 int ll_send_mgc_param(struct obd_export *mgc, char *string)
578 struct mgs_send_param *msp;
585 strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
586 rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
587 sizeof(struct mgs_send_param), msp, NULL);
589 CERROR("Failed to set parameter: %d\n", rc);
595 char *ll_get_fsname(struct inode *inode)
597 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
601 OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
602 len = strlen(lsi->lsi_lmd->lmd_profile);
603 ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
604 if (ptr && (strcmp(ptr, "-client") == 0))
606 strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
612 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
615 struct ll_sb_info *sbi = ll_i2sbi(inode);
616 struct md_op_data *op_data;
617 struct ptlrpc_request *req = NULL;
619 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
620 struct obd_device *mgc = lsi->lsi_mgc;
621 char *fsname = NULL, *param = NULL;
626 * This is coming from userspace, so should be in
627 * local endian. But the MDS would like it in little
628 * endian, so we swab it before we send it.
630 switch (lump->lmm_magic) {
631 case LOV_USER_MAGIC_V1: {
632 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
633 lustre_swab_lov_user_md_v1(lump);
634 lum_size = sizeof(struct lov_user_md_v1);
637 case LOV_USER_MAGIC_V3: {
638 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
639 lustre_swab_lov_user_md_v3(
640 (struct lov_user_md_v3 *)lump);
641 lum_size = sizeof(struct lov_user_md_v3);
645 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
646 " %#08x != %#08x nor %#08x\n",
647 lump->lmm_magic, LOV_USER_MAGIC_V1,
653 lum_size = sizeof(struct lov_user_md_v1);
656 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
657 LUSTRE_OPC_ANY, NULL);
659 RETURN(PTR_ERR(op_data));
661 /* swabbing is done in lov_setstripe() on server side */
662 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
663 NULL, 0, &req, NULL);
664 ll_finish_md_op_data(op_data);
665 ptlrpc_req_finished(req);
667 if (rc != -EPERM && rc != -EACCES)
668 CERROR("mdc_setattr fails: rc = %d\n", rc);
671 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
672 LOV_USER_MAGIC_V3 have the same initial fields so we do not
673 need the make the distiction between the 2 versions */
674 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
675 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
677 /* Get fsname and assume devname to be -MDT0000. */
678 fsname = ll_get_fsname(inode);
679 /* Set root stripesize */
680 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
681 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
682 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
686 /* Set root stripecount */
687 sprintf(param, "%s-MDT0000.lov.stripecount=%hd", fsname,
688 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
689 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
693 /* Set root stripeoffset */
694 sprintf(param, "%s-MDT0000.lov.stripeoffset=%hd", fsname,
695 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
696 (typeof(lump->lmm_stripe_offset))(-1));
697 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
702 OBD_FREE(fsname, MGS_PARAM_MAXLEN);
704 OBD_FREE(param, MGS_PARAM_MAXLEN);
709 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
710 int *lmm_size, struct ptlrpc_request **request)
712 struct ll_sb_info *sbi = ll_i2sbi(inode);
713 struct mdt_body *body;
714 struct lov_mds_md *lmm = NULL;
715 struct ptlrpc_request *req = NULL;
717 struct md_op_data *op_data;
719 rc = ll_get_max_mdsize(sbi, &lmmsize);
723 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
724 0, lmmsize, LUSTRE_OPC_ANY,
729 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
730 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
731 ll_finish_md_op_data(op_data);
733 CDEBUG(D_INFO, "md_getattr failed on inode "
734 "%lu/%u: rc %d\n", inode->i_ino,
735 inode->i_generation, rc);
739 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
740 LASSERT(body != NULL);
742 lmmsize = body->eadatasize;
744 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
746 GOTO(out, rc = -ENODATA);
749 lmm = req_capsule_server_sized_get(&req->rq_pill,
750 &RMF_MDT_MD, lmmsize);
751 LASSERT(lmm != NULL);
754 * This is coming from the MDS, so is probably in
755 * little endian. We convert it to host endian before
756 * passing it to userspace.
758 /* We don't swab objects for directories */
759 switch (le32_to_cpu(lmm->lmm_magic)) {
761 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
762 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
765 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
766 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
769 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
780 * Get MDT index for the inode.
782 int ll_get_mdt_idx(struct inode *inode)
784 struct ll_sb_info *sbi = ll_i2sbi(inode);
785 struct md_op_data *op_data;
789 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
790 0, LUSTRE_OPC_ANY, NULL);
794 op_data->op_valid |= OBD_MD_MDTIDX;
795 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
796 mdtidx = op_data->op_mds;
797 ll_finish_md_op_data(op_data);
799 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
805 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
813 if (cfs_copy_from_user(ptr, data, len)) {
817 rc = obd_iocontrol(cmd, exp, len, data, NULL);
822 static int ll_dir_ioctl(struct inode *inode, struct file *file,
823 unsigned int cmd, unsigned long arg)
825 struct ll_sb_info *sbi = ll_i2sbi(inode);
826 struct obd_ioctl_data *data;
830 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
831 inode->i_ino, inode->i_generation, inode, cmd);
833 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
834 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
837 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
839 case FSFILT_IOC_GETFLAGS:
840 case FSFILT_IOC_SETFLAGS:
841 RETURN(ll_iocontrol(inode, file, cmd, arg));
842 case FSFILT_IOC_GETVERSION_OLD:
843 case FSFILT_IOC_GETVERSION:
844 RETURN(put_user(inode->i_generation, (int *)arg));
845 /* We need to special case any other ioctls we want to handle,
846 * to send them to the MDS/OST as appropriate and to properly
847 * network encode the arg field.
848 case FSFILT_IOC_SETVERSION_OLD:
849 case FSFILT_IOC_SETVERSION:
851 case LL_IOC_GET_MDTIDX: {
854 mdtidx = ll_get_mdt_idx(inode);
858 if (put_user((int)mdtidx, (int*)arg))
863 case IOC_MDC_LOOKUP: {
864 struct ptlrpc_request *request = NULL;
865 int namelen, len = 0;
868 struct md_op_data *op_data;
870 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
875 filename = data->ioc_inlbuf1;
876 namelen = strlen(filename);
879 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
880 GOTO(out_free, rc = -EINVAL);
883 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
884 0, LUSTRE_OPC_ANY, NULL);
886 GOTO(out_free, rc = -ENOMEM);
888 op_data->op_valid = OBD_MD_FLID;
889 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
890 ll_finish_md_op_data(op_data);
892 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
895 ptlrpc_req_finished(request);
898 obd_ioctl_freedata(buf, len);
901 case LL_IOC_LOV_SETSTRIPE: {
902 struct lov_user_md_v3 lumv3;
903 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
904 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
905 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
909 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
910 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
911 sizeof(lumv3p->lmm_objects[0]));
912 /* first try with v1 which is smaller than v3 */
913 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
916 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
917 if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
921 if (inode->i_sb->s_root == file->f_dentry)
924 /* in v1 and v3 cases lumv1 points to data */
925 rc = ll_dir_setstripe(inode, lumv1, set_default);
929 case LL_IOC_OBD_STATFS:
930 RETURN(ll_obd_statfs(inode, (void *)arg));
931 case LL_IOC_LOV_GETSTRIPE:
932 case LL_IOC_MDC_GETINFO:
933 case IOC_MDC_GETFILEINFO:
934 case IOC_MDC_GETFILESTRIPE: {
935 struct ptlrpc_request *request = NULL;
936 struct lov_user_md *lump;
937 struct lov_mds_md *lmm = NULL;
938 struct mdt_body *body;
939 char *filename = NULL;
942 if (cmd == IOC_MDC_GETFILEINFO ||
943 cmd == IOC_MDC_GETFILESTRIPE) {
944 filename = getname((const char *)arg);
945 if (IS_ERR(filename))
946 RETURN(PTR_ERR(filename));
948 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
951 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
955 body = req_capsule_server_get(&request->rq_pill,
957 LASSERT(body != NULL);
963 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
964 cmd == LL_IOC_MDC_GETINFO))
965 GOTO(skip_lmm, rc = 0);
970 if (cmd == IOC_MDC_GETFILESTRIPE ||
971 cmd == LL_IOC_LOV_GETSTRIPE) {
972 lump = (struct lov_user_md *)arg;
974 struct lov_user_mds_data *lmdp;
975 lmdp = (struct lov_user_mds_data *)arg;
976 lump = &lmdp->lmd_lmm;
978 if (cfs_copy_to_user(lump, lmm, lmmsize)) {
979 if (cfs_copy_to_user(lump, lmm, sizeof(*lump)))
980 GOTO(out_req, rc = -EFAULT);
984 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
985 struct lov_user_mds_data *lmdp;
988 st.st_dev = inode->i_sb->s_dev;
989 st.st_mode = body->mode;
990 st.st_nlink = body->nlink;
991 st.st_uid = body->uid;
992 st.st_gid = body->gid;
993 st.st_rdev = body->rdev;
994 st.st_size = body->size;
995 st.st_blksize = CFS_PAGE_SIZE;
996 st.st_blocks = body->blocks;
997 st.st_atime = body->atime;
998 st.st_mtime = body->mtime;
999 st.st_ctime = body->ctime;
1000 st.st_ino = inode->i_ino;
1002 lmdp = (struct lov_user_mds_data *)arg;
1003 if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1004 GOTO(out_req, rc = -EFAULT);
1009 ptlrpc_req_finished(request);
1014 case IOC_LOV_GETINFO: {
1015 struct lov_user_mds_data *lumd;
1016 struct lov_stripe_md *lsm;
1017 struct lov_user_md *lum;
1018 struct lov_mds_md *lmm;
1022 lumd = (struct lov_user_mds_data *)arg;
1023 lum = &lumd->lmd_lmm;
1025 rc = ll_get_max_mdsize(sbi, &lmmsize);
1029 OBD_ALLOC_LARGE(lmm, lmmsize);
1030 if (cfs_copy_from_user(lmm, lum, lmmsize))
1031 GOTO(free_lmm, rc = -EFAULT);
1033 switch (lmm->lmm_magic) {
1034 case LOV_USER_MAGIC_V1:
1035 if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1037 /* swab objects first so that stripes num will be sane */
1038 lustre_swab_lov_user_md_objects(
1039 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1040 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1041 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1043 case LOV_USER_MAGIC_V3:
1044 if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1046 /* swab objects first so that stripes num will be sane */
1047 lustre_swab_lov_user_md_objects(
1048 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1049 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1050 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1053 GOTO(free_lmm, rc = -EINVAL);
1056 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1058 GOTO(free_lmm, rc = -ENOMEM);
1060 /* Perform glimpse_size operation. */
1061 memset(&st, 0, sizeof(st));
1063 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1067 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1068 GOTO(free_lsm, rc = -EFAULT);
1072 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1074 OBD_FREE_LARGE(lmm, lmmsize);
1077 case OBD_IOC_LLOG_CATINFO: {
1078 struct ptlrpc_request *req = NULL;
1083 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1088 if (!data->ioc_inlbuf1) {
1089 obd_ioctl_freedata(buf, len);
1093 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
1096 GOTO(out_catinfo, rc = -ENOMEM);
1098 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
1100 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
1103 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
1105 ptlrpc_request_free(req);
1106 GOTO(out_catinfo, rc);
1109 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
1110 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
1111 if (data->ioc_inllen2) {
1112 str = req_capsule_client_get(&req->rq_pill,
1114 memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
1117 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
1119 ptlrpc_request_set_replen(req);
1121 rc = ptlrpc_queue_wait(req);
1123 str = req_capsule_server_get(&req->rq_pill,
1125 if (cfs_copy_to_user(data->ioc_pbuf1, str,
1129 ptlrpc_req_finished(req);
1131 obd_ioctl_freedata(buf, len);
1134 case OBD_IOC_QUOTACHECK: {
1135 struct obd_quotactl *oqctl;
1138 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1139 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1142 OBD_ALLOC_PTR(oqctl);
1145 oqctl->qc_type = arg;
1146 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1148 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1152 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1154 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1156 OBD_FREE_PTR(oqctl);
1159 case OBD_IOC_POLL_QUOTACHECK: {
1160 struct if_quotacheck *check;
1162 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1163 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1166 OBD_ALLOC_PTR(check);
1170 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1173 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1174 if (cfs_copy_to_user((void *)arg, check,
1176 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1180 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1183 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1184 if (cfs_copy_to_user((void *)arg, check,
1186 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1190 OBD_FREE_PTR(check);
1193 case OBD_IOC_QUOTACTL: {
1194 struct if_quotactl *qctl;
1195 int cmd, type, id, valid;
1197 OBD_ALLOC_PTR(qctl);
1201 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1202 GOTO(out_quotactl, rc = -EFAULT);
1205 type = qctl->qc_type;
1207 valid = qctl->qc_valid;
1210 case LUSTRE_Q_INVALIDATE:
1211 case LUSTRE_Q_FINVALIDATE:
1216 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1217 sbi->ll_flags & LL_SBI_RMT_CLIENT)
1218 GOTO(out_quotactl, rc = -EPERM);
1221 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1222 (type == GRPQUOTA && !in_egroup_p(id))) &&
1223 (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1224 sbi->ll_flags & LL_SBI_RMT_CLIENT))
1225 GOTO(out_quotactl, rc = -EPERM);
1230 CERROR("unsupported quotactl op: %#x\n", cmd);
1231 GOTO(out_quotactl, rc = -ENOTTY);
1234 if (valid != QC_GENERAL) {
1235 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1236 GOTO(out_quotactl, rc = -EOPNOTSUPP);
1238 if (cmd == Q_GETINFO)
1239 qctl->qc_cmd = Q_GETOINFO;
1240 else if (cmd == Q_GETQUOTA)
1241 qctl->qc_cmd = Q_GETOQUOTA;
1243 GOTO(out_quotactl, rc = -EINVAL);
1247 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1249 sizeof(*qctl), qctl, NULL);
1252 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1254 sizeof(*qctl), qctl, NULL);
1257 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1259 sizeof(*qctl), qctl, NULL);
1261 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1263 sizeof(*qctl), qctl,
1272 GOTO(out_quotactl, rc);
1276 struct obd_quotactl *oqctl;
1278 OBD_ALLOC_PTR(oqctl);
1280 GOTO(out_quotactl, rc = -ENOMEM);
1282 QCTL_COPY(oqctl, qctl);
1283 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1285 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1286 oqctl->qc_cmd = Q_QUOTAOFF;
1287 obd_quotactl(sbi->ll_md_exp, oqctl);
1289 OBD_FREE_PTR(oqctl);
1290 GOTO(out_quotactl, rc);
1292 QCTL_COPY(qctl, oqctl);
1293 OBD_FREE_PTR(oqctl);
1297 if (cfs_copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1304 case OBD_IOC_GETNAME: {
1305 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1308 if (cfs_copy_to_user((void *)arg, obd->obd_name,
1309 strlen(obd->obd_name) + 1))
1313 case LL_IOC_FLUSHCTX:
1314 RETURN(ll_flush_ctx(inode));
1315 #ifdef CONFIG_FS_POSIX_ACL
1316 case LL_IOC_RMTACL: {
1317 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1318 inode == inode->i_sb->s_root->d_inode) {
1319 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1321 LASSERT(fd != NULL);
1322 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1324 fd->fd_flags |= LL_FILE_RMTACL;
1330 case LL_IOC_GETOBDCOUNT: {
1333 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1338 struct lov_obd *lov = &sbi->ll_dt_exp->exp_obd->u.lov;
1339 count = lov->desc.ld_tgt_count;
1342 struct lmv_obd *lmv = &sbi->ll_md_exp->exp_obd->u.lmv;
1343 count = lmv->desc.ld_tgt_count;
1346 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1351 case LL_IOC_PATH2FID:
1352 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1353 sizeof(struct lu_fid)))
1356 case LL_IOC_GET_CONNECT_FLAGS: {
1357 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1359 case OBD_IOC_CHANGELOG_SEND:
1360 case OBD_IOC_CHANGELOG_CLEAR:
1361 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1362 sizeof(struct ioc_changelog));
1364 case OBD_IOC_FID2PATH:
1365 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1366 case LL_IOC_HSM_CT_START:
1367 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1368 sizeof(struct lustre_kernelcomm));
1372 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1376 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1378 struct inode *inode = file->f_mapping->host;
1379 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1380 int need_32bit = ll_need_32bit_api(ll_i2sbi(inode));
1381 loff_t ret = -EINVAL;
1384 cfs_mutex_lock(&inode->i_mutex);
1389 offset += file->f_pos;
1395 offset += DIR_END_OFF_32BIT;
1397 offset += DIR_END_OFF;
1404 ((need_32bit && offset <= DIR_END_OFF_32BIT) || !need_32bit)) {
1405 if (offset != file->f_pos) {
1407 if (offset == DIR_END_OFF_32BIT)
1408 fd->fd_dir.lfd_pos = DIR_END_OFF;
1410 fd->fd_dir.lfd_pos = offset << 32;
1412 fd->fd_dir.lfd_pos = offset;
1414 file->f_pos = offset;
1415 file->f_version = 0;
1422 cfs_mutex_unlock(&inode->i_mutex);
1426 int ll_dir_open(struct inode *inode, struct file *file)
1429 RETURN(ll_file_open(inode, file));
1432 int ll_dir_release(struct inode *inode, struct file *file)
1435 RETURN(ll_file_release(inode, file));
1438 struct file_operations ll_dir_operations = {
1439 .llseek = ll_dir_seek,
1440 .open = ll_dir_open,
1441 .release = ll_dir_release,
1442 .read = generic_read_dir,
1443 .readdir = ll_readdir,
1444 .ioctl = ll_dir_ioctl