Whamcloud - gitweb
55fa6a8b403e2bd8e6b5b624682fe53c6f5a47f4
[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, NULL, dir };
285                 struct lookup_intent it = { .it_op = IT_READDIR };
286                 struct ptlrpc_request *request;
287                 struct md_op_data *op_data;
288
289                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
290                                              LUSTRE_OPC_ANY, NULL);
291                 if (IS_ERR(op_data))
292                         return (void *)op_data;
293
294                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
295                                 op_data, &lockh, NULL, 0, NULL, 0);
296
297                 ll_finish_md_op_data(op_data);
298
299                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
300                 if (request)
301                         ptlrpc_req_finished(request);
302                 if (rc < 0) {
303                         CERROR("lock enqueue: rc: %d\n", rc);
304                         return ERR_PTR(rc);
305                 }
306         } else {
307                 /* for cross-ref object, l_ast_data of the lock may not be set,
308                  * we reset it here */
309                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie, dir);
310         }
311         ldlm_lock_dump_handle(D_OTHER, &lockh);
312
313         page = ll_dir_page_locate(dir, hash, &start, &end);
314         if (IS_ERR(page))
315                 GOTO(out_unlock, page);
316
317         if (page != NULL) {
318                 /*
319                  * XXX nikita: not entirely correct handling of a corner case:
320                  * suppose hash chain of entries with hash value HASH crosses
321                  * border between pages P0 and P1. First both P0 and P1 are
322                  * cached, seekdir() is called for some entry from the P0 part
323                  * of the chain. Later P0 goes out of cache. telldir(HASH)
324                  * happens and finds P1, as it starts with matching hash
325                  * value. Remaining entries from P0 part of the chain are
326                  * skipped. (Is that really a bug?)
327                  *
328                  * Possible solutions: 0. don't cache P1 is such case, handle
329                  * it as an "overflow" page. 1. invalidate all pages at
330                  * once. 2. use HASH|1 as an index for P1.
331                  */
332                 if (exact && hash != start) {
333                         /*
334                          * readdir asked for a page starting _exactly_ from
335                          * given hash, but cache contains stale page, with
336                          * entries with smaller hash values. Stale page should
337                          * be invalidated, and new one fetched.
338                          */
339                         CWARN("Stale readpage page %p: "LPX64" != "LPX64"\n",
340                               page, hash, start);
341                         ll_release_page(page, hash, start, end);
342                 } else {
343                         GOTO(hash_collision, page);
344                 }
345         }
346
347         page = read_cache_page(mapping, hash_x_index((__u32)hash),
348                                (filler_t*)mapping->a_ops->readpage, NULL);
349         if (IS_ERR(page))
350                 GOTO(out_unlock, page);
351
352         wait_on_page(page);
353         (void)kmap(page);
354         if (!PageUptodate(page))
355                 goto fail;
356         if (!PageChecked(page))
357                 ll_check_page(dir, page);
358         if (PageError(page))
359                 goto fail;
360 hash_collision:
361         dp = page_address(page);
362
363         start = le64_to_cpu(dp->ldp_hash_start);
364         end   = le64_to_cpu(dp->ldp_hash_end);
365         if (end == start) {
366                 LASSERT(start == hash);
367                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
368                 /*
369                  * Fetch whole overflow chain...
370                  *
371                  * XXX not yet.
372                  */
373                 goto fail;
374         }
375 out_unlock:
376         ldlm_lock_decref(&lockh, mode);
377         return page;
378
379 fail:
380         ll_put_page(page);
381         page = ERR_PTR(-EIO);
382         goto out_unlock;
383 }
384
385 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
386 {
387         struct inode         *inode = filp->f_dentry->d_inode;
388         struct ll_inode_info *info  = ll_i2info(inode);
389         struct ll_sb_info    *sbi   = ll_i2sbi(inode);
390         __u64                 pos   = filp->f_pos;
391         struct page          *page;
392         struct ll_dir_chain   chain;
393         int rc;
394         int done;
395         int shift;
396         ENTRY;
397
398         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu\n",
399                inode->i_ino, inode->i_generation, inode,
400                (unsigned long)pos, i_size_read(inode));
401
402         if (pos == DIR_END_OFF)
403                 /*
404                  * end-of-file.
405                  */
406                 RETURN(0);
407
408         rc    = 0;
409         done  = 0;
410         shift = 0;
411         ll_dir_chain_init(&chain);
412
413         page = ll_get_dir_page(inode, pos, 0, &chain);
414
415         while (rc == 0 && !done) {
416                 struct lu_dirpage *dp;
417                 struct lu_dirent  *ent;
418
419                 if (!IS_ERR(page)) {
420                         /*
421                          * If page is empty (end of directoryis reached),
422                          * use this value.
423                          */
424                         __u64 hash = DIR_END_OFF;
425                         __u64 next;
426
427                         dp = page_address(page);
428                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
429                              ent = lu_dirent_next(ent)) {
430                                 char          *name;
431                                 int            namelen;
432                                 struct lu_fid  fid;
433                                 ino_t          ino;
434
435                                 /*
436                                  * XXX: implement correct swabbing here.
437                                  */
438
439                                 hash    = le64_to_cpu(ent->lde_hash);
440                                 namelen = le16_to_cpu(ent->lde_namelen);
441
442                                 if (hash < pos)
443                                         /*
444                                          * Skip until we find target hash
445                                          * value.
446                                          */
447                                         continue;
448
449                                 if (namelen == 0)
450                                         /*
451                                          * Skip dummy record.
452                                          */
453                                         continue;
454
455                                 fid  = ent->lde_fid;
456                                 name = ent->lde_name;
457                                 fid_le_to_cpu(&fid, &fid);
458                                 ino  = ll_fid_build_ino(sbi, &fid);
459
460                                 done = filldir(cookie, name, namelen,
461                                                (loff_t)hash, ino, DT_UNKNOWN);
462                         }
463                         next = le64_to_cpu(dp->ldp_hash_end);
464                         ll_put_page(page);
465                         if (!done) {
466                                 pos = next;
467                                 if (pos == DIR_END_OFF)
468                                         /*
469                                          * End of directory reached.
470                                          */
471                                         done = 1;
472                                 else if (1 /* chain is exhausted*/)
473                                         /*
474                                          * Normal case: continue to the next
475                                          * page.
476                                          */
477                                         page = ll_get_dir_page(inode, pos, 1,
478                                                                &chain);
479                                 else {
480                                         /*
481                                          * go into overflow page.
482                                          */
483                                 }
484                         } else
485                                 pos = hash;
486                 } else {
487                         rc = PTR_ERR(page);
488                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
489                                PFID(&info->lli_fid), (unsigned long)pos, rc);
490                 }
491         }
492
493         filp->f_pos = (loff_t)(__s32)pos;
494         filp->f_version = inode->i_version;
495         touch_atime(filp->f_vfsmnt, filp->f_dentry);
496
497         ll_dir_chain_fini(&chain);
498
499         RETURN(rc);
500 }
501
502 #define QCTL_COPY(out, in)              \
503 do {                                    \
504         Q_COPY(out, in, qc_cmd);        \
505         Q_COPY(out, in, qc_type);       \
506         Q_COPY(out, in, qc_id);         \
507         Q_COPY(out, in, qc_stat);       \
508         Q_COPY(out, in, qc_dqinfo);     \
509         Q_COPY(out, in, qc_dqblk);      \
510 } while (0)
511
512 int ll_send_mgc_param(struct obd_export *mgc, char *string)
513 {
514         struct mgs_send_param *msp;
515         int rc = 0;
516
517         OBD_ALLOC_PTR(msp);
518         if (!msp)
519                 return -ENOMEM;
520
521         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
522         rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
523                                 sizeof(struct mgs_send_param), msp, NULL);
524         if (rc)
525                 CERROR("Failed to set parameter: %d\n", rc);
526         OBD_FREE_PTR(msp);
527
528         return rc;
529 }
530
531 char *ll_get_fsname(struct inode *inode)
532 {
533         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
534         char *ptr, *fsname;
535         int len;
536
537         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
538         len = strlen(lsi->lsi_lmd->lmd_profile);
539         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
540         if (ptr && (strcmp(ptr, "-client") == 0))
541                 len -= 7;
542         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
543         fsname[len] = '\0';
544
545         return fsname;
546 }
547
548 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
549                      int set_default)
550 {
551         struct ll_sb_info *sbi = ll_i2sbi(inode);
552         struct md_op_data *op_data;
553         struct ptlrpc_request *req = NULL;
554         int rc = 0;
555         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
556         struct obd_device *mgc = lsi->lsi_mgc;
557         char *fsname = NULL, *param = NULL;
558
559         /*
560          * This is coming from userspace, so should be in
561          * local endian.  But the MDS would like it in little
562          * endian, so we swab it before we send it.
563          */
564         if (lump->lmm_magic != LOV_USER_MAGIC)
565                 RETURN(-EINVAL);
566
567         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
568                 lustre_swab_lov_user_md(lump);
569
570         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
571                                      LUSTRE_OPC_ANY, NULL);
572         if (IS_ERR(op_data))
573                 RETURN(PTR_ERR(op_data));
574
575         /* swabbing is done in lov_setstripe() on server side */
576         rc = md_setattr(sbi->ll_md_exp, op_data, lump, sizeof(*lump),
577                         NULL, 0, &req, NULL);
578         ll_finish_md_op_data(op_data);
579         ptlrpc_req_finished(req);
580         if (rc) {
581                 if (rc != -EPERM && rc != -EACCES)
582                         CERROR("mdc_setattr fails: rc = %d\n", rc);
583         }
584
585         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
586                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
587
588                 /* Get fsname and assume devname to be -MDT0000. */
589                 fsname = ll_get_fsname(inode);
590                 /* Set root stripesize */
591                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
592                         lump->lmm_stripe_size);
593                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
594                 if (rc)
595                         goto end;
596
597                 /* Set root stripecount */
598                 sprintf(param, "%s-MDT0000.lov.stripecount=%u", fsname,
599                         lump->lmm_stripe_count);
600                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
601                 if (rc)
602                         goto end;
603
604                 /* Set root stripeoffset */
605                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%u", fsname,
606                         lump->lmm_stripe_offset);
607                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
608                 if (rc)
609                         goto end;
610 end:
611                 if (fsname)
612                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
613                 if (param)
614                         OBD_FREE(param, MGS_PARAM_MAXLEN);
615         }
616         return rc;
617 }
618
619 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
620                      int *lmm_size, struct ptlrpc_request **request)
621 {
622         struct ll_sb_info *sbi = ll_i2sbi(inode);
623         struct mdt_body   *body;
624         struct lov_mds_md *lmm = NULL;
625         struct ptlrpc_request *req = NULL;
626         int rc, lmmsize;
627         struct obd_capa *oc;
628
629         rc = ll_get_max_mdsize(sbi, &lmmsize);
630         if (rc)
631                 RETURN(rc);
632
633         oc = ll_mdscapa_get(inode);
634         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
635                         oc, OBD_MD_FLEASIZE | OBD_MD_FLDIREA,
636                         lmmsize, &req);
637         capa_put(oc);
638         if (rc < 0) {
639                 CDEBUG(D_INFO, "md_getattr failed on inode "
640                        "%lu/%u: rc %d\n", inode->i_ino,
641                        inode->i_generation, rc);
642                 GOTO(out, rc);
643         }
644
645         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
646         LASSERT(body != NULL);
647
648         lmmsize = body->eadatasize;
649
650         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
651             lmmsize == 0) {
652                 GOTO(out, rc = -ENODATA);
653         }
654
655         lmm = req_capsule_server_sized_get(&req->rq_pill,
656                                            &RMF_MDT_MD, lmmsize);
657         LASSERT(lmm != NULL);
658
659         /*
660          * This is coming from the MDS, so is probably in
661          * little endian.  We convert it to host endian before
662          * passing it to userspace.
663          */
664         if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
665                 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
666         }
667 out:
668         *lmmp = lmm;
669         *lmm_size = lmmsize;
670         *request = req;
671         return rc;
672 }
673
674 static int ll_dir_ioctl(struct inode *inode, struct file *file,
675                         unsigned int cmd, unsigned long arg)
676 {
677         struct ll_sb_info *sbi = ll_i2sbi(inode);
678         struct obd_ioctl_data *data;
679         ENTRY;
680
681         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
682                inode->i_ino, inode->i_generation, inode, cmd);
683
684         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
685         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
686                 return -ENOTTY;
687
688         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
689         switch(cmd) {
690         case EXT3_IOC_GETFLAGS:
691         case EXT3_IOC_SETFLAGS:
692                 RETURN(ll_iocontrol(inode, file, cmd, arg));
693         case EXT3_IOC_GETVERSION_OLD:
694         case EXT3_IOC_GETVERSION:
695                 RETURN(put_user(inode->i_generation, (int *)arg));
696         /* We need to special case any other ioctls we want to handle,
697          * to send them to the MDS/OST as appropriate and to properly
698          * network encode the arg field.
699         case EXT3_IOC_SETVERSION_OLD:
700         case EXT3_IOC_SETVERSION:
701         */
702         case IOC_MDC_LOOKUP: {
703                 struct ptlrpc_request *request = NULL;
704                 int namelen, rc, len = 0;
705                 char *buf = NULL;
706                 char *filename;
707                 struct obd_capa *oc;
708
709                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
710                 if (rc)
711                         RETURN(rc);
712                 data = (void *)buf;
713
714                 filename = data->ioc_inlbuf1;
715                 namelen = data->ioc_inllen1;
716
717                 if (namelen < 1) {
718                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
719                         GOTO(out, rc = -EINVAL);
720                 }
721
722                 oc = ll_mdscapa_get(inode);
723                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode), oc,
724                                      filename, namelen, OBD_MD_FLID, 0,
725                                      ll_i2suppgid(inode), &request);
726                 capa_put(oc);
727                 if (rc < 0) {
728                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
729                         GOTO(out, rc);
730                 }
731
732                 ptlrpc_req_finished(request);
733
734                 EXIT;
735         out:
736                 obd_ioctl_freedata(buf, len);
737                 return rc;
738         }
739         case LL_IOC_LOV_SETSTRIPE: {
740                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
741                 int rc = 0;
742                 int set_default = 0;
743
744                 LASSERT(sizeof(lum) == sizeof(*lump));
745                 LASSERT(sizeof(lum.lmm_objects[0]) ==
746                         sizeof(lump->lmm_objects[0]));
747                 rc = copy_from_user(&lum, lump, sizeof(lum));
748                 if (rc)
749                         RETURN(-EFAULT);
750
751                 if (inode->i_sb->s_root == file->f_dentry)
752                         set_default = 1;
753
754                 rc = ll_dir_setstripe(inode, &lum, set_default);
755
756                 RETURN(rc);
757         }
758         case LL_IOC_OBD_STATFS:
759                 RETURN(ll_obd_statfs(inode, (void *)arg));
760         case LL_IOC_LOV_GETSTRIPE:
761         case LL_IOC_MDC_GETINFO:
762         case IOC_MDC_GETFILEINFO:
763         case IOC_MDC_GETFILESTRIPE: {
764                 struct ptlrpc_request *request = NULL;
765                 struct lov_user_md *lump;
766                 struct lov_mds_md *lmm = NULL;
767                 struct mdt_body *body;
768                 char *filename = NULL;
769                 int rc, lmmsize;
770
771                 if (cmd == IOC_MDC_GETFILEINFO ||
772                     cmd == IOC_MDC_GETFILESTRIPE) {
773                         filename = getname((const char *)arg);
774                         if (IS_ERR(filename))
775                                 RETURN(PTR_ERR(filename));
776
777                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
778                                                       &lmmsize, &request);
779                 } else {
780                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
781                 }
782
783                 if (request) {
784                         body = req_capsule_server_get(&request->rq_pill,
785                                                       &RMF_MDT_BODY);
786                         LASSERT(body != NULL);
787                 } else {
788                         GOTO(out_req, rc);
789                 }
790
791                 if (rc < 0) {
792                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
793                                                cmd == LL_IOC_MDC_GETINFO))
794                                 GOTO(skip_lmm, rc = 0);
795                         else
796                                 GOTO(out_req, rc);
797                 }
798
799                 if (cmd == IOC_MDC_GETFILESTRIPE ||
800                     cmd == LL_IOC_LOV_GETSTRIPE) {
801                         lump = (struct lov_user_md *)arg;
802                 } else {
803                         struct lov_user_mds_data *lmdp;
804                         lmdp = (struct lov_user_mds_data *)arg;
805                         lump = &lmdp->lmd_lmm;
806                 }
807                 rc = copy_to_user(lump, lmm, lmmsize);
808                 if (rc)
809                         GOTO(out_lmm, rc = -EFAULT);
810         skip_lmm:
811                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
812                         struct lov_user_mds_data *lmdp;
813                         lstat_t st = { 0 };
814
815                         st.st_dev     = inode->i_sb->s_dev;
816                         st.st_mode    = body->mode;
817                         st.st_nlink   = body->nlink;
818                         st.st_uid     = body->uid;
819                         st.st_gid     = body->gid;
820                         st.st_rdev    = body->rdev;
821                         st.st_size    = body->size;
822                         st.st_blksize = CFS_PAGE_SIZE;
823                         st.st_blocks  = body->blocks;
824                         st.st_atime   = body->atime;
825                         st.st_mtime   = body->mtime;
826                         st.st_ctime   = body->ctime;
827                         st.st_ino     = inode->i_ino;
828
829                         lmdp = (struct lov_user_mds_data *)arg;
830                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
831                         if (rc)
832                                 GOTO(out_lmm, rc = -EFAULT);
833                 }
834
835                 EXIT;
836         out_lmm:
837                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
838                         OBD_FREE(lmm, lmmsize);
839         out_req:
840                 ptlrpc_req_finished(request);
841                 if (filename)
842                         putname(filename);
843                 return rc;
844         }
845         case IOC_LOV_GETINFO: {
846                 struct lov_user_mds_data *lumd;
847                 struct lov_stripe_md *lsm;
848                 struct lov_user_md *lum;
849                 struct lov_mds_md *lmm;
850                 int lmmsize;
851                 lstat_t st;
852                 int rc;
853
854                 lumd = (struct lov_user_mds_data *)arg;
855                 lum = &lumd->lmd_lmm;
856
857                 rc = ll_get_max_mdsize(sbi, &lmmsize);
858                 if (rc)
859                         RETURN(rc);
860
861                 OBD_ALLOC(lmm, lmmsize);
862                 rc = copy_from_user(lmm, lum, lmmsize);
863                 if (rc)
864                         GOTO(free_lmm, rc = -EFAULT);
865
866                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
867                 if (rc < 0)
868                         GOTO(free_lmm, rc = -ENOMEM);
869
870                 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
871                 if (rc)
872                         GOTO(free_lsm, rc);
873
874                 /* Perform glimpse_size operation. */
875                 memset(&st, 0, sizeof(st));
876
877                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
878                 if (rc)
879                         GOTO(free_lsm, rc);
880
881                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
882                 if (rc)
883                         GOTO(free_lsm, rc = -EFAULT);
884
885                 EXIT;
886         free_lsm:
887                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
888         free_lmm:
889                 OBD_FREE(lmm, lmmsize);
890                 return rc;
891         }
892         case OBD_IOC_LLOG_CATINFO: {
893                 struct ptlrpc_request *req = NULL;
894                 char                  *buf = NULL;
895                 char                  *str;
896                 int                    len = 0;
897                 int                    rc;
898
899                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
900                 if (rc)
901                         RETURN(rc);
902                 data = (void *)buf;
903
904                 if (!data->ioc_inlbuf1) {
905                         obd_ioctl_freedata(buf, len);
906                         RETURN(-EINVAL);
907                 }
908
909                 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
910                                            &RQF_LLOG_CATINFO);
911                 if (req == NULL)
912                         GOTO(out_catinfo, rc = -ENOMEM);
913
914                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
915                                      data->ioc_inllen1);
916                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
917                                      data->ioc_inllen2);
918
919                 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
920                 if (rc) {
921                         ptlrpc_request_free(req);
922                         GOTO(out_catinfo, rc);
923                 }
924
925                 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
926                 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
927                 if (data->ioc_inllen2) {
928                         str = req_capsule_client_get(&req->rq_pill,
929                                                      &RMF_STRING);
930                         memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
931                 }
932
933                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
934                                      data->ioc_plen1);
935                 ptlrpc_request_set_replen(req);
936
937                 rc = ptlrpc_queue_wait(req);
938                 if (!rc) {
939                         str = req_capsule_server_get(&req->rq_pill,
940                                                      &RMF_STRING);
941                         rc = copy_to_user(data->ioc_pbuf1, str, data->ioc_plen1);
942                 }
943                 ptlrpc_req_finished(req);
944         out_catinfo:
945                 obd_ioctl_freedata(buf, len);
946                 RETURN(rc);
947         }
948         case OBD_IOC_QUOTACHECK: {
949                 struct obd_quotactl *oqctl;
950                 int rc, error = 0;
951
952                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
953                         RETURN(-EPERM);
954
955                 OBD_ALLOC_PTR(oqctl);
956                 if (!oqctl)
957                         RETURN(-ENOMEM);
958                 oqctl->qc_type = arg;
959                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
960                 if (rc < 0) {
961                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
962                         error = rc;
963                 }
964
965                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
966                 if (rc < 0)
967                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
968
969                 OBD_FREE_PTR(oqctl);
970                 return error ?: rc;
971         }
972         case OBD_IOC_POLL_QUOTACHECK: {
973                 struct if_quotacheck *check;
974                 int rc;
975
976                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
977                         RETURN(-EPERM);
978
979                 OBD_ALLOC_PTR(check);
980                 if (!check)
981                         RETURN(-ENOMEM);
982
983                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
984                                    NULL);
985                 if (rc) {
986                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
987                         if (copy_to_user((void *)arg, check, sizeof(*check)))
988                                 rc = -EFAULT;
989                         GOTO(out_poll, rc);
990                 }
991
992                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
993                                    NULL);
994                 if (rc) {
995                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
996                         if (copy_to_user((void *)arg, check, sizeof(*check)))
997                                 rc = -EFAULT;
998                         GOTO(out_poll, rc);
999                 }
1000         out_poll:
1001                 OBD_FREE_PTR(check);
1002                 RETURN(rc);
1003         }
1004 #ifdef HAVE_QUOTA_SUPPORT
1005         case OBD_IOC_QUOTACTL: {
1006                 struct if_quotactl *qctl;
1007                 struct obd_quotactl *oqctl;
1008
1009                 int cmd, type, id, rc = 0;
1010
1011                 OBD_ALLOC_PTR(qctl);
1012                 if (!qctl)
1013                         RETURN(-ENOMEM);
1014
1015                 OBD_ALLOC_PTR(oqctl);
1016                 if (!oqctl) {
1017                         OBD_FREE_PTR(qctl);
1018                         RETURN(-ENOMEM);
1019                 }
1020                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1021                         GOTO(out_quotactl, rc = -EFAULT);
1022
1023                 cmd = qctl->qc_cmd;
1024                 type = qctl->qc_type;
1025                 id = qctl->qc_id;
1026                 switch (cmd) {
1027                 case Q_QUOTAON:
1028                 case Q_QUOTAOFF:
1029                 case Q_SETQUOTA:
1030                 case Q_SETINFO:
1031                         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1032                                 GOTO(out_quotactl, rc = -EPERM);
1033                         break;
1034                 case Q_GETQUOTA:
1035                         if (((type == USRQUOTA && current->euid != id) ||
1036                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1037                             !cfs_capable(CFS_CAP_SYS_ADMIN))
1038                                 GOTO(out_quotactl, rc = -EPERM);
1039
1040                         /* XXX: dqb_valid is borrowed as a flag to mark that
1041                          *      only mds quota is wanted */
1042                         if (qctl->qc_dqblk.dqb_valid)
1043                                 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
1044                                                         u.cli.cl_target_uuid;
1045                         break;
1046                 case Q_GETINFO:
1047                         break;
1048                 default:
1049                         CERROR("unsupported quotactl op: %#x\n", cmd);
1050                         GOTO(out_quotactl, rc = -ENOTTY);
1051                 }
1052
1053                 QCTL_COPY(oqctl, qctl);
1054
1055                 if (qctl->obd_uuid.uuid[0]) {
1056                         struct obd_device *obd;
1057                         struct obd_uuid *uuid = &qctl->obd_uuid;
1058
1059                         obd = class_find_client_notype(uuid,
1060                                          &sbi->ll_dt_exp->exp_obd->obd_uuid);
1061                         if (!obd)
1062                                 GOTO(out_quotactl, rc = -ENOENT);
1063
1064                         if (cmd == Q_GETINFO)
1065                                 oqctl->qc_cmd = Q_GETOINFO;
1066                         else if (cmd == Q_GETQUOTA)
1067                                 oqctl->qc_cmd = Q_GETOQUOTA;
1068                         else
1069                                 GOTO(out_quotactl, rc = -EINVAL);
1070
1071                         if (sbi->ll_md_exp->exp_obd == obd) {
1072                                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1073                         } else {
1074                                 int i;
1075                                 struct obd_export *exp;
1076                                 struct lov_obd *lov = &sbi->ll_dt_exp->
1077                                                             exp_obd->u.lov;
1078
1079                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1080                                         if (!lov->lov_tgts[i] ||
1081                                             !lov->lov_tgts[i]->ltd_active)
1082                                                 continue;
1083                                         exp = lov->lov_tgts[i]->ltd_exp;
1084                                         if (exp->exp_obd == obd) {
1085                                                 rc = obd_quotactl(exp, oqctl);
1086                                                 break;
1087                                         }
1088                                 }
1089                         }
1090
1091                         oqctl->qc_cmd = cmd;
1092                         QCTL_COPY(qctl, oqctl);
1093
1094                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1095                                 rc = -EFAULT;
1096
1097                         GOTO(out_quotactl, rc);
1098                 }
1099
1100                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1101                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1102                         oqctl->qc_cmd = Q_QUOTAOFF;
1103                         obd_quotactl(sbi->ll_md_exp, oqctl);
1104                 }
1105
1106                 QCTL_COPY(qctl, oqctl);
1107
1108                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1109                         rc = -EFAULT;
1110         out_quotactl:
1111                 OBD_FREE_PTR(qctl);
1112                 OBD_FREE_PTR(oqctl);
1113                 RETURN(rc);
1114         }
1115 #endif /* HAVE_QUOTA_SUPPORT */
1116         case OBD_IOC_GETNAME: {
1117                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1118                 if (!obd)
1119                         RETURN(-EFAULT);
1120                 if (copy_to_user((void *)arg, obd->obd_name,
1121                                 strlen(obd->obd_name) + 1))
1122                         RETURN (-EFAULT);
1123                 RETURN(0);
1124         }
1125         case LL_IOC_FLUSHCTX:
1126                 RETURN(ll_flush_ctx(inode));
1127 #ifdef CONFIG_FS_POSIX_ACL
1128         case LL_IOC_RMTACL: {
1129             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1130                 inode == inode->i_sb->s_root->d_inode) {
1131                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1132                 int rc;
1133
1134                 LASSERT(fd != NULL);
1135                 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1136                 if (!rc)
1137                         fd->fd_flags |= LL_FILE_RMTACL;
1138                 RETURN(rc);
1139             } else
1140                 RETURN(0);
1141         }
1142 #endif
1143         default:
1144                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1145         }
1146 }
1147
1148 int ll_dir_open(struct inode *inode, struct file *file)
1149 {
1150         ENTRY;
1151         RETURN(ll_file_open(inode, file));
1152 }
1153
1154 int ll_dir_release(struct inode *inode, struct file *file)
1155 {
1156         ENTRY;
1157         RETURN(ll_file_release(inode, file));
1158 }
1159
1160 struct file_operations ll_dir_operations = {
1161         .open     = ll_dir_open,
1162         .release  = ll_dir_release,
1163         .read     = generic_read_dir,
1164         .readdir  = ll_readdir,
1165         .ioctl    = ll_dir_ioctl
1166 };