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