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