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