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