Whamcloud - gitweb
Branch HEAD
[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 #include <linux/buffer_head.h>   // for wait_on_buffer
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include <obd_support.h>
41 #include <obd_class.h>
42 #include <lustre_lib.h>
43 #include <lustre/lustre_idl.h>
44 #include <lustre_lite.h>
45 #include <lustre_dlm.h>
46 #include <lustre_fid.h>
47 #include "llite_internal.h"
48
49 #ifdef HAVE_PG_FS_MISC
50 #define PageChecked(page)        test_bit(PG_fs_misc, &(page)->flags)
51 #define SetPageChecked(page)     set_bit(PG_fs_misc, &(page)->flags)
52 #endif
53
54 /*
55  * (new) readdir implementation overview.
56  *
57  * Original lustre readdir implementation cached exact copy of raw directory
58  * pages on the client. These pages were indexed in client page cache by
59  * logical offset in the directory file. This design, while very simple and
60  * intuitive had some inherent problems:
61  *
62  *     . it implies that byte offset to the directory entry serves as a
63  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
64  *     ext3/htree directory entries may move due to splits, and more
65  *     importantly,
66  *
67  *     . it is incompatible with the design of split directories for cmd3,
68  *     that assumes that names are distributed across nodes based on their
69  *     hash, and so readdir should be done in hash order.
70  *
71  * New readdir implementation does readdir in hash order, and uses hash of a
72  * file name as a telldir/seekdir cookie. This led to number of complications:
73  *
74  *     . hash is not unique, so it cannot be used to index cached directory
75  *     pages on the client (note, that it requires a whole pageful of hash
76  *     collided entries to cause two pages to have identical hashes);
77  *
78  *     . hash is not unique, so it cannot, strictly speaking, be used as an
79  *     entry cookie. ext3/htree has the same problem and lustre implementation
80  *     mimics their solution: seekdir(hash) positions directory at the first
81  *     entry with the given hash.
82  *
83  * Client side.
84  *
85  * 0. caching
86  *
87  * Client caches directory pages using hash of the first entry as an index. As
88  * noted above hash is not unique, so this solution doesn't work as is:
89  * special processing is needed for "page hash chains" (i.e., sequences of
90  * pages filled with entries all having the same hash value).
91  *
92  * First, such chains have to be detected. To this end, server returns to the
93  * client the hash of the first entry on the page next to one returned. When
94  * client detects that this hash is the same as hash of the first entry on the
95  * returned page, page hash collision has to be handled. Pages in the
96  * hash chain, except first one, are termed "overflow pages".
97  *
98  * Solution to index uniqueness problem is to not cache overflow
99  * pages. Instead, when page hash collision is detected, all overflow pages
100  * from emerging chain are immediately requested from the server and placed in
101  * a special data structure (struct ll_dir_chain). This data structure is used
102  * by ll_readdir() to process entries from overflow pages. When readdir
103  * invocation finishes, overflow pages are discarded. If page hash collision
104  * chain weren't completely processed, next call to readdir will again detect
105  * page hash collision, again read overflow pages in, process next portion of
106  * entries and again discard the pages. This is not as wasteful as it looks,
107  * because, given reasonable hash, page hash collisions are extremely rare.
108  *
109  * 1. directory positioning
110  *
111  * When seekdir(hash) is called, original
112  *
113  *
114  *
115  *
116  *
117  *
118  *
119  *
120  * Server.
121  *
122  * identification of and access to overflow pages
123  *
124  * page format
125  *
126  *
127  *
128  *
129  *
130  */
131
132 #ifdef HAVE_PG_FS_MISC
133 #define PageChecked(page)        test_bit(PG_fs_misc, &(page)->flags)
134 #define SetPageChecked(page)     set_bit(PG_fs_misc, &(page)->flags)
135 #endif
136
137 /* returns the page unlocked, but with a reference */
138 static int ll_dir_readpage(struct file *file, struct page *page)
139 {
140         struct inode *inode = page->mapping->host;
141         struct ptlrpc_request *request;
142         struct mdt_body *body;
143         struct obd_capa *oc;
144         __u64 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         oc = ll_mdscapa_get(inode);
153         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
154                          oc, hash, page, &request);
155         capa_put(oc);
156         if (!rc) {
157                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
158                                       sizeof(*body));
159                 /* Checked by mdc_readpage() */
160                 LASSERT(body != NULL);
161
162                 /* Swabbed by mdc_readpage() */
163                 LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
164
165                 if (body->valid & OBD_MD_FLSIZE) {
166                         ll_inode_size_lock(inode, 0);
167                         i_size_write(inode, body->size);
168                         ll_inode_size_unlock(inode, 0);
169                 }
170                 SetPageUptodate(page);
171         }
172         ptlrpc_req_finished(request);
173
174         unlock_page(page);
175         EXIT;
176         return rc;
177 }
178
179 struct address_space_operations ll_dir_aops = {
180         .readpage  = ll_dir_readpage,
181 };
182
183 static inline unsigned long dir_pages(struct inode *inode)
184 {
185         return (i_size_read(inode) + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
186 }
187
188 static inline unsigned ll_chunk_size(struct inode *inode)
189 {
190         return inode->i_sb->s_blocksize;
191 }
192
193 static void ll_check_page(struct inode *dir, struct page *page)
194 {
195         /* XXX: check page format later */
196         SetPageChecked(page);
197 }
198
199 static inline void ll_put_page(struct page *page)
200 {
201         kunmap(page);
202         page_cache_release(page);
203 }
204
205 /*
206  * Find, kmap and return page that contains given hash.
207  */
208 static struct page *ll_dir_page_locate(struct inode *dir, unsigned long hash,
209                                        __u32 *start, __u32 *end)
210 {
211         struct address_space *mapping = dir->i_mapping;
212         /*
213          * Complement of hash is used as an index so that
214          * radix_tree_gang_lookup() can be used to find a page with starting
215          * hash _smaller_ than one we are looking for.
216          */
217         unsigned long offset = hash_x_index(hash);
218         struct page *page;
219         int found;
220
221         TREE_READ_LOCK_IRQ(mapping);
222         found = radix_tree_gang_lookup(&mapping->page_tree,
223                                        (void **)&page, offset, 1);
224         if (found > 0) {
225                 struct lu_dirpage *dp;
226
227                 page_cache_get(page);
228                 TREE_READ_UNLOCK_IRQ(mapping);
229                 /*
230                  * In contrast to find_lock_page() we are sure that directory
231                  * page cannot be truncated (while DLM lock is held) and,
232                  * hence, can avoid restart.
233                  *
234                  * In fact, page cannot be locked here at all, because
235                  * ll_dir_readpage() does synchronous io.
236                  */
237                 wait_on_page(page);
238                 if (PageUptodate(page)) {
239                         dp = kmap(page);
240                         *start = le32_to_cpu(dp->ldp_hash_start);
241                         *end   = le32_to_cpu(dp->ldp_hash_end);
242                         LASSERT(*start <= hash);
243                         if (hash > *end || (*end != *start && hash == *end)) {
244                                 kunmap(page);
245                                 lock_page(page);
246                                 ll_truncate_complete_page(page);
247                                 unlock_page(page);
248                                 page_cache_release(page);
249                                 page = NULL;
250                         }
251                 } else {
252                         page_cache_release(page);
253                         page = ERR_PTR(-EIO);
254                 }
255
256         } else {
257                 TREE_READ_UNLOCK_IRQ(mapping);
258                 page = NULL;
259         }
260         return page;
261 }
262
263 static struct page *ll_get_dir_page(struct inode *dir, __u32 hash, int exact,
264                                     struct ll_dir_chain *chain)
265 {
266         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
267         struct address_space *mapping = dir->i_mapping;
268         struct lustre_handle lockh;
269         struct lu_dirpage *dp;
270         struct page *page;
271         ldlm_mode_t mode;
272         int rc;
273         __u32 start;
274         __u32 end;
275
276         mode = LCK_PR;
277         rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
278                            ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
279         if (!rc) {
280                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
281                        ll_md_blocking_ast, ldlm_completion_ast, NULL, dir };
282                 struct lookup_intent it = { .it_op = IT_READDIR };
283                 struct ptlrpc_request *request;
284                 struct md_op_data *op_data;
285
286                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0, 
287                                              LUSTRE_OPC_ANY, NULL);
288                 if (IS_ERR(op_data))
289                         return (void *)op_data;
290
291                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
292                                 op_data, &lockh, NULL, 0, 0);
293
294                 ll_finish_md_op_data(op_data);
295
296                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
297                 if (request)
298                         ptlrpc_req_finished(request);
299                 if (rc < 0) {
300                         CERROR("lock enqueue: rc: %d\n", rc);
301                         return ERR_PTR(rc);
302                 }
303         } else {
304                 /* for cross-ref object, l_ast_data of the lock may not be set,
305                  * we reset it here */
306                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie, dir);
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 %p: %#lx != %#lx\n", page,
337                               (unsigned long)hash, (unsigned long)start);
338                         lock_page(page);
339                         ll_truncate_complete_page(page);
340                         unlock_page(page);
341                         page_cache_release(page);
342                 } else
343                         GOTO(hash_collision, page);
344         }
345
346         page = read_cache_page(mapping, hash_x_index(hash),
347                                (filler_t*)mapping->a_ops->readpage, NULL);
348         if (IS_ERR(page))
349                 GOTO(out_unlock, page);
350
351         wait_on_page(page);
352         (void)kmap(page);
353         if (!PageUptodate(page))
354                 goto fail;
355         if (!PageChecked(page))
356                 ll_check_page(dir, page);
357         if (PageError(page))
358                 goto fail;
359 hash_collision:
360         dp = page_address(page);
361
362         start = le32_to_cpu(dp->ldp_hash_start);
363         end   = le32_to_cpu(dp->ldp_hash_end);
364         if (end == start) {
365                 LASSERT(start == hash);
366                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
367                 /*
368                  * Fetch whole overflow chain...
369                  *
370                  * XXX not yet.
371                  */
372                 goto fail;
373         }
374 out_unlock:
375         ldlm_lock_decref(&lockh, mode);
376         return page;
377
378 fail:
379         ll_put_page(page);
380         page = ERR_PTR(-EIO);
381         goto out_unlock;
382 }
383
384 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
385 {
386         struct inode         *inode = filp->f_dentry->d_inode;
387         struct ll_inode_info *info  = ll_i2info(inode);
388         struct ll_sb_info    *sbi   = ll_i2sbi(inode);
389         __u32                 pos   = filp->f_pos;
390         struct page          *page;
391         struct ll_dir_chain   chain;
392         int rc;
393         int done;
394         int shift;
395         ENTRY;
396
397         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu\n",
398                inode->i_ino, inode->i_generation, inode,
399                (unsigned long)pos, i_size_read(inode));
400
401         if (pos == DIR_END_OFF)
402                 /*
403                  * end-of-file.
404                  */
405                 RETURN(0);
406
407         rc    = 0;
408         done  = 0;
409         shift = 0;
410         ll_dir_chain_init(&chain);
411
412         page = ll_get_dir_page(inode, pos, 0, &chain);
413
414         while (rc == 0 && !done) {
415                 struct lu_dirpage *dp;
416                 struct lu_dirent  *ent;
417
418                 if (!IS_ERR(page)) {
419                         /* 
420                          * If page is empty (end of directoryis reached),
421                          * use this value. 
422                          */
423                         __u32 hash = DIR_END_OFF;
424                         __u32 next;
425
426                         dp = page_address(page);
427                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
428                              ent = lu_dirent_next(ent)) {
429                                 char          *name;
430                                 int            namelen;
431                                 struct lu_fid  fid;
432                                 ino_t          ino;
433
434                                 /*
435                                  * XXX: implement correct swabbing here.
436                                  */
437
438                                 hash    = le32_to_cpu(ent->lde_hash);
439                                 namelen = le16_to_cpu(ent->lde_namelen);
440
441                                 if (hash < pos)
442                                         /*
443                                          * Skip until we find target hash
444                                          * value.
445                                          */
446                                         continue;
447
448                                 if (namelen == 0)
449                                         /*
450                                          * Skip dummy record.
451                                          */
452                                         continue;
453
454                                 fid  = ent->lde_fid;
455                                 name = ent->lde_name;
456                                 fid_le_to_cpu(&fid, &fid);
457                                 ino  = ll_fid_build_ino(sbi, &fid);
458
459                                 done = filldir(cookie, name, namelen,
460                                                (loff_t)hash, ino, DT_UNKNOWN);
461                         }
462                         next = le32_to_cpu(dp->ldp_hash_end);
463                         ll_put_page(page);
464                         if (!done) {
465                                 pos = next;
466                                 if (pos == DIR_END_OFF)
467                                         /*
468                                          * End of directory reached.
469                                          */
470                                         done = 1;
471                                 else if (1 /* chain is exhausted*/)
472                                         /*
473                                          * Normal case: continue to the next
474                                          * page.
475                                          */
476                                         page = ll_get_dir_page(inode, pos, 1,
477                                                                &chain);
478                                 else {
479                                         /*
480                                          * go into overflow page.
481                                          */
482                                 }
483                         } else
484                                 pos = hash;
485                 } else {
486                         rc = PTR_ERR(page);
487                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
488                                PFID(&info->lli_fid), (unsigned long)pos, rc);
489                 }
490         }
491
492         filp->f_pos = (loff_t)(__s32)pos;
493         filp->f_version = inode->i_version;
494         touch_atime(filp->f_vfsmnt, filp->f_dentry);
495
496         ll_dir_chain_fini(&chain);
497
498         RETURN(rc);
499 }
500
501 #define QCTL_COPY(out, in)              \
502 do {                                    \
503         Q_COPY(out, in, qc_cmd);        \
504         Q_COPY(out, in, qc_type);       \
505         Q_COPY(out, in, qc_id);         \
506         Q_COPY(out, in, qc_stat);       \
507         Q_COPY(out, in, qc_dqinfo);     \
508         Q_COPY(out, in, qc_dqblk);      \
509 } while (0)
510
511 int ll_send_mgc_param(struct obd_export *mgc, char *string)
512 {
513         struct mgs_send_param *msp;
514         int rc = 0;
515
516         OBD_ALLOC_PTR(msp);
517         if (!msp)
518                 return -ENOMEM;
519
520         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
521         rc = obd_set_info_async(mgc, strlen(KEY_SET_INFO), KEY_SET_INFO,
522                                 sizeof(struct mgs_send_param), msp, NULL);
523         if (rc)
524                 CERROR("Failed to set parameter: %d\n", rc);
525         OBD_FREE_PTR(msp);
526
527         return rc;
528 }
529
530 char *ll_get_fsname(struct inode *inode)
531 {
532         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
533         char *ptr, *fsname;
534         int len;
535
536         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
537         len = strlen(lsi->lsi_lmd->lmd_profile);
538         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
539         if (ptr && (strcmp(ptr, "-client") == 0))
540                 len -= 7;
541         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
542         fsname[len] = '\0';
543
544         return fsname;
545 }
546
547 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
548                      int set_default)
549 {
550         struct ll_sb_info *sbi = ll_i2sbi(inode);
551         struct md_op_data *op_data;
552         struct ptlrpc_request *req = NULL;
553         int rc = 0;
554         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
555         struct obd_device *mgc = lsi->lsi_mgc;
556         char *fsname = NULL, *param = NULL;
557
558         /*
559          * This is coming from userspace, so should be in
560          * local endian.  But the MDS would like it in little
561          * endian, so we swab it before we send it.
562          */
563         if (lump->lmm_magic != LOV_USER_MAGIC)
564                 RETURN(-EINVAL);
565
566         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
567                 lustre_swab_lov_user_md(lump);
568
569         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
570                                      LUSTRE_OPC_ANY, NULL);
571         if (IS_ERR(op_data))
572                 RETURN(PTR_ERR(op_data));
573
574         /* swabbing is done in lov_setstripe() on server side */
575         rc = md_setattr(sbi->ll_md_exp, op_data, lump, sizeof(*lump),
576                         NULL, 0, &req, NULL);
577         ll_finish_md_op_data(op_data);
578         ptlrpc_req_finished(req);
579         if (rc) {
580                 if (rc != -EPERM && rc != -EACCES)
581                         CERROR("mdc_setattr fails: rc = %d\n", rc);
582         }
583
584         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
585                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
586
587                 /* Get fsname and assume devname to be -MDT0000. */
588                 fsname = ll_get_fsname(inode);
589                 /* Set root stripesize */
590                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
591                         lump->lmm_stripe_size);
592                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
593                 if (rc)
594                         goto end;
595
596                 /* Set root stripecount */
597                 sprintf(param, "%s-MDT0000.lov.stripecount=%u", fsname,
598                         lump->lmm_stripe_count);
599                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
600                 if (rc)
601                         goto end;
602
603                 /* Set root stripeoffset */
604                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%u", fsname,
605                         lump->lmm_stripe_offset);
606                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
607                 if (rc)
608                         goto end;
609 end:
610                 if (fsname)
611                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
612                 if (param)
613                         OBD_FREE(param, MGS_PARAM_MAXLEN);
614         }
615         return rc;
616 }
617
618 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp, 
619                      int *lmm_size, struct ptlrpc_request **request) 
620 {
621         struct ll_sb_info *sbi = ll_i2sbi(inode);
622         struct mdt_body   *body;
623         struct lov_mds_md *lmm = NULL;
624         struct ptlrpc_request *req = NULL;
625         int rc, lmmsize;
626         struct obd_capa *oc;
627         
628         rc = ll_get_max_mdsize(sbi, &lmmsize);
629         if (rc)
630                 RETURN(rc);
631
632         oc = ll_mdscapa_get(inode);
633         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
634                         oc, OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
635                         lmmsize, &req);
636         capa_put(oc);
637         if (rc < 0) {
638                 CDEBUG(D_INFO, "md_getattr failed on inode "
639                        "%lu/%u: rc %d\n", inode->i_ino,
640                        inode->i_generation, rc);
641                 GOTO(out, rc);
642         }
643
644         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
645         LASSERT(body != NULL); /* checked by md_getattr_name */
646         /* swabbed by mdc_getattr_name */
647         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF));
648
649         lmmsize = body->eadatasize;
650
651         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
652             lmmsize == 0) {
653                 GOTO(out, rc = -ENODATA);
654         }
655
656         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, lmmsize);
657         LASSERT(lmm != NULL);
658         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF + 1));
659
660         /*
661          * This is coming from the MDS, so is probably in
662          * little endian.  We convert it to host endian before
663          * passing it to userspace.
664          */
665         if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
666                 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
667                 lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
668         }
669 out:
670         *lmmp = lmm;
671         *lmm_size = lmmsize;
672         *request = req;
673         return rc;
674 }
675
676 static int ll_dir_ioctl(struct inode *inode, struct file *file,
677                         unsigned int cmd, unsigned long arg)
678 {
679         struct ll_sb_info *sbi = ll_i2sbi(inode);
680         struct obd_ioctl_data *data;
681         ENTRY;
682
683         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
684                inode->i_ino, inode->i_generation, inode, cmd);
685
686         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
687         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
688                 return -ENOTTY;
689
690         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
691         switch(cmd) {
692         case EXT3_IOC_GETFLAGS:
693         case EXT3_IOC_SETFLAGS:
694                 RETURN(ll_iocontrol(inode, file, cmd, arg));
695         case EXT3_IOC_GETVERSION_OLD:
696         case EXT3_IOC_GETVERSION:
697                 RETURN(put_user(inode->i_generation, (int *)arg));
698         /* We need to special case any other ioctls we want to handle,
699          * to send them to the MDS/OST as appropriate and to properly
700          * network encode the arg field.
701         case EXT3_IOC_SETVERSION_OLD:
702         case EXT3_IOC_SETVERSION:
703         */
704         case IOC_MDC_LOOKUP: {
705                 struct ptlrpc_request *request = NULL;
706                 int namelen, rc, len = 0;
707                 char *buf = NULL;
708                 char *filename;
709                 struct obd_capa *oc;
710
711                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
712                 if (rc)
713                         RETURN(rc);
714                 data = (void *)buf;
715
716                 filename = data->ioc_inlbuf1;
717                 namelen = data->ioc_inllen1;
718
719                 if (namelen < 1) {
720                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
721                         GOTO(out, rc = -EINVAL);
722                 }
723
724                 oc = ll_mdscapa_get(inode);
725                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode), oc,
726                                      filename, namelen, OBD_MD_FLID, 0,
727                                      &request);
728                 capa_put(oc);
729                 if (rc < 0) {
730                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
731                         GOTO(out, rc);
732                 }
733
734                 ptlrpc_req_finished(request);
735
736                 EXIT;
737         out:
738                 obd_ioctl_freedata(buf, len);
739                 return rc;
740         }
741         case LL_IOC_LOV_SETSTRIPE: {
742                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
743                 int rc = 0;
744                 int set_default = 0;
745
746                 LASSERT(sizeof(lum) == sizeof(*lump));
747                 LASSERT(sizeof(lum.lmm_objects[0]) ==
748                         sizeof(lump->lmm_objects[0]));
749                 rc = copy_from_user(&lum, lump, sizeof(lum));
750                 if (rc)
751                         RETURN(-EFAULT);
752
753                 if (inode->i_sb->s_root == file->f_dentry)
754                         set_default = 1;
755
756                 rc = ll_dir_setstripe(inode, &lum, set_default);
757
758                 RETURN(rc);
759         }
760         case LL_IOC_OBD_STATFS:
761                 RETURN(ll_obd_statfs(inode, (void *)arg));
762         case LL_IOC_LOV_GETSTRIPE:
763         case LL_IOC_MDC_GETINFO:
764         case IOC_MDC_GETFILEINFO:
765         case IOC_MDC_GETFILESTRIPE: {
766                 struct ptlrpc_request *request = NULL;
767                 struct lov_user_md *lump;
768                 struct lov_mds_md *lmm = NULL;
769                 struct mdt_body *body;
770                 char *filename = NULL;
771                 int rc, lmmsize;
772
773                 if (cmd == IOC_MDC_GETFILEINFO ||
774                     cmd == IOC_MDC_GETFILESTRIPE) {
775                         filename = getname((const char *)arg);
776                         if (IS_ERR(filename))
777                                 RETURN(PTR_ERR(filename));
778
779                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm, 
780                                                       &lmmsize, &request);
781                 } else {
782                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
783                 }
784
785                 if (request) {
786                         body = lustre_msg_buf(request->rq_repmsg,
787                                               REPLY_REC_OFF, sizeof(*body));
788                         LASSERT(body != NULL); /* checked by md_getattr_name */
789                         /* swabbed by md_getattr_name */
790                         LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
791                 } else {
792                         GOTO(out_req, rc);
793                 }
794
795                 if (rc < 0) {
796                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO || 
797                                                cmd == LL_IOC_MDC_GETINFO))
798                                 GOTO(skip_lmm, rc = 0);
799                         else
800                                 GOTO(out_req, rc);
801                 }
802
803                 if (cmd == IOC_MDC_GETFILESTRIPE ||
804                     cmd == LL_IOC_LOV_GETSTRIPE) {
805                         lump = (struct lov_user_md *)arg;
806                 } else {
807                         struct lov_user_mds_data *lmdp;
808                         lmdp = (struct lov_user_mds_data *)arg;
809                         lump = &lmdp->lmd_lmm;
810                 }
811                 rc = copy_to_user(lump, lmm, lmmsize);
812                 if (rc)
813                         GOTO(out_lmm, rc = -EFAULT);
814         skip_lmm:
815                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
816                         struct lov_user_mds_data *lmdp;
817                         lstat_t st = { 0 };
818
819                         st.st_dev     = inode->i_sb->s_dev;
820                         st.st_mode    = body->mode;
821                         st.st_nlink   = body->nlink;
822                         st.st_uid     = body->uid;
823                         st.st_gid     = body->gid;
824                         st.st_rdev    = body->rdev;
825                         st.st_size    = body->size;
826                         st.st_blksize = CFS_PAGE_SIZE;
827                         st.st_blocks  = body->blocks;
828                         st.st_atime   = body->atime;
829                         st.st_mtime   = body->mtime;
830                         st.st_ctime   = body->ctime;
831                         st.st_ino     = inode->i_ino;
832
833                         lmdp = (struct lov_user_mds_data *)arg;
834                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
835                         if (rc)
836                                 GOTO(out_lmm, rc = -EFAULT);
837                 }
838
839                 EXIT;
840         out_lmm:
841                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
842                         OBD_FREE(lmm, lmmsize);
843         out_req:
844                 ptlrpc_req_finished(request);
845                 if (filename)
846                         putname(filename);
847                 return rc;
848         }
849         case IOC_LOV_GETINFO: {
850                 struct lov_user_mds_data *lumd;
851                 struct lov_stripe_md *lsm;
852                 struct lov_user_md *lum;
853                 struct lov_mds_md *lmm;
854                 int lmmsize;
855                 lstat_t st;
856                 int rc;
857
858                 lumd = (struct lov_user_mds_data *)arg;
859                 lum = &lumd->lmd_lmm;
860
861                 rc = ll_get_max_mdsize(sbi, &lmmsize);
862                 if (rc)
863                         RETURN(rc);
864
865                 OBD_ALLOC(lmm, lmmsize);
866                 rc = copy_from_user(lmm, lum, lmmsize);
867                 if (rc)
868                         GOTO(free_lmm, rc = -EFAULT);
869
870                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
871                 if (rc < 0)
872                         GOTO(free_lmm, rc = -ENOMEM);
873
874                 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
875                 if (rc)
876                         GOTO(free_lsm, rc);
877
878                 /* Perform glimpse_size operation. */
879                 memset(&st, 0, sizeof(st));
880
881                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
882                 if (rc)
883                         GOTO(free_lsm, rc);
884
885                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
886                 if (rc)
887                         GOTO(free_lsm, rc = -EFAULT);
888
889                 EXIT;
890         free_lsm:
891                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
892         free_lmm:
893                 OBD_FREE(lmm, lmmsize);
894                 return rc;
895         }
896         case OBD_IOC_LLOG_CATINFO: {
897                 struct ptlrpc_request *req = NULL;
898                 char *buf = NULL;
899                 int rc, len = 0;
900                 char *bufs[3] = { NULL }, *str;
901                 int lens[3] = { sizeof(struct ptlrpc_body) };
902                 int size[2] = { sizeof(struct ptlrpc_body) };
903
904                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
905                 if (rc)
906                         RETURN(rc);
907                 data = (void *)buf;
908
909                 if (!data->ioc_inlbuf1) {
910                         obd_ioctl_freedata(buf, len);
911                         RETURN(-EINVAL);
912                 }
913
914                 lens[REQ_REC_OFF] = data->ioc_inllen1;
915                 bufs[REQ_REC_OFF] = data->ioc_inlbuf1;
916                 if (data->ioc_inllen2) {
917                         lens[REQ_REC_OFF + 1] = data->ioc_inllen2;
918                         bufs[REQ_REC_OFF + 1] = data->ioc_inlbuf2;
919                 } else {
920                         lens[REQ_REC_OFF + 1] = 0;
921                         bufs[REQ_REC_OFF + 1] = NULL;
922                 }
923
924                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
925                                       LUSTRE_LOG_VERSION, LLOG_CATINFO, 3, lens,
926                                       bufs);
927                 if (!req)
928                         GOTO(out_catinfo, rc = -ENOMEM);
929
930                 size[REPLY_REC_OFF] = data->ioc_plen1;
931                 ptlrpc_req_set_repsize(req, 2, size);
932
933                 rc = ptlrpc_queue_wait(req);
934                 if (!rc) {
935                         str = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF,
936                                                 data->ioc_plen1);
937                         rc = copy_to_user(data->ioc_pbuf1, str, data->ioc_plen1);
938                 }
939                 ptlrpc_req_finished(req);
940         out_catinfo:
941                 obd_ioctl_freedata(buf, len);
942                 RETURN(rc);
943         }
944         case OBD_IOC_QUOTACHECK: {
945                 struct obd_quotactl *oqctl;
946                 int rc, error = 0;
947
948                 if (!capable(CAP_SYS_ADMIN))
949                         RETURN(-EPERM);
950
951                 OBD_ALLOC_PTR(oqctl);
952                 if (!oqctl)
953                         RETURN(-ENOMEM);
954                 oqctl->qc_type = arg;
955                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
956                 if (rc < 0) {
957                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
958                         error = rc;
959                 }
960
961                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
962                 if (rc < 0)
963                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
964
965                 OBD_FREE_PTR(oqctl);
966                 return error ?: rc;
967         }
968         case OBD_IOC_POLL_QUOTACHECK: {
969                 struct if_quotacheck *check;
970                 int rc;
971
972                 if (!capable(CAP_SYS_ADMIN))
973                         RETURN(-EPERM);
974
975                 OBD_ALLOC_PTR(check);
976                 if (!check)
977                         RETURN(-ENOMEM);
978
979                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
980                                    NULL);
981                 if (rc) {
982                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
983                         if (copy_to_user((void *)arg, check, sizeof(*check)))
984                                 rc = -EFAULT;
985                         GOTO(out_poll, rc);
986                 }
987
988                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
989                                    NULL);
990                 if (rc) {
991                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
992                         if (copy_to_user((void *)arg, check, sizeof(*check)))
993                                 rc = -EFAULT;
994                         GOTO(out_poll, rc);
995                 }
996         out_poll:
997                 OBD_FREE_PTR(check);
998                 RETURN(rc);
999         }
1000 #ifdef HAVE_QUOTA_SUPPORT
1001         case OBD_IOC_QUOTACTL: {
1002                 struct if_quotactl *qctl;
1003                 struct obd_quotactl *oqctl;
1004
1005                 int cmd, type, id, rc = 0;
1006
1007                 OBD_ALLOC_PTR(qctl);
1008                 if (!qctl)
1009                         RETURN(-ENOMEM);
1010
1011                 OBD_ALLOC_PTR(oqctl);
1012                 if (!oqctl) {
1013                         OBD_FREE_PTR(qctl);
1014                         RETURN(-ENOMEM);
1015                 }
1016                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1017                         GOTO(out_quotactl, rc = -EFAULT);
1018
1019                 cmd = qctl->qc_cmd;
1020                 type = qctl->qc_type;
1021                 id = qctl->qc_id;
1022                 switch (cmd) {
1023                 case Q_QUOTAON:
1024                 case Q_QUOTAOFF:
1025                 case Q_SETQUOTA:
1026                 case Q_SETINFO:
1027                         if (!capable(CAP_SYS_ADMIN))
1028                                 GOTO(out_quotactl, rc = -EPERM);
1029                         break;
1030                 case Q_GETQUOTA:
1031                         if (((type == USRQUOTA && current->euid != id) ||
1032                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1033                             !capable(CAP_SYS_ADMIN))
1034                                 GOTO(out_quotactl, rc = -EPERM);
1035
1036                         /* XXX: dqb_valid is borrowed as a flag to mark that
1037                          *      only mds quota is wanted */
1038                         if (qctl->qc_dqblk.dqb_valid)
1039                                 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
1040                                                         u.cli.cl_target_uuid;
1041                         break;
1042                 case Q_GETINFO:
1043                         break;
1044                 default:
1045                         CERROR("unsupported quotactl op: %#x\n", cmd);
1046                         GOTO(out_quotactl, rc = -ENOTTY);
1047                 }
1048
1049                 QCTL_COPY(oqctl, qctl);
1050
1051                 if (qctl->obd_uuid.uuid[0]) {
1052                         struct obd_device *obd;
1053                         struct obd_uuid *uuid = &qctl->obd_uuid;
1054
1055                         obd = class_find_client_notype(uuid,
1056                                          &sbi->ll_dt_exp->exp_obd->obd_uuid);
1057                         if (!obd)
1058                                 GOTO(out_quotactl, rc = -ENOENT);
1059
1060                         if (cmd == Q_GETINFO)
1061                                 oqctl->qc_cmd = Q_GETOINFO;
1062                         else if (cmd == Q_GETQUOTA)
1063                                 oqctl->qc_cmd = Q_GETOQUOTA;
1064                         else
1065                                 GOTO(out_quotactl, rc = -EINVAL);
1066
1067                         if (sbi->ll_md_exp->exp_obd == obd) {
1068                                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1069                         } else {
1070                                 int i;
1071                                 struct obd_export *exp;
1072                                 struct lov_obd *lov = &sbi->ll_dt_exp->
1073                                                             exp_obd->u.lov;
1074
1075                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1076                                         if (!lov->lov_tgts[i] ||
1077                                             !lov->lov_tgts[i]->ltd_active)
1078                                                 continue;
1079                                         exp = lov->lov_tgts[i]->ltd_exp;
1080                                         if (exp->exp_obd == obd) {
1081                                                 rc = obd_quotactl(exp, oqctl);
1082                                                 break;
1083                                         }
1084                                 }
1085                         }
1086
1087                         oqctl->qc_cmd = cmd;
1088                         QCTL_COPY(qctl, oqctl);
1089
1090                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1091                                 rc = -EFAULT;
1092
1093                         GOTO(out_quotactl, rc);
1094                 }
1095
1096                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1097                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1098                         oqctl->qc_cmd = Q_QUOTAOFF;
1099                         obd_quotactl(sbi->ll_md_exp, oqctl);
1100                 }
1101
1102                 QCTL_COPY(qctl, oqctl);
1103
1104                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1105                         rc = -EFAULT;
1106         out_quotactl:
1107                 OBD_FREE_PTR(qctl);
1108                 OBD_FREE_PTR(oqctl);
1109                 RETURN(rc);
1110         }
1111 #endif /* HAVE_QUOTA_SUPPORT */
1112         case OBD_IOC_GETNAME: {
1113                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1114                 if (!obd)
1115                         RETURN(-EFAULT);
1116                 if (copy_to_user((void *)arg, obd->obd_name,
1117                                 strlen(obd->obd_name) + 1))
1118                         RETURN (-EFAULT);
1119                 RETURN(0);
1120         }
1121         case LL_IOC_FLUSHCTX:
1122                 RETURN(ll_flush_ctx(inode));
1123         case LL_IOC_GETFACL: {
1124                 struct rmtacl_ioctl_data ioc;
1125
1126                 if (copy_from_user(&ioc, (void *)arg, sizeof(ioc)))
1127                         RETURN(-EFAULT);
1128
1129                 RETURN(ll_ioctl_getfacl(inode, &ioc));
1130         }
1131         case LL_IOC_SETFACL: {
1132                 struct rmtacl_ioctl_data ioc;
1133
1134                 if (copy_from_user(&ioc, (void *)arg, sizeof(ioc)))
1135                         RETURN(-EFAULT);
1136
1137                 RETURN(ll_ioctl_setfacl(inode, &ioc));
1138         }
1139         default:
1140                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1141         }
1142 }
1143
1144 int ll_dir_open(struct inode *inode, struct file *file)
1145 {
1146         ENTRY;
1147         RETURN(ll_file_open(inode, file));
1148 }
1149
1150 int ll_dir_release(struct inode *inode, struct file *file)
1151 {
1152         ENTRY;
1153         RETURN(ll_file_release(inode, file));
1154 }
1155
1156 struct file_operations ll_dir_operations = {
1157         .open     = ll_dir_open,
1158         .release  = ll_dir_release,
1159         .read     = generic_read_dir,
1160         .readdir  = ll_readdir,
1161         .ioctl    = ll_dir_ioctl
1162 };
1163