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