Whamcloud - gitweb
Branch: b_new_cmd
[fs/lustre-release.git] / lustre / llite / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
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)
8  *
9  *  from
10  *
11  *  linux/fs/minix/dir.c
12  *  linux/fs/ext2/dir.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext2 directory handling functions
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *
21  *  All code that works with directory layout had been switched to pagecache
22  *  and moved here. AV
23  *
24  *  Adapted for Lustre Light
25  *  Copyright (C) 2002-2003, Cluster File Systems, Inc.
26  *
27  */
28
29 #include <linux/fs.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
32 #include <linux/mm.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
38 #else
39 # include <linux/buffer_head.h>   // for wait_on_buffer
40 #endif
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
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"
51
52 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
53 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
54
55 /*
56  * (new) readdir implementation overview.
57  *
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:
62  *
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
66  *     importantly,
67  *
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.
71  *
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:
74  *
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);
78  *
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.
83  *
84  * Client side.
85  *
86  * 0. caching
87  *
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).
92  *
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".
98  *
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.
109  *
110  * 1. directory positioning
111  *
112  * When seekdir(hash) is called, original
113  *
114  *
115  *
116  *
117  *
118  *
119  *
120  *
121  * Server.
122  *
123  * identification of and access to overflow pages
124  *
125  * page format
126  *
127  *
128  *
129  *
130  *
131  */
132
133 static __u32 hash_x_index(__u32 value)
134 {
135         return ((__u32)~0) - value;
136 }
137
138 /* returns the page unlocked, but with a reference */
139 static int ll_dir_readpage(struct file *file, struct page *page)
140 {
141         struct inode *inode = page->mapping->host;
142         struct ptlrpc_request *request;
143         struct mdt_body *body;
144         __u32 hash;
145         int rc;
146         ENTRY;
147
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);
151
152         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
153                          hash, page, &request);
154         if (!rc) {
155                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
156                                       sizeof(*body));
157                 LASSERT(body != NULL); /* checked by md_readpage() */
158                 /* swabbed by md_readpage() */
159                 LASSERT_REPSWABBED(request, REPLY_REC_OFF);
160
161                 if (body->valid & OBD_MD_FLSIZE)
162                         inode->i_size = body->size;
163                 SetPageUptodate(page);
164         }
165         ptlrpc_req_finished(request);
166
167         unlock_page(page);
168         EXIT;
169         return rc;
170 }
171
172 struct address_space_operations ll_dir_aops = {
173         .readpage  = ll_dir_readpage,
174 };
175
176 static inline unsigned long dir_pages(struct inode *inode)
177 {
178         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
179 }
180
181 static inline unsigned ll_chunk_size(struct inode *inode)
182 {
183         return inode->i_sb->s_blocksize;
184 }
185
186 static void ll_check_page(struct inode *dir, struct page *page)
187 {
188         /* XXX: check page format later */
189         SetPageChecked(page);
190 }
191
192 static inline void ll_put_page(struct page *page)
193 {
194         kunmap(page);
195         page_cache_release(page);
196 }
197
198 /*
199  * Find, kmap and return page that contains given hash.
200  */
201 static struct page *ll_dir_page_locate(struct inode *dir, unsigned long hash,
202                                        __u32 *start, __u32 *end)
203 {
204         struct address_space *mapping = dir->i_mapping;
205         /*
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.
209          */
210         unsigned long offset = hash_x_index(hash);
211         struct page *page;
212         int found;
213
214         spin_lock_irq(&mapping->tree_lock);
215         found = radix_tree_gang_lookup(&mapping->page_tree,
216                                        (void **)&page, offset, 1);
217         if (found > 0) {
218                 struct lu_dirpage *dp;
219
220                 page_cache_get(page);
221                 spin_unlock_irq(&mapping->tree_lock);
222                 /*
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.
226                  *
227                  * In fact, page cannot be locked here at all, because
228                  * ll_dir_readpage() does synchronous io.
229                  */
230                 wait_on_page(page);
231                 if (PageUptodate(page)) {
232                         dp = kmap(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)) {
237                                 kunmap(page);
238                                 page_cache_release(page);
239                                 page = NULL;
240                         }
241                 } else {
242                         page_cache_release(page);
243                         page = ERR_PTR(-EIO);
244                 }
245
246         } else {
247                 spin_unlock_irq(&mapping->tree_lock);
248                 page = NULL;
249         }
250         return page;
251 }
252
253 /*
254  * Chain of hash overflow pages.
255  */
256 struct ll_dir_chain {
257         /* XXX something. Later */
258 };
259
260 static void ll_dir_chain_init(struct ll_dir_chain *chain)
261 {
262 }
263
264 static void ll_dir_chain_fini(struct ll_dir_chain *chain)
265 {
266 }
267
268 static struct page *ll_get_dir_page(struct inode *dir, __u32 hash, int exact,
269                                     struct ll_dir_chain *chain)
270 {
271         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
272         struct address_space *mapping = dir->i_mapping;
273         struct lustre_handle lockh;
274         struct page *page;
275         struct lu_dirpage *dp;
276         int rc;
277         __u32 start;
278         __u32 end;
279
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);
282         if (!rc) {
283                 struct lookup_intent it = { .it_op = IT_READDIR };
284                 struct ptlrpc_request *request;
285                 struct md_op_data *op_data;
286
287                 OBD_ALLOC_PTR(op_data);
288                 if (op_data == NULL)
289                         return ERR_PTR(-ENOMEM);
290
291                 ll_prepare_md_op_data(op_data, dir, NULL, NULL, 0, 0);
292
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,
296                                 0);
297
298                 OBD_FREE_PTR(op_data);
299
300                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
301                 if (request)
302                         ptlrpc_req_finished(request);
303                 if (rc < 0) {
304                         CERROR("lock enqueue: rc: %d\n", rc);
305                         return ERR_PTR(rc);
306                 }
307         }
308         ldlm_lock_dump_handle(D_OTHER, &lockh);
309
310         page = ll_dir_page_locate(dir, hash, &start, &end);
311         if (IS_ERR(page))
312                 GOTO(out_unlock, page);
313
314         if (page != NULL) {
315                 /*
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?)
324                  *
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.
328                  */
329                 if (exact && hash != start) {
330                         /*
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.
335                          */
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);
340                 } else
341                         GOTO(hash_collision, page);
342         }
343
344         page = read_cache_page(mapping, hash_x_index(hash),
345                                (filler_t*)mapping->a_ops->readpage, NULL);
346         if (IS_ERR(page))
347                 GOTO(out_unlock, page);
348
349         wait_on_page(page);
350         (void)kmap(page);
351         if (!PageUptodate(page))
352                 goto fail;
353         if (!PageChecked(page))
354                 ll_check_page(dir, page);
355         if (PageError(page))
356                 goto fail;
357 hash_collision:
358         dp = page_address(page);
359
360         start = le32_to_cpu(dp->ldp_hash_start);
361         end   = le32_to_cpu(dp->ldp_hash_end);
362         if (end == start) {
363                 LASSERT(start == hash);
364                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
365                 /*
366                  * Fetch whole overflow chain...
367                  *
368                  * XXX not yet.
369                  */
370                 goto fail;
371         }
372 out_unlock:
373         ldlm_lock_decref(&lockh, LCK_CR);
374         return page;
375
376 fail:
377         ll_put_page(page);
378         page = ERR_PTR(-EIO);
379         goto out_unlock;
380 }
381
382 static loff_t ll_llseek(struct file *filp, loff_t off, int whence)
383 {
384         if (off != 0 || whence != 1 /* SEEK_CUR */) {
385                 /*
386                  * Except when telldir() is going on, reset readdir to the
387                  * beginning of hash collision chain.
388                  */
389                 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
390
391                 fd->fd_dir.lfd_dup = 0;
392         }
393         return default_llseek(filp, off, whence);
394 }
395
396 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
397 {
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;
403         struct page          *page;
404         struct ll_dir_chain   chain;
405         __u32 prevhash;
406         int rc;
407         int dup;
408         int done;
409         int shift;
410         ENTRY;
411
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);
415
416         if (pos == ~0)
417                 /*
418                  * end-of-file.
419                  */
420                 RETURN(0);
421
422         rc    = 0;
423         dup   = 0;
424         done  = 0;
425         shift = 0;
426         prevhash = ~0; /* impossible hash value */
427         ll_dir_chain_init(&chain);
428
429         page = ll_get_dir_page(inode, pos, 0, &chain);
430
431         while (rc == 0 && !done) {
432                 struct lu_dirpage *dp;
433                 struct lu_dirent  *ent;
434
435                 if (!IS_ERR(page)) {
436                         __u32 hash; /* no, Richard, it _is_ initialized */
437                         __u32 next;
438
439                         dp = page_address(page);
440                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
441                              ent = lu_dirent_next(ent)) {
442                                 char          *name;
443                                 int            namelen;
444                                 struct lu_fid  fid;
445                                 ino_t          ino;
446
447                                 /*
448                                  * XXX: implement correct swabbing here.
449                                  */
450
451                                 hash    = le32_to_cpu(ent->lde_hash);
452                                 namelen = le16_to_cpu(ent->lde_namelen);
453
454                                 if (hash < pos)
455                                         /*
456                                          * Skip until we find target hash
457                                          * value.
458                                          */
459                                         continue;
460
461                                 if (namelen == 0)
462                                         /*
463                                          * Skip dummy record.
464                                          */
465                                         continue;
466                                 /*
467                                  * Keep track of how far we get into duplicate
468                                  * hash segment.
469                                  */
470                                 if (hash == prevhash)
471                                         dup++;
472                                 prevhash = hash;
473
474                                 if (hash == fd->fd_dir.lfd_duppos &&
475                                     fd->fd_dir.lfd_dup > 0) {
476                                         fd->fd_dir.lfd_dup--;
477                                         continue;
478                                 }
479
480                                 fid  = ent->lde_fid;
481                                 name = ent->lde_name;
482                                 fid_le_to_cpu(&fid);
483                                 ino  = ll_fid_build_ino(sbi, &fid);
484
485                                 done = filldir(cookie, name, namelen,
486                                                hash, ino, DT_UNKNOWN);
487                         }
488                         next = le32_to_cpu(dp->ldp_hash_end);
489                         ll_put_page(page);
490                         if (!done) {
491                                 pos = next;
492                                 if (pos == ~0)
493                                         /*
494                                          * End of directory reached.
495                                          */
496                                         done = 1;
497                                 else if (1 /* chain is exhausted*/)
498                                         /*
499                                          * Normal case: continue to the next
500                                          * page.
501                                          */
502                                         page = ll_get_dir_page(inode, pos, 1,
503                                                                &chain);
504                                 else {
505                                         /*
506                                          * go into overflow page.
507                                          */
508                                 }
509                         } else
510                                 pos = hash;
511                 } else {
512                         rc = PTR_ERR(page);
513                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
514                                PFID(&info->lli_fid), (unsigned long)pos, rc);
515                 }
516         }
517
518         filp->f_pos = pos;
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);
523
524         ll_dir_chain_fini(&chain);
525
526         RETURN(rc);
527 }
528
529 #define QCTL_COPY(out, in)              \
530 do {                                    \
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);      \
537 } while (0)
538
539 static int ll_dir_ioctl(struct inode *inode, struct file *file,
540                         unsigned int cmd, unsigned long arg)
541 {
542         struct ll_sb_info *sbi = ll_i2sbi(inode);
543         struct obd_ioctl_data *data;
544         ENTRY;
545
546         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
547                inode->i_ino, inode->i_generation, inode, cmd);
548
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 */
551                 return -ENOTTY;
552
553         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
554         switch(cmd) {
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:
566         */
567         case IOC_MDC_LOOKUP: {
568                 struct ptlrpc_request *request = NULL;
569                 int namelen, rc, len = 0;
570                 char *buf = NULL;
571                 char *filename;
572
573                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
574                 if (rc)
575                         RETURN(rc);
576                 data = (void *)buf;
577
578                 filename = data->ioc_inlbuf1;
579                 namelen = data->ioc_inllen1;
580
581                 if (namelen < 1) {
582                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
583                         GOTO(out, rc = -EINVAL);
584                 }
585
586                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
587                                      filename, namelen, OBD_MD_FLID, 0,
588                                      &request);
589                 if (rc < 0) {
590                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
591                         GOTO(out, rc);
592                 }
593
594                 ptlrpc_req_finished(request);
595
596                 EXIT;
597         out:
598                 obd_ioctl_freedata(buf, len);
599                 return rc;
600         }
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 };
606                 int rc = 0;
607
608                 OBD_ALLOC_PTR(op_data);
609                 if (op_data == NULL)
610                         RETURN(-ENOMEM);
611
612                 ll_prepare_md_op_data(op_data, inode,
613                                       NULL, NULL, 0, 0);
614
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));
619                 if (rc)
620                         RETURN(-EFAULT);
621
622                 /*
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.
626                  */
627                 if (lum.lmm_magic != LOV_USER_MAGIC)
628                         RETURN(-EINVAL);
629
630                 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
631                         lustre_swab_lov_user_md(&lum);
632
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);
636                 if (rc) {
637                         if (rc != -EPERM && rc != -EACCES)
638                                 CERROR("md_setattr fails: rc = %d\n", rc);
639                 }
640                 OBD_FREE_PTR(op_data);
641                 ptlrpc_req_finished(request);
642                 RETURN(rc);
643         }
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;
655                 int rc, lmmsize;
656
657                 rc = ll_get_max_mdsize(sbi, &lmmsize);
658                 if (rc)
659                         RETURN(rc);
660
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));
666
667                         rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
668                                              filename, strlen(filename) + 1,
669                                              OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
670                                              lmmsize, &request);
671                         if (rc < 0) {
672                                 CDEBUG(D_INFO, "md_getattr_name failed "
673                                        "on %s: rc %d\n", filename, rc);
674                                 GOTO(out_name, rc);
675                         }
676                 } else {
677                         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
678                                         OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
679                                         lmmsize, &request);
680                         if (rc < 0) {
681                                 CDEBUG(D_INFO, "md_getattr failed on inode "
682                                        "%lu/%u: rc %d\n", inode->i_ino,
683                                        inode->i_generation, rc);
684                                 GOTO(out_name, rc);
685                          }
686                 }
687
688                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
689                                       sizeof(*body));
690                 LASSERT(body != NULL); /* checked by md_getattr_name */
691                 /* swabbed by md_getattr_name */
692                 LASSERT_REPSWABBED(request, REPLY_REC_OFF);
693
694                 lmmsize = body->eadatasize;
695
696                 if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
697                     lmmsize == 0) {
698                         if (cmd == LL_IOC_LOV_GETSTRIPE ||
699                             cmd == IOC_MDC_GETFILESTRIPE)
700                                 GOTO(out_req, rc = -ENODATA);
701                         else
702                                 GOTO(skip_lmm, rc = 0);
703                 }
704
705                 lmm = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 1,
706                                      lmmsize);
707                 LASSERT(lmm != NULL);
708                 LASSERT_REPSWABBED(request, REPLY_REC_OFF + 1);
709
710                 /*
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.
714                  */
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);
720                 }
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;
725
726                         rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
727                         if (rc < 0)
728                                 GOTO(out_req, rc = -ENOMEM);
729                         rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
730                         if (rc)
731                                 GOTO(out_free_memmd, rc);
732
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);
737                         if (!lmj)
738                                 GOTO(out_free_memmd, rc = -ENOMEM);
739
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];
744
745                                 if (lex->le_loi_idx + lex->le_stripe_count <= i)
746                                         aindex ++;
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 =
751                                         lex->le_start;
752
753                                 if ((int)lex->le_len == -1)
754                                         lmj->lmm_objects[i].l_extent_end = -1;
755                                 else
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;
766                         }
767                         lmm = (struct lov_mds_md *)lmj;
768                         lmmsize = lmj_size;
769                 out_free_memmd:
770                         obd_free_memmd(sbi->ll_dt_exp, &lsm);
771                         if (rc)
772                                 GOTO(out_req, rc);
773                 }
774                 if (cmd == IOC_MDC_GETFILESTRIPE ||
775                     cmd == LL_IOC_LOV_GETSTRIPE) {
776                         lump = (struct lov_user_md *)arg;
777                 } else {
778                         struct lov_user_mds_data *lmdp;
779                         lmdp = (struct lov_user_mds_data *)arg;
780                         lump = &lmdp->lmd_lmm;
781                 }
782                 rc = copy_to_user(lump, lmm, lmmsize);
783                 if (rc)
784                         GOTO(out_lmm, rc = -EFAULT);
785         skip_lmm:
786                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
787                         struct lov_user_mds_data *lmdp;
788                         lstat_t st = { 0 };
789
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;
803
804                         lmdp = (struct lov_user_mds_data *)arg;
805                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
806                         if (rc)
807                                 GOTO(out_lmm, rc = -EFAULT);
808                 }
809
810                 EXIT;
811         out_lmm:
812                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
813                         OBD_FREE(lmm, lmmsize);
814         out_req:
815                 ptlrpc_req_finished(request);
816         out_name:
817                 if (filename)
818                         putname(filename);
819                 return rc;
820         }
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;
826                 int lmmsize;
827                 lstat_t st;
828                 int rc;
829
830                 lumd = (struct lov_user_mds_data *)arg;
831                 lum = &lumd->lmd_lmm;
832
833                 rc = ll_get_max_mdsize(sbi, &lmmsize);
834                 if (rc)
835                         RETURN(rc);
836
837                 OBD_ALLOC(lmm, lmmsize);
838                 rc = copy_from_user(lmm, lum, lmmsize);
839                 if (rc)
840                         GOTO(free_lmm, rc = -EFAULT);
841
842                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
843                 if (rc < 0)
844                         GOTO(free_lmm, rc = -ENOMEM);
845
846                 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
847                 if (rc)
848                         GOTO(free_lsm, rc);
849
850                 /* Perform glimpse_size operation. */
851                 memset(&st, 0, sizeof(st));
852
853                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
854                 if (rc)
855                         GOTO(free_lsm, rc);
856
857                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
858                 if (rc)
859                         GOTO(free_lsm, rc = -EFAULT);
860
861                 EXIT;
862         free_lsm:
863                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
864         free_lmm:
865                 OBD_FREE(lmm, lmmsize);
866                 return rc;
867         }
868         case OBD_IOC_LLOG_CATINFO: {
869                 struct ptlrpc_request *req = NULL;
870                 char *buf = NULL;
871                 int rc, len = 0;
872                 char *bufs[3] = { NULL }, *str;
873                 int lens[3] = { sizeof(struct ptlrpc_body) };
874                 int size[2] = { sizeof(struct ptlrpc_body) };
875
876                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
877                 if (rc)
878                         RETURN(rc);
879                 data = (void *)buf;
880
881                 if (!data->ioc_inlbuf1) {
882                         obd_ioctl_freedata(buf, len);
883                         RETURN(-EINVAL);
884                 }
885
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;
891                 } else {
892                         lens[REQ_REC_OFF + 1] = 0;
893                         bufs[REQ_REC_OFF + 1] = NULL;
894                 }
895
896                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
897                                       LUSTRE_LOG_VERSION, LLOG_CATINFO, 3, lens,
898                                       bufs);
899                 if (!req)
900                         GOTO(out_catinfo, rc = -ENOMEM);
901
902                 size[REPLY_REC_OFF] = data->ioc_plen1;
903                 ptlrpc_req_set_repsize(req, 2, size);
904
905                 rc = ptlrpc_queue_wait(req);
906                 str = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF,
907                                         data->ioc_plen1);
908                 if (!rc)
909                         rc = copy_to_user(data->ioc_pbuf1, str,data->ioc_plen1);
910                 ptlrpc_req_finished(req);
911         out_catinfo:
912                 obd_ioctl_freedata(buf, len);
913                 RETURN(rc);
914         }
915         case OBD_IOC_QUOTACHECK: {
916                 struct obd_quotactl *oqctl;
917                 int rc, error = 0;
918
919                 if (!capable(CAP_SYS_ADMIN))
920                         RETURN(-EPERM);
921
922                 OBD_ALLOC_PTR(oqctl);
923                 if (!oqctl)
924                         RETURN(-ENOMEM);
925                 oqctl->qc_type = arg;
926                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
927                 if (rc < 0) {
928                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
929                         error = rc;
930                 }
931
932                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
933                 if (rc < 0)
934                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
935
936                 OBD_FREE_PTR(oqctl);
937                 return error ?: rc;
938         }
939         case OBD_IOC_POLL_QUOTACHECK: {
940                 struct if_quotacheck *check;
941                 int rc;
942
943                 if (!capable(CAP_SYS_ADMIN))
944                         RETURN(-EPERM);
945
946                 OBD_ALLOC_PTR(check);
947                 if (!check)
948                         RETURN(-ENOMEM);
949
950                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
951                                    NULL);
952                 if (rc) {
953                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
954                         if (copy_to_user((void *)arg, check, sizeof(*check)))
955                                 rc = -EFAULT;
956                         GOTO(out_poll, rc);
957                 }
958
959                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
960                                    NULL);
961                 if (rc) {
962                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
963                         if (copy_to_user((void *)arg, check, sizeof(*check)))
964                                 rc = -EFAULT;
965                         GOTO(out_poll, rc);
966                 }
967         out_poll:
968                 OBD_FREE_PTR(check);
969                 RETURN(rc);
970         }
971 #if HAVE_QUOTA_SUPPORT
972         case OBD_IOC_QUOTACTL: {
973                 struct if_quotactl *qctl;
974                 struct obd_quotactl *oqctl;
975
976                 int cmd, type, id, rc = 0;
977
978                 OBD_ALLOC_PTR(qctl);
979                 if (!qctl)
980                         RETURN(-ENOMEM);
981
982                 OBD_ALLOC_PTR(oqctl);
983                 if (!oqctl) {
984                         OBD_FREE_PTR(qctl);
985                         RETURN(-ENOMEM);
986                 }
987                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
988                         GOTO(out_quotactl, rc = -EFAULT);
989
990                 cmd = qctl->qc_cmd;
991                 type = qctl->qc_type;
992                 id = qctl->qc_id;
993                 switch (cmd) {
994                 case Q_QUOTAON:
995                 case Q_QUOTAOFF:
996                 case Q_SETQUOTA:
997                 case Q_SETINFO:
998                         if (!capable(CAP_SYS_ADMIN))
999                                 GOTO(out_quotactl, rc = -EPERM);
1000                         break;
1001                 case Q_GETQUOTA:
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);
1006
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;
1012                         break;
1013                 case Q_GETINFO:
1014                         break;
1015                 default:
1016                         CERROR("unsupported quotactl op: %#x\n", cmd);
1017                         GOTO(out_quotactl, -ENOTTY);
1018                 }
1019
1020                 QCTL_COPY(oqctl, qctl);
1021
1022                 if (qctl->obd_uuid.uuid[0]) {
1023                         struct obd_device *obd;
1024                         struct obd_uuid *uuid = &qctl->obd_uuid;
1025
1026                         obd = class_find_client_notype(uuid,
1027                                          &sbi->ll_dt_exp->exp_obd->obd_uuid);
1028                         if (!obd)
1029                                 GOTO(out_quotactl, rc = -ENOENT);
1030
1031                         if (cmd == Q_GETINFO)
1032                                 oqctl->qc_cmd = Q_GETOINFO;
1033                         else if (cmd == Q_GETQUOTA)
1034                                 oqctl->qc_cmd = Q_GETOQUOTA;
1035                         else
1036                                 GOTO(out_quotactl, rc = -EINVAL);
1037
1038                         if (sbi->ll_md_exp->exp_obd == obd) {
1039                                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1040                         } else {
1041                                 int i;
1042                                 struct obd_export *exp;
1043                                 struct lov_obd *lov = &sbi->ll_dt_exp->
1044                                                             exp_obd->u.lov;
1045
1046                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1047                                         if (!lov->lov_tgts[i] ||
1048                                             !lov->lov_tgts[i]->ltd_active)
1049                                                 continue;
1050                                         exp = lov->lov_tgts[i]->ltd_exp;
1051                                         if (exp->exp_obd == obd) {
1052                                                 rc = obd_quotactl(exp, oqctl);
1053                                                 break;
1054                                         }
1055                                 }
1056                         }
1057
1058                         oqctl->qc_cmd = cmd;
1059                         QCTL_COPY(qctl, oqctl);
1060
1061                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1062                                 rc = -EFAULT;
1063
1064                         GOTO(out_quotactl, rc);
1065                 }
1066
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);
1071                 }
1072
1073                 QCTL_COPY(qctl, oqctl);
1074
1075                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1076                         rc = -EFAULT;
1077         out_quotactl:
1078                 OBD_FREE_PTR(qctl);
1079                 OBD_FREE_PTR(oqctl);
1080                 RETURN(rc);
1081         }
1082 #endif /* HAVE_QUOTA_SUPPORT */
1083         case OBD_IOC_GETNAME: {
1084                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1085                 if (!obd)
1086                         RETURN(-EFAULT);
1087                 if (copy_to_user((void *)arg, obd->obd_name,
1088                                 strlen(obd->obd_name) + 1))
1089                         RETURN (-EFAULT);
1090                 RETURN(0);
1091         }
1092         default:
1093                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1094         }
1095 }
1096
1097 int ll_dir_open(struct inode *inode, struct file *file)
1098 {
1099         ENTRY;
1100         RETURN(ll_file_open(inode, file));
1101 }
1102
1103 int ll_dir_release(struct inode *inode, struct file *file)
1104 {
1105         ENTRY;
1106         RETURN(ll_file_release(inode, file));
1107 }
1108
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
1116 };
1117