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