Whamcloud - gitweb
b=14230
[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, __u64 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         }
661 out:
662         *lmmp = lmm;
663         *lmm_size = lmmsize;
664         *request = req;
665         return rc;
666 }
667
668 static int ll_dir_ioctl(struct inode *inode, struct file *file,
669                         unsigned int cmd, unsigned long arg)
670 {
671         struct ll_sb_info *sbi = ll_i2sbi(inode);
672         struct obd_ioctl_data *data;
673         ENTRY;
674
675         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
676                inode->i_ino, inode->i_generation, inode, cmd);
677
678         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
679         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
680                 return -ENOTTY;
681
682         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
683         switch(cmd) {
684         case EXT3_IOC_GETFLAGS:
685         case EXT3_IOC_SETFLAGS:
686                 RETURN(ll_iocontrol(inode, file, cmd, arg));
687         case EXT3_IOC_GETVERSION_OLD:
688         case EXT3_IOC_GETVERSION:
689                 RETURN(put_user(inode->i_generation, (int *)arg));
690         /* We need to special case any other ioctls we want to handle,
691          * to send them to the MDS/OST as appropriate and to properly
692          * network encode the arg field.
693         case EXT3_IOC_SETVERSION_OLD:
694         case EXT3_IOC_SETVERSION:
695         */
696         case IOC_MDC_LOOKUP: {
697                 struct ptlrpc_request *request = NULL;
698                 int namelen, rc, len = 0;
699                 char *buf = NULL;
700                 char *filename;
701                 struct obd_capa *oc;
702
703                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
704                 if (rc)
705                         RETURN(rc);
706                 data = (void *)buf;
707
708                 filename = data->ioc_inlbuf1;
709                 namelen = data->ioc_inllen1;
710
711                 if (namelen < 1) {
712                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
713                         GOTO(out, rc = -EINVAL);
714                 }
715
716                 oc = ll_mdscapa_get(inode);
717                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode), oc,
718                                      filename, namelen, OBD_MD_FLID, 0,
719                                      ll_i2suppgid(inode), &request);
720                 capa_put(oc);
721                 if (rc < 0) {
722                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
723                         GOTO(out, rc);
724                 }
725
726                 ptlrpc_req_finished(request);
727
728                 EXIT;
729         out:
730                 obd_ioctl_freedata(buf, len);
731                 return rc;
732         }
733         case LL_IOC_LOV_SETSTRIPE: {
734                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
735                 int rc = 0;
736                 int set_default = 0;
737
738                 LASSERT(sizeof(lum) == sizeof(*lump));
739                 LASSERT(sizeof(lum.lmm_objects[0]) ==
740                         sizeof(lump->lmm_objects[0]));
741                 rc = copy_from_user(&lum, lump, sizeof(lum));
742                 if (rc)
743                         RETURN(-EFAULT);
744
745                 if (inode->i_sb->s_root == file->f_dentry)
746                         set_default = 1;
747
748                 rc = ll_dir_setstripe(inode, &lum, set_default);
749
750                 RETURN(rc);
751         }
752         case LL_IOC_OBD_STATFS:
753                 RETURN(ll_obd_statfs(inode, (void *)arg));
754         case LL_IOC_LOV_GETSTRIPE:
755         case LL_IOC_MDC_GETINFO:
756         case IOC_MDC_GETFILEINFO:
757         case IOC_MDC_GETFILESTRIPE: {
758                 struct ptlrpc_request *request = NULL;
759                 struct lov_user_md *lump;
760                 struct lov_mds_md *lmm = NULL;
761                 struct mdt_body *body;
762                 char *filename = NULL;
763                 int rc, lmmsize;
764
765                 if (cmd == IOC_MDC_GETFILEINFO ||
766                     cmd == IOC_MDC_GETFILESTRIPE) {
767                         filename = getname((const char *)arg);
768                         if (IS_ERR(filename))
769                                 RETURN(PTR_ERR(filename));
770
771                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm, 
772                                                       &lmmsize, &request);
773                 } else {
774                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
775                 }
776
777                 if (request) {
778                         body = req_capsule_server_get(&request->rq_pill,
779                                                       &RMF_MDT_BODY);
780                         LASSERT(body != NULL);
781                 } else {
782                         GOTO(out_req, rc);
783                 }
784
785                 if (rc < 0) {
786                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO || 
787                                                cmd == LL_IOC_MDC_GETINFO))
788                                 GOTO(skip_lmm, rc = 0);
789                         else
790                                 GOTO(out_req, rc);
791                 }
792
793                 if (cmd == IOC_MDC_GETFILESTRIPE ||
794                     cmd == LL_IOC_LOV_GETSTRIPE) {
795                         lump = (struct lov_user_md *)arg;
796                 } else {
797                         struct lov_user_mds_data *lmdp;
798                         lmdp = (struct lov_user_mds_data *)arg;
799                         lump = &lmdp->lmd_lmm;
800                 }
801                 rc = copy_to_user(lump, lmm, lmmsize);
802                 if (rc)
803                         GOTO(out_lmm, rc = -EFAULT);
804         skip_lmm:
805                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
806                         struct lov_user_mds_data *lmdp;
807                         lstat_t st = { 0 };
808
809                         st.st_dev     = inode->i_sb->s_dev;
810                         st.st_mode    = body->mode;
811                         st.st_nlink   = body->nlink;
812                         st.st_uid     = body->uid;
813                         st.st_gid     = body->gid;
814                         st.st_rdev    = body->rdev;
815                         st.st_size    = body->size;
816                         st.st_blksize = CFS_PAGE_SIZE;
817                         st.st_blocks  = body->blocks;
818                         st.st_atime   = body->atime;
819                         st.st_mtime   = body->mtime;
820                         st.st_ctime   = body->ctime;
821                         st.st_ino     = inode->i_ino;
822
823                         lmdp = (struct lov_user_mds_data *)arg;
824                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
825                         if (rc)
826                                 GOTO(out_lmm, rc = -EFAULT);
827                 }
828
829                 EXIT;
830         out_lmm:
831                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
832                         OBD_FREE(lmm, lmmsize);
833         out_req:
834                 ptlrpc_req_finished(request);
835                 if (filename)
836                         putname(filename);
837                 return rc;
838         }
839         case IOC_LOV_GETINFO: {
840                 struct lov_user_mds_data *lumd;
841                 struct lov_stripe_md *lsm;
842                 struct lov_user_md *lum;
843                 struct lov_mds_md *lmm;
844                 int lmmsize;
845                 lstat_t st;
846                 int rc;
847
848                 lumd = (struct lov_user_mds_data *)arg;
849                 lum = &lumd->lmd_lmm;
850
851                 rc = ll_get_max_mdsize(sbi, &lmmsize);
852                 if (rc)
853                         RETURN(rc);
854
855                 OBD_ALLOC(lmm, lmmsize);
856                 rc = copy_from_user(lmm, lum, lmmsize);
857                 if (rc)
858                         GOTO(free_lmm, rc = -EFAULT);
859
860                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
861                 if (rc < 0)
862                         GOTO(free_lmm, rc = -ENOMEM);
863
864                 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
865                 if (rc)
866                         GOTO(free_lsm, rc);
867
868                 /* Perform glimpse_size operation. */
869                 memset(&st, 0, sizeof(st));
870
871                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
872                 if (rc)
873                         GOTO(free_lsm, rc);
874
875                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
876                 if (rc)
877                         GOTO(free_lsm, rc = -EFAULT);
878
879                 EXIT;
880         free_lsm:
881                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
882         free_lmm:
883                 OBD_FREE(lmm, lmmsize);
884                 return rc;
885         }
886         case OBD_IOC_LLOG_CATINFO: {
887                 struct ptlrpc_request *req = NULL;
888                 char                  *buf = NULL;
889                 char                  *str;
890                 int                    len = 0;
891                 int                    rc;
892
893                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
894                 if (rc)
895                         RETURN(rc);
896                 data = (void *)buf;
897
898                 if (!data->ioc_inlbuf1) {
899                         obd_ioctl_freedata(buf, len);
900                         RETURN(-EINVAL);
901                 }
902
903                 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
904                                            &RQF_LLOG_CATINFO);
905                 if (req == NULL)
906                         GOTO(out_catinfo, rc = -ENOMEM);
907
908                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
909                                      data->ioc_inllen1);
910                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
911                                      data->ioc_inllen2);
912
913                 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
914                 if (rc) {
915                         ptlrpc_request_free(req);
916                         GOTO(out_catinfo, rc);
917                 }
918
919                 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
920                 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
921                 if (data->ioc_inllen2) {
922                         str = req_capsule_client_get(&req->rq_pill,
923                                                      &RMF_STRING);
924                         memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
925                 }
926
927                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
928                                      data->ioc_plen1);
929                 ptlrpc_request_set_replen(req);
930
931                 rc = ptlrpc_queue_wait(req);
932                 if (!rc) {
933                         str = req_capsule_server_get(&req->rq_pill,
934                                                      &RMF_STRING);
935                         rc = copy_to_user(data->ioc_pbuf1, str, data->ioc_plen1);
936                 }
937                 ptlrpc_req_finished(req);
938         out_catinfo:
939                 obd_ioctl_freedata(buf, len);
940                 RETURN(rc);
941         }
942         case OBD_IOC_QUOTACHECK: {
943                 struct obd_quotactl *oqctl;
944                 int rc, error = 0;
945
946                 if (!capable(CAP_SYS_ADMIN))
947                         RETURN(-EPERM);
948
949                 OBD_ALLOC_PTR(oqctl);
950                 if (!oqctl)
951                         RETURN(-ENOMEM);
952                 oqctl->qc_type = arg;
953                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
954                 if (rc < 0) {
955                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
956                         error = rc;
957                 }
958
959                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
960                 if (rc < 0)
961                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
962
963                 OBD_FREE_PTR(oqctl);
964                 return error ?: rc;
965         }
966         case OBD_IOC_POLL_QUOTACHECK: {
967                 struct if_quotacheck *check;
968                 int rc;
969
970                 if (!capable(CAP_SYS_ADMIN))
971                         RETURN(-EPERM);
972
973                 OBD_ALLOC_PTR(check);
974                 if (!check)
975                         RETURN(-ENOMEM);
976
977                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
978                                    NULL);
979                 if (rc) {
980                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
981                         if (copy_to_user((void *)arg, check, sizeof(*check)))
982                                 rc = -EFAULT;
983                         GOTO(out_poll, rc);
984                 }
985
986                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
987                                    NULL);
988                 if (rc) {
989                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
990                         if (copy_to_user((void *)arg, check, sizeof(*check)))
991                                 rc = -EFAULT;
992                         GOTO(out_poll, rc);
993                 }
994         out_poll:
995                 OBD_FREE_PTR(check);
996                 RETURN(rc);
997         }
998 #ifdef HAVE_QUOTA_SUPPORT
999         case OBD_IOC_QUOTACTL: {
1000                 struct if_quotactl *qctl;
1001                 struct obd_quotactl *oqctl;
1002
1003                 int cmd, type, id, rc = 0;
1004
1005                 OBD_ALLOC_PTR(qctl);
1006                 if (!qctl)
1007                         RETURN(-ENOMEM);
1008
1009                 OBD_ALLOC_PTR(oqctl);
1010                 if (!oqctl) {
1011                         OBD_FREE_PTR(qctl);
1012                         RETURN(-ENOMEM);
1013                 }
1014                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1015                         GOTO(out_quotactl, rc = -EFAULT);
1016
1017                 cmd = qctl->qc_cmd;
1018                 type = qctl->qc_type;
1019                 id = qctl->qc_id;
1020                 switch (cmd) {
1021                 case Q_QUOTAON:
1022                 case Q_QUOTAOFF:
1023                 case Q_SETQUOTA:
1024                 case Q_SETINFO:
1025                         if (!capable(CAP_SYS_ADMIN))
1026                                 GOTO(out_quotactl, rc = -EPERM);
1027                         break;
1028                 case Q_GETQUOTA:
1029                         if (((type == USRQUOTA && current->euid != id) ||
1030                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1031                             !capable(CAP_SYS_ADMIN))
1032                                 GOTO(out_quotactl, rc = -EPERM);
1033
1034                         /* XXX: dqb_valid is borrowed as a flag to mark that
1035                          *      only mds quota is wanted */
1036                         if (qctl->qc_dqblk.dqb_valid)
1037                                 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
1038                                                         u.cli.cl_target_uuid;
1039                         break;
1040                 case Q_GETINFO:
1041                         break;
1042                 default:
1043                         CERROR("unsupported quotactl op: %#x\n", cmd);
1044                         GOTO(out_quotactl, rc = -ENOTTY);
1045                 }
1046
1047                 QCTL_COPY(oqctl, qctl);
1048
1049                 if (qctl->obd_uuid.uuid[0]) {
1050                         struct obd_device *obd;
1051                         struct obd_uuid *uuid = &qctl->obd_uuid;
1052
1053                         obd = class_find_client_notype(uuid,
1054                                          &sbi->ll_dt_exp->exp_obd->obd_uuid);
1055                         if (!obd)
1056                                 GOTO(out_quotactl, rc = -ENOENT);
1057
1058                         if (cmd == Q_GETINFO)
1059                                 oqctl->qc_cmd = Q_GETOINFO;
1060                         else if (cmd == Q_GETQUOTA)
1061                                 oqctl->qc_cmd = Q_GETOQUOTA;
1062                         else
1063                                 GOTO(out_quotactl, rc = -EINVAL);
1064
1065                         if (sbi->ll_md_exp->exp_obd == obd) {
1066                                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1067                         } else {
1068                                 int i;
1069                                 struct obd_export *exp;
1070                                 struct lov_obd *lov = &sbi->ll_dt_exp->
1071                                                             exp_obd->u.lov;
1072
1073                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1074                                         if (!lov->lov_tgts[i] ||
1075                                             !lov->lov_tgts[i]->ltd_active)
1076                                                 continue;
1077                                         exp = lov->lov_tgts[i]->ltd_exp;
1078                                         if (exp->exp_obd == obd) {
1079                                                 rc = obd_quotactl(exp, oqctl);
1080                                                 break;
1081                                         }
1082                                 }
1083                         }
1084
1085                         oqctl->qc_cmd = cmd;
1086                         QCTL_COPY(qctl, oqctl);
1087
1088                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1089                                 rc = -EFAULT;
1090
1091                         GOTO(out_quotactl, rc);
1092                 }
1093
1094                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1095                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1096                         oqctl->qc_cmd = Q_QUOTAOFF;
1097                         obd_quotactl(sbi->ll_md_exp, oqctl);
1098                 }
1099
1100                 QCTL_COPY(qctl, oqctl);
1101
1102                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1103                         rc = -EFAULT;
1104         out_quotactl:
1105                 OBD_FREE_PTR(qctl);
1106                 OBD_FREE_PTR(oqctl);
1107                 RETURN(rc);
1108         }
1109 #endif /* HAVE_QUOTA_SUPPORT */
1110         case OBD_IOC_GETNAME: {
1111                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1112                 if (!obd)
1113                         RETURN(-EFAULT);
1114                 if (copy_to_user((void *)arg, obd->obd_name,
1115                                 strlen(obd->obd_name) + 1))
1116                         RETURN (-EFAULT);
1117                 RETURN(0);
1118         }
1119         case LL_IOC_FLUSHCTX:
1120                 RETURN(ll_flush_ctx(inode));
1121 #ifdef CONFIG_FS_POSIX_ACL
1122         case LL_IOC_RMTACL: {
1123             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1124                 inode == inode->i_sb->s_root->d_inode) {
1125                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1126                 int rc;
1127
1128                 LASSERT(fd != NULL);
1129                 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1130                 if (!rc)
1131                         fd->fd_flags |= LL_FILE_RMTACL;
1132                 RETURN(rc);
1133             } else
1134                 RETURN(0);
1135         }
1136 #endif
1137         default:
1138                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1139         }
1140 }
1141
1142 int ll_dir_open(struct inode *inode, struct file *file)
1143 {
1144         ENTRY;
1145         RETURN(ll_file_open(inode, file));
1146 }
1147
1148 int ll_dir_release(struct inode *inode, struct file *file)
1149 {
1150         ENTRY;
1151         RETURN(ll_file_release(inode, file));
1152 }
1153
1154 struct file_operations ll_dir_operations = {
1155         .open     = ll_dir_open,
1156         .release  = ll_dir_release,
1157         .read     = generic_read_dir,
1158         .readdir  = ll_readdir,
1159         .ioctl    = ll_dir_ioctl
1160 };
1161