1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/dir.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext2 directory handling functions
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
21 * All code that works with directory layout had been switched to pagecache
24 * Adapted for Lustre Light
25 * Copyright (C) 2002-2003, Cluster File Systems, Inc.
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
33 #include <linux/version.h>
34 #include <linux/smp_lock.h>
35 #include <asm/uaccess.h>
36 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
37 # include <linux/locks.h> // for wait_on_buffer
39 # include <linux/buffer_head.h> // for wait_on_buffer
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_lib.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_lite.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
52 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
53 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
56 * (new) readdir implementation overview.
58 * Original lustre readdir implementation cached exact copy of raw directory
59 * pages on the client. These pages were indexed in client page cache by
60 * logical offset in the directory file. This design, while very simple and
61 * intuitive had some inherent problems:
63 * . it implies that byte offset to the directory entry serves as a
64 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
65 * ext3/htree directory entries may move due to splits, and more
68 * . it is incompatible with the design of split directories for cmd3,
69 * that assumes that names are distributed across nodes based on their
70 * hash, and so readdir should be done in hash order.
72 * New readdir implementation does readdir in hash order, and uses hash of a
73 * file name as a telldir/seekdir cookie. This led to number of complications:
75 * . hash is not unique, so it cannot be used to index cached directory
76 * pages on the client (note, that it requires a whole pageful of hash
77 * collided entries to cause two pages to have identical hashes);
79 * . hash is not unique, so it cannot, strictly speaking, be used as an
80 * entry cookie. ext3/htree has the same problem and lustre implementation
81 * mimics their solution: seekdir(hash) positions directory at the first
82 * entry with the given hash.
88 * Client caches directory pages using hash of the first entry as an index. As
89 * noted above hash is not unique, so this solution doesn't work as is:
90 * special processing is needed for "page hash chains" (i.e., sequences of
91 * pages filled with entries all having the same hash value).
93 * First, such chains have to be detected. To this end, server returns to the
94 * client the hash of the first entry on the page next to one returned. When
95 * client detects that this hash is the same as hash of the first entry on the
96 * returned page, page hash collision has to be handled. Pages in the
97 * hash chain, except first one, are termed "overflow pages".
99 * Solution to index uniqueness problem is to not cache overflow
100 * pages. Instead, when page hash collision is detected, all overflow pages
101 * from emerging chain are immediately requested from the server and placed in
102 * a special data structure (struct ll_dir_chain). This data structure is used
103 * by ll_readdir() to process entries from overflow pages. When readdir
104 * invocation finishes, overflow pages are discarded. If page hash collision
105 * chain weren't completely processed, next call to readdir will again detect
106 * page hash collision, again read overflow pages in, process next portion of
107 * entries and again discard the pages. This is not as wasteful as it looks,
108 * because, given reasonable hash, page hash collisions are extremely rare.
110 * 1. directory positioning
112 * When seekdir(hash) is called, original
123 * identification of and access to overflow pages
133 static __u32 hash_x_index(__u32 value)
135 return ((__u32)~0) - value;
138 /* returns the page unlocked, but with a reference */
139 static int ll_dir_readpage(struct file *file, struct page *page)
141 struct inode *inode = page->mapping->host;
142 struct ptlrpc_request *request;
143 struct mdt_body *body;
148 hash = hash_x_index(page->index);
149 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off %lu\n",
150 inode->i_ino, inode->i_generation, inode, (unsigned long)hash);
152 rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
153 hash, page, &request);
155 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
157 LASSERT(body != NULL); /* checked by md_readpage() */
158 /* swabbed by md_readpage() */
159 LASSERT_REPSWABBED(request, REPLY_REC_OFF);
161 if (body->valid & OBD_MD_FLSIZE)
162 inode->i_size = body->size;
163 SetPageUptodate(page);
165 ptlrpc_req_finished(request);
172 struct address_space_operations ll_dir_aops = {
173 .readpage = ll_dir_readpage,
176 static inline unsigned long dir_pages(struct inode *inode)
178 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
181 static inline unsigned ll_chunk_size(struct inode *inode)
183 return inode->i_sb->s_blocksize;
186 static void ll_check_page(struct inode *dir, struct page *page)
188 /* XXX: check page format later */
189 SetPageChecked(page);
192 static inline void ll_put_page(struct page *page)
195 page_cache_release(page);
199 * Find, kmap and return page that contains given hash.
201 static struct page *ll_dir_page_locate(struct inode *dir, unsigned long hash,
202 __u32 *start, __u32 *end)
204 struct address_space *mapping = dir->i_mapping;
206 * Complement of hash is used as an index so that
207 * radix_tree_gang_lookup() can be used to find a page with starting
208 * hash _smaller_ than one we are looking for.
210 unsigned long offset = hash_x_index(hash);
214 spin_lock_irq(&mapping->tree_lock);
215 found = radix_tree_gang_lookup(&mapping->page_tree,
216 (void **)&page, offset, 1);
218 struct lu_dirpage *dp;
220 page_cache_get(page);
221 spin_unlock_irq(&mapping->tree_lock);
223 * In contrast to find_lock_page() we are sure that directory
224 * page cannot be truncated (while DLM lock is held) and,
225 * hence, can avoid restart.
227 * In fact, page cannot be locked here at all, because
228 * ll_dir_readpage() does synchronous io.
231 if (PageUptodate(page)) {
233 *start = le32_to_cpu(dp->ldp_hash_start);
234 *end = le32_to_cpu(dp->ldp_hash_end);
235 LASSERT(*start <= hash);
236 if (hash > *end || (*end != *start && hash == *end)) {
238 page_cache_release(page);
242 page_cache_release(page);
243 page = ERR_PTR(-EIO);
247 spin_unlock_irq(&mapping->tree_lock);
254 * Chain of hash overflow pages.
256 struct ll_dir_chain {
257 /* XXX something. Later */
260 static void ll_dir_chain_init(struct ll_dir_chain *chain)
264 static void ll_dir_chain_fini(struct ll_dir_chain *chain)
268 static struct page *ll_get_dir_page(struct inode *dir, __u32 hash, int exact,
269 struct ll_dir_chain *chain)
271 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
272 struct address_space *mapping = dir->i_mapping;
273 struct lustre_handle lockh;
275 struct lu_dirpage *dp;
280 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
281 ll_inode2fid(dir), LDLM_IBITS, &policy, LCK_CR, &lockh);
283 struct lookup_intent it = { .it_op = IT_READDIR };
284 struct ptlrpc_request *request;
285 struct md_op_data *op_data;
287 OBD_ALLOC_PTR(op_data);
289 return ERR_PTR(-ENOMEM);
291 ll_prepare_md_op_data(op_data, dir, NULL, NULL, 0, 0);
293 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
294 LCK_CR, op_data, &lockh, NULL, 0,
295 ldlm_completion_ast, ll_md_blocking_ast, dir,
298 OBD_FREE_PTR(op_data);
300 request = (struct ptlrpc_request *)it.d.lustre.it_data;
302 ptlrpc_req_finished(request);
304 CERROR("lock enqueue: rc: %d\n", rc);
308 ldlm_lock_dump_handle(D_OTHER, &lockh);
310 page = ll_dir_page_locate(dir, hash, &start, &end);
312 GOTO(out_unlock, page);
316 * XXX nikita: not entirely correct handling of a corner case:
317 * suppose hash chain of entries with hash value HASH crosses
318 * border between pages P0 and P1. First both P0 and P1 are
319 * cached, seekdir() is called for some entry from the P0 part
320 * of the chain. Later P0 goes out of cache. telldir(HASH)
321 * happens and finds P1, as it starts with matching hash
322 * value. Remaining entries from P0 part of the chain are
323 * skipped. (Is that really a bug?)
325 * Possible solutions: 0. don't cache P1 is such case, handle
326 * it as an "overflow" page. 1. invalidate all pages at
327 * once. 2. use HASH|1 as an index for P1.
329 if (exact && hash != start) {
331 * readdir asked for a page starting _exactly_ from
332 * given hash, but cache contains stale page, with
333 * entries with smaller hash values. Stale page should
334 * be invalidated, and new one fetched.
336 CWARN("Stale readpage page: %#lx != %#lx\n",
337 (unsigned long)hash, (unsigned long)start);
338 truncate_complete_page(mapping, page);
339 page_cache_release(page);
341 GOTO(hash_collision, page);
344 page = read_cache_page(mapping, hash_x_index(hash),
345 (filler_t*)mapping->a_ops->readpage, NULL);
347 GOTO(out_unlock, page);
351 if (!PageUptodate(page))
353 if (!PageChecked(page))
354 ll_check_page(dir, page);
358 dp = page_address(page);
360 start = le32_to_cpu(dp->ldp_hash_start);
361 end = le32_to_cpu(dp->ldp_hash_end);
363 LASSERT(start == hash);
364 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
366 * Fetch whole overflow chain...
373 ldlm_lock_decref(&lockh, LCK_CR);
378 page = ERR_PTR(-EIO);
382 static loff_t ll_llseek(struct file *filp, loff_t off, int whence)
384 if (off != 0 || whence != 1 /* SEEK_CUR */) {
386 * Except when telldir() is going on, reset readdir to the
387 * beginning of hash collision chain.
389 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
391 fd->fd_dir.lfd_dup = 0;
393 return default_llseek(filp, off, whence);
396 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
398 struct inode *inode = filp->f_dentry->d_inode;
399 struct ll_inode_info *info = ll_i2info(inode);
400 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
401 struct ll_sb_info *sbi = ll_i2sbi(inode);
402 __u32 pos = filp->f_pos;
404 struct ll_dir_chain chain;
412 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu\n",
413 inode->i_ino, inode->i_generation, inode,
414 (unsigned long)pos, inode->i_size);
426 prevhash = ~0; /* impossible hash value */
427 ll_dir_chain_init(&chain);
429 page = ll_get_dir_page(inode, pos, 0, &chain);
431 while (rc == 0 && !done) {
432 struct lu_dirpage *dp;
433 struct lu_dirent *ent;
436 __u32 hash; /* no, Richard, it _is_ initialized */
439 dp = page_address(page);
440 for (ent = lu_dirent_start(dp); ent != NULL && !done;
441 ent = lu_dirent_next(ent)) {
448 * XXX: implement correct swabbing here.
451 hash = le32_to_cpu(ent->lde_hash);
452 namelen = le16_to_cpu(ent->lde_namelen);
456 * Skip until we find target hash
467 * Keep track of how far we get into duplicate
470 if (hash == prevhash)
474 if (hash == fd->fd_dir.lfd_duppos &&
475 fd->fd_dir.lfd_dup > 0) {
476 fd->fd_dir.lfd_dup--;
481 name = ent->lde_name;
483 ino = ll_fid_build_ino(sbi, &fid);
485 done = filldir(cookie, name, namelen,
486 hash, ino, DT_UNKNOWN);
488 next = le32_to_cpu(dp->ldp_hash_end);
494 * End of directory reached.
497 else if (1 /* chain is exhausted*/)
499 * Normal case: continue to the next
502 page = ll_get_dir_page(inode, pos, 1,
506 * go into overflow page.
513 CERROR("error reading dir "DFID" at %lu: rc %d\n",
514 PFID(&info->lli_fid), (unsigned long)pos, rc);
519 filp->f_version = inode->i_version;
520 fd->fd_dir.lfd_dup = dup;
521 fd->fd_dir.lfd_duppos = prevhash;
522 touch_atime(filp->f_vfsmnt, filp->f_dentry);
524 ll_dir_chain_fini(&chain);
529 #define QCTL_COPY(out, in) \
531 Q_COPY(out, in, qc_cmd); \
532 Q_COPY(out, in, qc_type); \
533 Q_COPY(out, in, qc_id); \
534 Q_COPY(out, in, qc_stat); \
535 Q_COPY(out, in, qc_dqinfo); \
536 Q_COPY(out, in, qc_dqblk); \
539 static int ll_dir_ioctl(struct inode *inode, struct file *file,
540 unsigned int cmd, unsigned long arg)
542 struct ll_sb_info *sbi = ll_i2sbi(inode);
543 struct obd_ioctl_data *data;
546 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
547 inode->i_ino, inode->i_generation, inode, cmd);
549 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
550 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
553 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
555 case EXT3_IOC_GETFLAGS:
556 case EXT3_IOC_SETFLAGS:
557 RETURN(ll_iocontrol(inode, file, cmd, arg));
558 case EXT3_IOC_GETVERSION_OLD:
559 case EXT3_IOC_GETVERSION:
560 RETURN(put_user(inode->i_generation, (int *)arg));
561 /* We need to special case any other ioctls we want to handle,
562 * to send them to the MDS/OST as appropriate and to properly
563 * network encode the arg field.
564 case EXT3_IOC_SETVERSION_OLD:
565 case EXT3_IOC_SETVERSION:
567 case IOC_MDC_LOOKUP: {
568 struct ptlrpc_request *request = NULL;
569 int namelen, rc, len = 0;
573 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
578 filename = data->ioc_inlbuf1;
579 namelen = data->ioc_inllen1;
582 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
583 GOTO(out, rc = -EINVAL);
586 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
587 filename, namelen, OBD_MD_FLID, 0,
590 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
594 ptlrpc_req_finished(request);
598 obd_ioctl_freedata(buf, len);
601 case LL_IOC_LOV_SETSTRIPE: {
602 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
603 struct ptlrpc_request *request = NULL;
604 struct md_op_data *op_data;
605 struct iattr attr = { 0 };
608 OBD_ALLOC_PTR(op_data);
612 ll_prepare_md_op_data(op_data, inode,
615 LASSERT(sizeof(lum) == sizeof(*lump));
616 LASSERT(sizeof(lum.lmm_objects[0]) ==
617 sizeof(lump->lmm_objects[0]));
618 rc = copy_from_user(&lum, lump, sizeof(lum));
623 * This is coming from userspace, so should be in
624 * local endian. But the MDS would like it in little
625 * endian, so we swab it before we send it.
627 if (lum.lmm_magic != LOV_USER_MAGIC)
630 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
631 lustre_swab_lov_user_md(&lum);
633 /* swabbing is done in lov_setstripe() on server side */
634 rc = md_setattr(sbi->ll_md_exp, op_data,
635 &attr, &lum, sizeof(lum), NULL, 0, &request);
637 if (rc != -EPERM && rc != -EACCES)
638 CERROR("md_setattr fails: rc = %d\n", rc);
640 OBD_FREE_PTR(op_data);
641 ptlrpc_req_finished(request);
644 case LL_IOC_OBD_STATFS:
645 RETURN(ll_obd_statfs(inode, (void *)arg));
646 case LL_IOC_LOV_GETSTRIPE:
647 case LL_IOC_MDC_GETINFO:
648 case IOC_MDC_GETFILEINFO:
649 case IOC_MDC_GETFILESTRIPE: {
650 struct ptlrpc_request *request = NULL;
651 struct lov_user_md *lump;
652 struct lov_mds_md *lmm = NULL;
653 struct mdt_body *body;
654 char *filename = NULL;
657 rc = ll_get_max_mdsize(sbi, &lmmsize);
661 if (cmd == IOC_MDC_GETFILEINFO ||
662 cmd == IOC_MDC_GETFILESTRIPE) {
663 filename = getname((const char *)arg);
664 if (IS_ERR(filename))
665 RETURN(PTR_ERR(filename));
667 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
668 filename, strlen(filename) + 1,
669 OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
672 CDEBUG(D_INFO, "md_getattr_name failed "
673 "on %s: rc %d\n", filename, rc);
677 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
678 OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
681 CDEBUG(D_INFO, "md_getattr failed on inode "
682 "%lu/%u: rc %d\n", inode->i_ino,
683 inode->i_generation, rc);
688 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
690 LASSERT(body != NULL); /* checked by md_getattr_name */
691 /* swabbed by md_getattr_name */
692 LASSERT_REPSWABBED(request, REPLY_REC_OFF);
694 lmmsize = body->eadatasize;
696 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
698 if (cmd == LL_IOC_LOV_GETSTRIPE ||
699 cmd == IOC_MDC_GETFILESTRIPE)
700 GOTO(out_req, rc = -ENODATA);
702 GOTO(skip_lmm, rc = 0);
705 lmm = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 1,
707 LASSERT(lmm != NULL);
708 LASSERT_REPSWABBED(request, REPLY_REC_OFF + 1);
711 * This is coming from the MDS, so is probably in
712 * little endian. We convert it to host endian before
713 * passing it to userspace.
715 if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
716 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
717 lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
718 } else if (lmm->lmm_magic == __swab32(LOV_MAGIC_JOIN)) {
719 lustre_swab_lov_user_md_join((struct lov_user_md_join *)lmm);
721 if (lmm->lmm_magic == LOV_MAGIC_JOIN) {
722 struct lov_stripe_md *lsm;
723 struct lov_user_md_join *lmj;
724 int lmj_size, i, aindex = 0, rc;
726 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
728 GOTO(out_req, rc = -ENOMEM);
729 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
731 GOTO(out_free_memmd, rc);
733 lmj_size = sizeof(struct lov_user_md_join) +
734 lsm->lsm_stripe_count *
735 sizeof(struct lov_user_ost_data_join);
736 OBD_ALLOC(lmj, lmj_size);
738 GOTO(out_free_memmd, rc = -ENOMEM);
740 memcpy(lmj, lmm, sizeof(struct lov_user_md_join));
741 for (i = 0; i < lsm->lsm_stripe_count; i++) {
742 struct lov_extent *lex =
743 &lsm->lsm_array->lai_ext_array[aindex];
745 if (lex->le_loi_idx + lex->le_stripe_count <= i)
747 CDEBUG(D_INFO, "aindex %d i %d l_extent_start "
748 LPU64" len %d\n", aindex, i,
749 lex->le_start, (int)lex->le_len);
750 lmj->lmm_objects[i].l_extent_start =
753 if ((int)lex->le_len == -1)
754 lmj->lmm_objects[i].l_extent_end = -1;
756 lmj->lmm_objects[i].l_extent_end =
757 lex->le_start + lex->le_len;
758 lmj->lmm_objects[i].l_object_id =
759 lsm->lsm_oinfo[i].loi_id;
760 lmj->lmm_objects[i].l_object_gr =
761 lsm->lsm_oinfo[i].loi_gr;
762 lmj->lmm_objects[i].l_ost_gen =
763 lsm->lsm_oinfo[i].loi_ost_gen;
764 lmj->lmm_objects[i].l_ost_idx =
765 lsm->lsm_oinfo[i].loi_ost_idx;
767 lmm = (struct lov_mds_md *)lmj;
770 obd_free_memmd(sbi->ll_dt_exp, &lsm);
774 if (cmd == IOC_MDC_GETFILESTRIPE ||
775 cmd == LL_IOC_LOV_GETSTRIPE) {
776 lump = (struct lov_user_md *)arg;
778 struct lov_user_mds_data *lmdp;
779 lmdp = (struct lov_user_mds_data *)arg;
780 lump = &lmdp->lmd_lmm;
782 rc = copy_to_user(lump, lmm, lmmsize);
784 GOTO(out_lmm, rc = -EFAULT);
786 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
787 struct lov_user_mds_data *lmdp;
790 st.st_dev = inode->i_sb->s_dev;
791 st.st_mode = body->mode;
792 st.st_nlink = body->nlink;
793 st.st_uid = body->uid;
794 st.st_gid = body->gid;
795 st.st_rdev = body->rdev;
796 st.st_size = body->size;
797 st.st_blksize = PAGE_SIZE;
798 st.st_blocks = body->blocks;
799 st.st_atime = body->atime;
800 st.st_mtime = body->mtime;
801 st.st_ctime = body->ctime;
802 st.st_ino = inode->i_ino;
804 lmdp = (struct lov_user_mds_data *)arg;
805 rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
807 GOTO(out_lmm, rc = -EFAULT);
812 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
813 OBD_FREE(lmm, lmmsize);
815 ptlrpc_req_finished(request);
821 case IOC_LOV_GETINFO: {
822 struct lov_user_mds_data *lumd;
823 struct lov_stripe_md *lsm;
824 struct lov_user_md *lum;
825 struct lov_mds_md *lmm;
830 lumd = (struct lov_user_mds_data *)arg;
831 lum = &lumd->lmd_lmm;
833 rc = ll_get_max_mdsize(sbi, &lmmsize);
837 OBD_ALLOC(lmm, lmmsize);
838 rc = copy_from_user(lmm, lum, lmmsize);
840 GOTO(free_lmm, rc = -EFAULT);
842 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
844 GOTO(free_lmm, rc = -ENOMEM);
846 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
850 /* Perform glimpse_size operation. */
851 memset(&st, 0, sizeof(st));
853 rc = ll_glimpse_ioctl(sbi, lsm, &st);
857 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
859 GOTO(free_lsm, rc = -EFAULT);
863 obd_free_memmd(sbi->ll_dt_exp, &lsm);
865 OBD_FREE(lmm, lmmsize);
868 case OBD_IOC_LLOG_CATINFO: {
869 struct ptlrpc_request *req = NULL;
872 char *bufs[3] = { NULL }, *str;
873 int lens[3] = { sizeof(struct ptlrpc_body) };
874 int size[2] = { sizeof(struct ptlrpc_body) };
876 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
881 if (!data->ioc_inlbuf1) {
882 obd_ioctl_freedata(buf, len);
886 lens[REQ_REC_OFF] = data->ioc_inllen1;
887 bufs[REQ_REC_OFF] = data->ioc_inlbuf1;
888 if (data->ioc_inllen2) {
889 lens[REQ_REC_OFF + 1] = data->ioc_inllen2;
890 bufs[REQ_REC_OFF + 1] = data->ioc_inlbuf2;
892 lens[REQ_REC_OFF + 1] = 0;
893 bufs[REQ_REC_OFF + 1] = NULL;
896 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
897 LUSTRE_LOG_VERSION, LLOG_CATINFO, 3, lens,
900 GOTO(out_catinfo, rc = -ENOMEM);
902 size[REPLY_REC_OFF] = data->ioc_plen1;
903 ptlrpc_req_set_repsize(req, 2, size);
905 rc = ptlrpc_queue_wait(req);
906 str = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF,
909 rc = copy_to_user(data->ioc_pbuf1, str,data->ioc_plen1);
910 ptlrpc_req_finished(req);
912 obd_ioctl_freedata(buf, len);
915 case OBD_IOC_QUOTACHECK: {
916 struct obd_quotactl *oqctl;
919 if (!capable(CAP_SYS_ADMIN))
922 OBD_ALLOC_PTR(oqctl);
925 oqctl->qc_type = arg;
926 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
928 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
932 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
934 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
939 case OBD_IOC_POLL_QUOTACHECK: {
940 struct if_quotacheck *check;
943 if (!capable(CAP_SYS_ADMIN))
946 OBD_ALLOC_PTR(check);
950 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
953 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
954 if (copy_to_user((void *)arg, check, sizeof(*check)))
959 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
962 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
963 if (copy_to_user((void *)arg, check, sizeof(*check)))
971 #if HAVE_QUOTA_SUPPORT
972 case OBD_IOC_QUOTACTL: {
973 struct if_quotactl *qctl;
974 struct obd_quotactl *oqctl;
976 int cmd, type, id, rc = 0;
982 OBD_ALLOC_PTR(oqctl);
987 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
988 GOTO(out_quotactl, rc = -EFAULT);
991 type = qctl->qc_type;
998 if (!capable(CAP_SYS_ADMIN))
999 GOTO(out_quotactl, rc = -EPERM);
1002 if (((type == USRQUOTA && current->euid != id) ||
1003 (type == GRPQUOTA && !in_egroup_p(id))) &&
1004 !capable(CAP_SYS_ADMIN))
1005 GOTO(out_quotactl, rc = -EPERM);
1007 /* XXX: dqb_valid is borrowed as a flag to mark that
1008 * only mds quota is wanted */
1009 if (qctl->qc_dqblk.dqb_valid)
1010 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
1011 u.cli.cl_target_uuid;
1016 CERROR("unsupported quotactl op: %#x\n", cmd);
1017 GOTO(out_quotactl, -ENOTTY);
1020 QCTL_COPY(oqctl, qctl);
1022 if (qctl->obd_uuid.uuid[0]) {
1023 struct obd_device *obd;
1024 struct obd_uuid *uuid = &qctl->obd_uuid;
1026 obd = class_find_client_notype(uuid,
1027 &sbi->ll_dt_exp->exp_obd->obd_uuid);
1029 GOTO(out_quotactl, rc = -ENOENT);
1031 if (cmd == Q_GETINFO)
1032 oqctl->qc_cmd = Q_GETOINFO;
1033 else if (cmd == Q_GETQUOTA)
1034 oqctl->qc_cmd = Q_GETOQUOTA;
1036 GOTO(out_quotactl, rc = -EINVAL);
1038 if (sbi->ll_md_exp->exp_obd == obd) {
1039 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1042 struct obd_export *exp;
1043 struct lov_obd *lov = &sbi->ll_dt_exp->
1046 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1047 if (!lov->lov_tgts[i] ||
1048 !lov->lov_tgts[i]->ltd_active)
1050 exp = lov->lov_tgts[i]->ltd_exp;
1051 if (exp->exp_obd == obd) {
1052 rc = obd_quotactl(exp, oqctl);
1058 oqctl->qc_cmd = cmd;
1059 QCTL_COPY(qctl, oqctl);
1061 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1064 GOTO(out_quotactl, rc);
1067 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1068 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1069 oqctl->qc_cmd = Q_QUOTAOFF;
1070 obd_quotactl(sbi->ll_md_exp, oqctl);
1073 QCTL_COPY(qctl, oqctl);
1075 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1079 OBD_FREE_PTR(oqctl);
1082 #endif /* HAVE_QUOTA_SUPPORT */
1083 case OBD_IOC_GETNAME: {
1084 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1087 if (copy_to_user((void *)arg, obd->obd_name,
1088 strlen(obd->obd_name) + 1))
1093 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1097 int ll_dir_open(struct inode *inode, struct file *file)
1100 RETURN(ll_file_open(inode, file));
1103 int ll_dir_release(struct inode *inode, struct file *file)
1106 RETURN(ll_file_release(inode, file));
1109 struct file_operations ll_dir_operations = {
1110 .open = ll_dir_open,
1111 .release = ll_dir_release,
1112 .read = generic_read_dir,
1113 .readdir = ll_readdir,
1114 .llseek = ll_llseek,
1115 .ioctl = ll_dir_ioctl