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