Whamcloud - gitweb
b=20878 change kernelcomms from netlink to pipes
[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                         cl_isize_write(inode, body->size);
173                 SetPageUptodate(page);
174         }
175         ptlrpc_req_finished(request);
176
177         unlock_page(page);
178         EXIT;
179         return rc;
180 }
181
182 struct address_space_operations ll_dir_aops = {
183         .readpage  = ll_dir_readpage,
184 };
185
186 static void ll_check_page(struct inode *dir, struct page *page)
187 {
188         /* XXX: check page format later */
189         SetPageChecked(page);
190 }
191
192 static void ll_release_page(struct page *page, __u64 hash,
193                             __u64 start, __u64 end)
194 {
195         kunmap(page);
196         lock_page(page);
197         if (likely(page->mapping != NULL)) {
198                 ll_truncate_complete_page(page);
199                 unlock_page(page);
200         } else {
201                 unlock_page(page);
202                 CWARN("NULL mapping page %p, truncated by others: "
203                       "hash("LPX64") | start("LPX64") | end("LPX64")\n",
204                       page, hash, start, end);
205         }
206         page_cache_release(page);
207 }
208
209 /*
210  * Find, kmap and return page that contains given hash.
211  */
212 static struct page *ll_dir_page_locate(struct inode *dir, __u64 hash,
213                                        __u64 *start, __u64 *end)
214 {
215         struct address_space *mapping = dir->i_mapping;
216         /*
217          * Complement of hash is used as an index so that
218          * radix_tree_gang_lookup() can be used to find a page with starting
219          * hash _smaller_ than one we are looking for.
220          */
221         unsigned long offset = hash_x_index((unsigned long)hash);
222         struct page *page;
223         int found;
224
225         TREE_READ_LOCK_IRQ(mapping);
226         found = radix_tree_gang_lookup(&mapping->page_tree,
227                                        (void **)&page, offset, 1);
228         if (found > 0) {
229                 struct lu_dirpage *dp;
230
231                 page_cache_get(page);
232                 TREE_READ_UNLOCK_IRQ(mapping);
233                 /*
234                  * In contrast to find_lock_page() we are sure that directory
235                  * page cannot be truncated (while DLM lock is held) and,
236                  * hence, can avoid restart.
237                  *
238                  * In fact, page cannot be locked here at all, because
239                  * ll_dir_readpage() does synchronous io.
240                  */
241                 wait_on_page(page);
242                 if (PageUptodate(page)) {
243                         dp = kmap(page);
244                         *start = le64_to_cpu(dp->ldp_hash_start);
245                         *end   = le64_to_cpu(dp->ldp_hash_end);
246                         LASSERT(*start <= hash);
247                         if (hash > *end || (*end != *start && hash == *end)) {
248                                 ll_release_page(page, hash, *start, *end);
249                                 page = NULL;
250                         }
251                 } else {
252                         page_cache_release(page);
253                         page = ERR_PTR(-EIO);
254                 }
255
256         } else {
257                 TREE_READ_UNLOCK_IRQ(mapping);
258                 page = NULL;
259         }
260         return page;
261 }
262
263 struct page *ll_get_dir_page(struct inode *dir, __u64 hash, int exact,
264                              struct ll_dir_chain *chain)
265 {
266         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
267         struct address_space *mapping = dir->i_mapping;
268         struct lustre_handle lockh;
269         struct lu_dirpage *dp;
270         struct page *page;
271         ldlm_mode_t mode;
272         int rc;
273         __u64 start = 0;
274         __u64 end = 0;
275
276         mode = LCK_PR;
277         rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
278                            ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
279         if (!rc) {
280                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
281                        ll_md_blocking_ast, ldlm_completion_ast,
282                        NULL, NULL, dir };
283                 struct lookup_intent it = { .it_op = IT_READDIR };
284                 struct ptlrpc_request *request;
285                 struct md_op_data *op_data;
286
287                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
288                                              LUSTRE_OPC_ANY, NULL);
289                 if (IS_ERR(op_data))
290                         return (void *)op_data;
291
292                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
293                                 op_data, &lockh, NULL, 0, NULL, 0);
294
295                 ll_finish_md_op_data(op_data);
296
297                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
298                 if (request)
299                         ptlrpc_req_finished(request);
300                 if (rc < 0) {
301                         CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
302                                PFID(ll_inode2fid(dir)), hash, rc);
303                         return ERR_PTR(rc);
304                 }
305         } else {
306                 /* for cross-ref object, l_ast_data of the lock may not be set,
307                  * we reset it here */
308                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
309                                  dir, NULL);
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                 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
316                        PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
317                 GOTO(out_unlock, page);
318         }
319
320         if (page != NULL) {
321                 /*
322                  * XXX nikita: not entirely correct handling of a corner case:
323                  * suppose hash chain of entries with hash value HASH crosses
324                  * border between pages P0 and P1. First both P0 and P1 are
325                  * cached, seekdir() is called for some entry from the P0 part
326                  * of the chain. Later P0 goes out of cache. telldir(HASH)
327                  * happens and finds P1, as it starts with matching hash
328                  * value. Remaining entries from P0 part of the chain are
329                  * skipped. (Is that really a bug?)
330                  *
331                  * Possible solutions: 0. don't cache P1 is such case, handle
332                  * it as an "overflow" page. 1. invalidate all pages at
333                  * once. 2. use HASH|1 as an index for P1.
334                  */
335                 if (exact && hash != start) {
336                         /*
337                          * readdir asked for a page starting _exactly_ from
338                          * given hash, but cache contains stale page, with
339                          * entries with smaller hash values. Stale page should
340                          * be invalidated, and new one fetched.
341                          */
342                         CDEBUG(D_OTHER, "Stale readpage page %p: "LPX64" != "LPX64"\n",
343                                page, hash, start);
344                         ll_release_page(page, hash, start, end);
345                 } else {
346                         GOTO(hash_collision, page);
347                 }
348         }
349
350         page = read_cache_page(mapping, hash_x_index((unsigned long)hash),
351                                (filler_t*)mapping->a_ops->readpage, NULL);
352         if (IS_ERR(page)) {
353                 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
354                        PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
355                 GOTO(out_unlock, page);
356         }
357
358         wait_on_page(page);
359         (void)kmap(page);
360         if (!PageUptodate(page)) {
361                 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
362                        PFID(ll_inode2fid(dir)), hash, -5);
363                 goto fail;
364         }
365         if (!PageChecked(page))
366                 ll_check_page(dir, page);
367         if (PageError(page)) {
368                 CERROR("page error: "DFID" at "LPU64": rc %d\n",
369                        PFID(ll_inode2fid(dir)), hash, -5);
370                 goto fail;
371         }
372 hash_collision:
373         dp = page_address(page);
374
375         start = le64_to_cpu(dp->ldp_hash_start);
376         end   = le64_to_cpu(dp->ldp_hash_end);
377         if (end == start) {
378                 LASSERT(start == hash);
379                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
380                 /*
381                  * Fetch whole overflow chain...
382                  *
383                  * XXX not yet.
384                  */
385                 goto fail;
386         }
387 out_unlock:
388         ldlm_lock_decref(&lockh, mode);
389         return page;
390
391 fail:
392         ll_put_page(page);
393         page = ERR_PTR(-EIO);
394         goto out_unlock;
395 }
396
397 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
398 {
399         struct inode         *inode = filp->f_dentry->d_inode;
400         struct ll_inode_info *info  = ll_i2info(inode);
401         __u64                 pos   = filp->f_pos;
402         struct page          *page;
403         struct ll_dir_chain   chain;
404         int rc;
405         int done;
406         int shift;
407         __u16 type;
408         ENTRY;
409
410         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu\n",
411                inode->i_ino, inode->i_generation, inode,
412                (unsigned long)pos, i_size_read(inode));
413
414         if (pos == DIR_END_OFF)
415                 /*
416                  * end-of-file.
417                  */
418                 RETURN(0);
419
420         rc    = 0;
421         done  = 0;
422         shift = 0;
423         ll_dir_chain_init(&chain);
424
425         page = ll_get_dir_page(inode, pos, 0, &chain);
426
427         while (rc == 0 && !done) {
428                 struct lu_dirpage *dp;
429                 struct lu_dirent  *ent;
430
431                 if (!IS_ERR(page)) {
432                         /*
433                          * If page is empty (end of directory is reached),
434                          * use this value.
435                          */
436                         __u64 hash = DIR_END_OFF;
437                         __u64 next;
438
439                         dp = page_address(page);
440                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
441                              ent = lu_dirent_next(ent)) {
442                                 char          *name;
443                                 int            namelen;
444                                 struct lu_fid  fid;
445                                 ino_t          ino;
446
447                                 /*
448                                  * XXX: implement correct swabbing here.
449                                  */
450
451                                 hash    = le64_to_cpu(ent->lde_hash);
452                                 namelen = le16_to_cpu(ent->lde_namelen);
453
454                                 if (hash < pos)
455                                         /*
456                                          * Skip until we find target hash
457                                          * value.
458                                          */
459                                         continue;
460
461                                 if (namelen == 0)
462                                         /*
463                                          * Skip dummy record.
464                                          */
465                                         continue;
466
467                                 fid  = ent->lde_fid;
468                                 name = ent->lde_name;
469                                 fid_le_to_cpu(&fid, &fid);
470                                 ino  = cl_fid_build_ino(&fid);
471                                 type = ll_dirent_type_get(ent);
472                                 done = filldir(cookie, name, namelen,
473                                                (loff_t)hash, ino, type);
474                         }
475                         next = le64_to_cpu(dp->ldp_hash_end);
476                         ll_put_page(page);
477                         if (!done) {
478                                 pos = next;
479                                 if (pos == DIR_END_OFF)
480                                         /*
481                                          * End of directory reached.
482                                          */
483                                         done = 1;
484                                 else if (1 /* chain is exhausted*/)
485                                         /*
486                                          * Normal case: continue to the next
487                                          * page.
488                                          */
489                                         page = ll_get_dir_page(inode, pos, 1,
490                                                                &chain);
491                                 else {
492                                         /*
493                                          * go into overflow page.
494                                          */
495                                 }
496                         } else
497                                 pos = hash;
498                 } else {
499                         rc = PTR_ERR(page);
500                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
501                                PFID(&info->lli_fid), (unsigned long)pos, rc);
502                 }
503         }
504
505         filp->f_pos = (loff_t)pos;
506         filp->f_version = inode->i_version;
507         touch_atime(filp->f_vfsmnt, filp->f_dentry);
508
509         ll_dir_chain_fini(&chain);
510
511         RETURN(rc);
512 }
513
514 int ll_send_mgc_param(struct obd_export *mgc, char *string)
515 {
516         struct mgs_send_param *msp;
517         int rc = 0;
518
519         OBD_ALLOC_PTR(msp);
520         if (!msp)
521                 return -ENOMEM;
522
523         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
524         rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
525                                 sizeof(struct mgs_send_param), msp, NULL);
526         if (rc)
527                 CERROR("Failed to set parameter: %d\n", rc);
528         OBD_FREE_PTR(msp);
529
530         return rc;
531 }
532
533 char *ll_get_fsname(struct inode *inode)
534 {
535         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
536         char *ptr, *fsname;
537         int len;
538
539         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
540         len = strlen(lsi->lsi_lmd->lmd_profile);
541         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
542         if (ptr && (strcmp(ptr, "-client") == 0))
543                 len -= 7;
544         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
545         fsname[len] = '\0';
546
547         return fsname;
548 }
549
550 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
551                      int set_default)
552 {
553         struct ll_sb_info *sbi = ll_i2sbi(inode);
554         struct md_op_data *op_data;
555         struct ptlrpc_request *req = NULL;
556         int rc = 0;
557         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
558         struct obd_device *mgc = lsi->lsi_mgc;
559         char *fsname = NULL, *param = NULL;
560         int lum_size;
561
562         /*
563          * This is coming from userspace, so should be in
564          * local endian.  But the MDS would like it in little
565          * endian, so we swab it before we send it.
566          */
567         switch (lump->lmm_magic) {
568         case LOV_USER_MAGIC_V1: {
569                 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
570                         lustre_swab_lov_user_md_v1(lump);
571                 lum_size = sizeof(struct lov_user_md_v1);
572                 break;
573                 }
574         case LOV_USER_MAGIC_V3: {
575                 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
576                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lump);
577                 lum_size = sizeof(struct lov_user_md_v3);
578                 break;
579                 }
580         default: {
581                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
582                                 " %#08x != %#08x nor %#08x\n",
583                                 lump->lmm_magic, LOV_USER_MAGIC_V1,
584                                 LOV_USER_MAGIC_V3);
585                 RETURN(-EINVAL);
586                 }
587         }
588
589         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
590                                      LUSTRE_OPC_ANY, NULL);
591         if (IS_ERR(op_data))
592                 RETURN(PTR_ERR(op_data));
593
594         /* swabbing is done in lov_setstripe() on server side */
595         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
596                         NULL, 0, &req, NULL);
597         ll_finish_md_op_data(op_data);
598         ptlrpc_req_finished(req);
599         if (rc) {
600                 if (rc != -EPERM && rc != -EACCES)
601                         CERROR("mdc_setattr fails: rc = %d\n", rc);
602         }
603
604         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
605          LOV_USER_MAGIC_V3 have the same initial fields so we do not
606          need the make the distiction between the 2 versions */
607         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
608                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
609
610                 /* Get fsname and assume devname to be -MDT0000. */
611                 fsname = ll_get_fsname(inode);
612                 /* Set root stripesize */
613                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
614                         lump->lmm_stripe_size);
615                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
616                 if (rc)
617                         goto end;
618
619                 /* Set root stripecount */
620                 sprintf(param, "%s-MDT0000.lov.stripecount=%hd", fsname,
621                         lump->lmm_stripe_count);
622                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
623                 if (rc)
624                         goto end;
625
626                 /* Set root stripeoffset */
627                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%hd", fsname,
628                         lump->lmm_stripe_offset);
629                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
630                 if (rc)
631                         goto end;
632 end:
633                 if (fsname)
634                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
635                 if (param)
636                         OBD_FREE(param, MGS_PARAM_MAXLEN);
637         }
638         return rc;
639 }
640
641 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
642                      int *lmm_size, struct ptlrpc_request **request)
643 {
644         struct ll_sb_info *sbi = ll_i2sbi(inode);
645         struct mdt_body   *body;
646         struct lov_mds_md *lmm = NULL;
647         struct ptlrpc_request *req = NULL;
648         int rc, lmmsize;
649         struct md_op_data *op_data;
650
651         rc = ll_get_max_mdsize(sbi, &lmmsize);
652         if (rc)
653                 RETURN(rc);
654
655         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
656                                      0, lmmsize, LUSTRE_OPC_ANY,
657                                      NULL);
658         if (op_data == NULL)
659                 RETURN(-ENOMEM);
660
661         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
662         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
663         ll_finish_md_op_data(op_data);
664         if (rc < 0) {
665                 CDEBUG(D_INFO, "md_getattr failed on inode "
666                        "%lu/%u: rc %d\n", inode->i_ino,
667                        inode->i_generation, rc);
668                 GOTO(out, rc);
669         }
670
671         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
672         LASSERT(body != NULL);
673
674         lmmsize = body->eadatasize;
675
676         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
677             lmmsize == 0) {
678                 GOTO(out, rc = -ENODATA);
679         }
680
681         lmm = req_capsule_server_sized_get(&req->rq_pill,
682                                            &RMF_MDT_MD, lmmsize);
683         LASSERT(lmm != NULL);
684
685         /*
686          * This is coming from the MDS, so is probably in
687          * little endian.  We convert it to host endian before
688          * passing it to userspace.
689          */
690         /* We don't swab objects for directories */
691         switch (le32_to_cpu(lmm->lmm_magic)) {
692         case LOV_MAGIC_V1:
693                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
694                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
695                 break;
696         case LOV_MAGIC_V3:
697                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
698                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
699                 break;
700         default:
701                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
702                 rc = -EPROTO;
703         }
704 out:
705         *lmmp = lmm;
706         *lmm_size = lmmsize;
707         *request = req;
708         return rc;
709 }
710
711 /*
712  *  Get MDT index for the inode.
713  */
714 int ll_get_mdt_idx(struct inode *inode)
715 {
716         struct ll_sb_info *sbi = ll_i2sbi(inode);
717         struct md_op_data *op_data;
718         int rc, mdtidx;
719         ENTRY;
720
721         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
722                                      0, LUSTRE_OPC_ANY, NULL);
723         if (op_data == NULL)
724                 RETURN(-ENOMEM);
725
726         op_data->op_valid |= OBD_MD_MDTIDX;
727         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
728         mdtidx = op_data->op_mds;
729         ll_finish_md_op_data(op_data);
730         if (rc < 0) {
731                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
732                 RETURN(rc);
733         }
734         return mdtidx;
735 }
736
737 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
738 {
739         void *ptr;
740         int rc;
741
742         OBD_ALLOC(ptr, len);
743         if (ptr == NULL)
744                 return -ENOMEM;
745         if (cfs_copy_from_user(ptr, data, len)) {
746                 OBD_FREE(ptr, len);
747                 return -EFAULT;
748         }
749         rc = obd_iocontrol(cmd, exp, len, data, NULL);
750         OBD_FREE(ptr, len);
751         return rc;
752 }
753
754 static int ll_dir_ioctl(struct inode *inode, struct file *file,
755                         unsigned int cmd, unsigned long arg)
756 {
757         struct ll_sb_info *sbi = ll_i2sbi(inode);
758         struct obd_ioctl_data *data;
759         int rc = 0;
760         ENTRY;
761
762         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
763                inode->i_ino, inode->i_generation, inode, cmd);
764
765         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
766         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
767                 return -ENOTTY;
768
769         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
770         switch(cmd) {
771         case FSFILT_IOC_GETFLAGS:
772         case FSFILT_IOC_SETFLAGS:
773                 RETURN(ll_iocontrol(inode, file, cmd, arg));
774         case FSFILT_IOC_GETVERSION_OLD:
775         case FSFILT_IOC_GETVERSION:
776                 RETURN(put_user(inode->i_generation, (int *)arg));
777         /* We need to special case any other ioctls we want to handle,
778          * to send them to the MDS/OST as appropriate and to properly
779          * network encode the arg field.
780         case FSFILT_IOC_SETVERSION_OLD:
781         case FSFILT_IOC_SETVERSION:
782         */
783         case LL_IOC_GET_MDTIDX: {
784                 int mdtidx;
785
786                 mdtidx = ll_get_mdt_idx(inode);
787                 if (mdtidx < 0)
788                         RETURN(mdtidx);
789
790                 if (put_user((int)mdtidx, (int*)arg))
791                         RETURN(-EFAULT);
792
793                 return 0;
794         }
795         case IOC_MDC_LOOKUP: {
796                 struct ptlrpc_request *request = NULL;
797                 int namelen, len = 0;
798                 char *buf = NULL;
799                 char *filename;
800                 struct md_op_data *op_data;
801
802                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
803                 if (rc)
804                         RETURN(rc);
805                 data = (void *)buf;
806
807                 filename = data->ioc_inlbuf1;
808                 namelen = strlen(filename);
809
810                 if (namelen < 1) {
811                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
812                         GOTO(out_free, rc = -EINVAL);
813                 }
814
815                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
816                                              0, LUSTRE_OPC_ANY, NULL);
817                 if (op_data == NULL)
818                         GOTO(out_free, rc = -ENOMEM);
819
820                 op_data->op_valid = OBD_MD_FLID;
821                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
822                 ll_finish_md_op_data(op_data);
823                 if (rc < 0) {
824                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
825                         GOTO(out_free, rc);
826                 }
827                 ptlrpc_req_finished(request);
828                 EXIT;
829 out_free:
830                 obd_ioctl_freedata(buf, len);
831                 return rc;
832         }
833         case LL_IOC_LOV_SETSTRIPE: {
834                 struct lov_user_md_v3 lumv3;
835                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
836                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
837                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
838
839                 int set_default = 0;
840
841                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
842                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
843                         sizeof(lumv3p->lmm_objects[0]));
844                 /* first try with v1 which is smaller than v3 */
845                 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
846                         RETURN(-EFAULT);
847
848                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
849                         if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
850                                 RETURN(-EFAULT);
851                 }
852
853                 if (inode->i_sb->s_root == file->f_dentry)
854                         set_default = 1;
855
856                 /* in v1 and v3 cases lumv1 points to data */
857                 rc = ll_dir_setstripe(inode, lumv1, set_default);
858
859                 RETURN(rc);
860         }
861         case LL_IOC_OBD_STATFS:
862                 RETURN(ll_obd_statfs(inode, (void *)arg));
863         case LL_IOC_LOV_GETSTRIPE:
864         case LL_IOC_MDC_GETINFO:
865         case IOC_MDC_GETFILEINFO:
866         case IOC_MDC_GETFILESTRIPE: {
867                 struct ptlrpc_request *request = NULL;
868                 struct lov_user_md *lump;
869                 struct lov_mds_md *lmm = NULL;
870                 struct mdt_body *body;
871                 char *filename = NULL;
872                 int lmmsize;
873
874                 if (cmd == IOC_MDC_GETFILEINFO ||
875                     cmd == IOC_MDC_GETFILESTRIPE) {
876                         filename = getname((const char *)arg);
877                         if (IS_ERR(filename))
878                                 RETURN(PTR_ERR(filename));
879
880                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
881                                                       &lmmsize, &request);
882                 } else {
883                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
884                 }
885
886                 if (request) {
887                         body = req_capsule_server_get(&request->rq_pill,
888                                                       &RMF_MDT_BODY);
889                         LASSERT(body != NULL);
890                 } else {
891                         GOTO(out_req, rc);
892                 }
893
894                 if (rc < 0) {
895                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
896                                                cmd == LL_IOC_MDC_GETINFO))
897                                 GOTO(skip_lmm, rc = 0);
898                         else
899                                 GOTO(out_req, rc);
900                 }
901
902                 if (cmd == IOC_MDC_GETFILESTRIPE ||
903                     cmd == LL_IOC_LOV_GETSTRIPE) {
904                         lump = (struct lov_user_md *)arg;
905                 } else {
906                         struct lov_user_mds_data *lmdp;
907                         lmdp = (struct lov_user_mds_data *)arg;
908                         lump = &lmdp->lmd_lmm;
909                 }
910                 if (cfs_copy_to_user(lump, lmm, lmmsize))
911                         GOTO(out_req, rc = -EFAULT);
912         skip_lmm:
913                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
914                         struct lov_user_mds_data *lmdp;
915                         lstat_t st = { 0 };
916
917                         st.st_dev     = inode->i_sb->s_dev;
918                         st.st_mode    = body->mode;
919                         st.st_nlink   = body->nlink;
920                         st.st_uid     = body->uid;
921                         st.st_gid     = body->gid;
922                         st.st_rdev    = body->rdev;
923                         st.st_size    = body->size;
924                         st.st_blksize = CFS_PAGE_SIZE;
925                         st.st_blocks  = body->blocks;
926                         st.st_atime   = body->atime;
927                         st.st_mtime   = body->mtime;
928                         st.st_ctime   = body->ctime;
929                         st.st_ino     = inode->i_ino;
930
931                         lmdp = (struct lov_user_mds_data *)arg;
932                         if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
933                                 GOTO(out_req, rc = -EFAULT);
934                 }
935
936                 EXIT;
937         out_req:
938                 ptlrpc_req_finished(request);
939                 if (filename)
940                         putname(filename);
941                 return rc;
942         }
943         case IOC_LOV_GETINFO: {
944                 struct lov_user_mds_data *lumd;
945                 struct lov_stripe_md *lsm;
946                 struct lov_user_md *lum;
947                 struct lov_mds_md *lmm;
948                 int lmmsize;
949                 lstat_t st;
950
951                 lumd = (struct lov_user_mds_data *)arg;
952                 lum = &lumd->lmd_lmm;
953
954                 rc = ll_get_max_mdsize(sbi, &lmmsize);
955                 if (rc)
956                         RETURN(rc);
957
958                 OBD_ALLOC(lmm, lmmsize);
959                 if (cfs_copy_from_user(lmm, lum, lmmsize))
960                         GOTO(free_lmm, rc = -EFAULT);
961
962                 switch (lmm->lmm_magic) {
963                 case LOV_USER_MAGIC_V1:
964                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
965                                 break;
966                         /* swab objects first so that stripes num will be sane */
967                         lustre_swab_lov_user_md_objects(
968                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
969                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
970                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
971                         break;
972                 case LOV_USER_MAGIC_V3:
973                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
974                                 break;
975                         /* swab objects first so that stripes num will be sane */
976                         lustre_swab_lov_user_md_objects(
977                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
978                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
979                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
980                         break;
981                 default:
982                         GOTO(free_lmm, rc = -EINVAL);
983                 }
984
985                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
986                 if (rc < 0)
987                         GOTO(free_lmm, rc = -ENOMEM);
988
989                 /* Perform glimpse_size operation. */
990                 memset(&st, 0, sizeof(st));
991
992                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
993                 if (rc)
994                         GOTO(free_lsm, rc);
995
996                 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
997                         GOTO(free_lsm, rc = -EFAULT);
998
999                 EXIT;
1000         free_lsm:
1001                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1002         free_lmm:
1003                 OBD_FREE(lmm, lmmsize);
1004                 return rc;
1005         }
1006         case OBD_IOC_LLOG_CATINFO: {
1007                 struct ptlrpc_request *req = NULL;
1008                 char                  *buf = NULL;
1009                 char                  *str;
1010                 int                    len = 0;
1011
1012                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1013                 if (rc)
1014                         RETURN(rc);
1015                 data = (void *)buf;
1016
1017                 if (!data->ioc_inlbuf1) {
1018                         obd_ioctl_freedata(buf, len);
1019                         RETURN(-EINVAL);
1020                 }
1021
1022                 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
1023                                            &RQF_LLOG_CATINFO);
1024                 if (req == NULL)
1025                         GOTO(out_catinfo, rc = -ENOMEM);
1026
1027                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
1028                                      data->ioc_inllen1);
1029                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
1030                                      data->ioc_inllen2);
1031
1032                 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
1033                 if (rc) {
1034                         ptlrpc_request_free(req);
1035                         GOTO(out_catinfo, rc);
1036                 }
1037
1038                 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
1039                 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
1040                 if (data->ioc_inllen2) {
1041                         str = req_capsule_client_get(&req->rq_pill,
1042                                                      &RMF_STRING);
1043                         memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
1044                 }
1045
1046                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
1047                                      data->ioc_plen1);
1048                 ptlrpc_request_set_replen(req);
1049
1050                 rc = ptlrpc_queue_wait(req);
1051                 if (!rc) {
1052                         str = req_capsule_server_get(&req->rq_pill,
1053                                                      &RMF_STRING);
1054                         if (cfs_copy_to_user(data->ioc_pbuf1, str,
1055                                              data->ioc_plen1))
1056                                 rc = -EFAULT;
1057                 }
1058                 ptlrpc_req_finished(req);
1059         out_catinfo:
1060                 obd_ioctl_freedata(buf, len);
1061                 RETURN(rc);
1062         }
1063         case OBD_IOC_QUOTACHECK: {
1064                 struct obd_quotactl *oqctl;
1065                 int error = 0;
1066
1067                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1068                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1069                         RETURN(-EPERM);
1070
1071                 OBD_ALLOC_PTR(oqctl);
1072                 if (!oqctl)
1073                         RETURN(-ENOMEM);
1074                 oqctl->qc_type = arg;
1075                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1076                 if (rc < 0) {
1077                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1078                         error = rc;
1079                 }
1080
1081                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1082                 if (rc < 0)
1083                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1084
1085                 OBD_FREE_PTR(oqctl);
1086                 return error ?: rc;
1087         }
1088         case OBD_IOC_POLL_QUOTACHECK: {
1089                 struct if_quotacheck *check;
1090
1091                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1092                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1093                         RETURN(-EPERM);
1094
1095                 OBD_ALLOC_PTR(check);
1096                 if (!check)
1097                         RETURN(-ENOMEM);
1098
1099                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1100                                    NULL);
1101                 if (rc) {
1102                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1103                         if (cfs_copy_to_user((void *)arg, check,
1104                                              sizeof(*check)))
1105                                 rc = -EFAULT;
1106                         GOTO(out_poll, rc);
1107                 }
1108
1109                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1110                                    NULL);
1111                 if (rc) {
1112                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1113                         if (cfs_copy_to_user((void *)arg, check,
1114                                              sizeof(*check)))
1115                                 rc = -EFAULT;
1116                         GOTO(out_poll, rc);
1117                 }
1118         out_poll:
1119                 OBD_FREE_PTR(check);
1120                 RETURN(rc);
1121         }
1122         case OBD_IOC_QUOTACTL: {
1123                 struct if_quotactl *qctl;
1124                 int cmd, type, id, valid;
1125
1126                 OBD_ALLOC_PTR(qctl);
1127                 if (!qctl)
1128                         RETURN(-ENOMEM);
1129
1130                 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1131                         GOTO(out_quotactl, rc = -EFAULT);
1132
1133                 cmd = qctl->qc_cmd;
1134                 type = qctl->qc_type;
1135                 id = qctl->qc_id;
1136                 valid = qctl->qc_valid;
1137
1138                 switch (cmd) {
1139                 case LUSTRE_Q_INVALIDATE:
1140                 case LUSTRE_Q_FINVALIDATE:
1141                 case Q_QUOTAON:
1142                 case Q_QUOTAOFF:
1143                 case Q_SETQUOTA:
1144                 case Q_SETINFO:
1145                         if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1146                             sbi->ll_flags & LL_SBI_RMT_CLIENT)
1147                                 GOTO(out_quotactl, rc = -EPERM);
1148                         break;
1149                 case Q_GETQUOTA:
1150                         if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1151                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1152                             (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1153                              sbi->ll_flags & LL_SBI_RMT_CLIENT))
1154                                 GOTO(out_quotactl, rc = -EPERM);
1155                         break;
1156                 case Q_GETINFO:
1157                         break;
1158                 default:
1159                         CERROR("unsupported quotactl op: %#x\n", cmd);
1160                         GOTO(out_quotactl, rc = -ENOTTY);
1161                 }
1162
1163                 if (valid != QC_GENERAL) {
1164                         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1165                                 GOTO(out_quotactl, rc = -EOPNOTSUPP);
1166
1167                         if (cmd == Q_GETINFO)
1168                                 qctl->qc_cmd = Q_GETOINFO;
1169                         else if (cmd == Q_GETQUOTA)
1170                                 qctl->qc_cmd = Q_GETOQUOTA;
1171                         else
1172                                 GOTO(out_quotactl, rc = -EINVAL);
1173
1174                         switch (valid) {
1175                         case QC_MDTIDX:
1176                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1177                                                    sbi->ll_md_exp,
1178                                                    sizeof(*qctl), qctl, NULL);
1179                                 break;
1180                         case QC_OSTIDX:
1181                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1182                                                    sbi->ll_dt_exp,
1183                                                    sizeof(*qctl), qctl, NULL);
1184                                 break;
1185                         case QC_UUID:
1186                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1187                                                    sbi->ll_md_exp,
1188                                                    sizeof(*qctl), qctl, NULL);
1189                                 if (rc == -EAGAIN)
1190                                         rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1191                                                            sbi->ll_dt_exp,
1192                                                            sizeof(*qctl), qctl,
1193                                                            NULL);
1194                                 break;
1195                         default:
1196                                 rc = -EINVAL;
1197                                 break;
1198                         }
1199
1200                         if (rc)
1201                                 GOTO(out_quotactl, rc);
1202                         else
1203                                 qctl->qc_cmd = cmd;
1204                 } else {
1205                         struct obd_quotactl *oqctl;
1206
1207                         OBD_ALLOC_PTR(oqctl);
1208                         if (!oqctl)
1209                                 GOTO(out_quotactl, rc = -ENOMEM);
1210
1211                         QCTL_COPY(oqctl, qctl);
1212                         rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1213                         if (rc) {
1214                                 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1215                                         oqctl->qc_cmd = Q_QUOTAOFF;
1216                                         obd_quotactl(sbi->ll_md_exp, oqctl);
1217                                 }
1218                                 OBD_FREE_PTR(oqctl);
1219                                 GOTO(out_quotactl, rc);
1220                         } else {
1221                                 QCTL_COPY(qctl, oqctl);
1222                                 OBD_FREE_PTR(oqctl);
1223                         }
1224                 }
1225
1226                 if (cfs_copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1227                         rc = -EFAULT;
1228
1229         out_quotactl:
1230                 OBD_FREE_PTR(qctl);
1231                 RETURN(rc);
1232         }
1233         case OBD_IOC_GETNAME: {
1234                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1235                 if (!obd)
1236                         RETURN(-EFAULT);
1237                 if (cfs_copy_to_user((void *)arg, obd->obd_name,
1238                                      strlen(obd->obd_name) + 1))
1239                         RETURN (-EFAULT);
1240                 RETURN(0);
1241         }
1242         case LL_IOC_FLUSHCTX:
1243                 RETURN(ll_flush_ctx(inode));
1244 #ifdef CONFIG_FS_POSIX_ACL
1245         case LL_IOC_RMTACL: {
1246             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1247                 inode == inode->i_sb->s_root->d_inode) {
1248                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1249
1250                 LASSERT(fd != NULL);
1251                 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1252                 if (!rc)
1253                         fd->fd_flags |= LL_FILE_RMTACL;
1254                 RETURN(rc);
1255             } else
1256                 RETURN(0);
1257         }
1258 #endif
1259         case LL_IOC_GETOBDCOUNT: {
1260                 int count;
1261
1262                 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1263                         RETURN(-EFAULT);
1264
1265                 if (!count) {
1266                         /* get ost count */
1267                         struct lov_obd *lov = &sbi->ll_dt_exp->exp_obd->u.lov;
1268                         count = lov->desc.ld_tgt_count;
1269                 } else {
1270                         /* get mdt count */
1271                         struct lmv_obd *lmv = &sbi->ll_md_exp->exp_obd->u.lmv;
1272                         count = lmv->desc.ld_tgt_count;
1273                 }
1274
1275                 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1276                         RETURN(-EFAULT);
1277
1278                 RETURN(0);
1279         }
1280         case LL_IOC_PATH2FID:
1281                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1282                                      sizeof(struct lu_fid)))
1283                         RETURN(-EFAULT);
1284                 RETURN(0);
1285         case OBD_IOC_CHANGELOG_SEND:
1286         case OBD_IOC_CHANGELOG_CLEAR:
1287                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1288                                     sizeof(struct ioc_changelog));
1289                 RETURN(rc);
1290         case OBD_IOC_FID2PATH:
1291                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1292         case LL_IOC_HSM_CT_START:
1293                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1294                                     sizeof(struct lustre_kernelcomm));
1295                 RETURN(rc);
1296
1297         default:
1298                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1299         }
1300 }
1301
1302 int ll_dir_open(struct inode *inode, struct file *file)
1303 {
1304         ENTRY;
1305         RETURN(ll_file_open(inode, file));
1306 }
1307
1308 int ll_dir_release(struct inode *inode, struct file *file)
1309 {
1310         ENTRY;
1311         RETURN(ll_file_release(inode, file));
1312 }
1313
1314 struct file_operations ll_dir_operations = {
1315         .open     = ll_dir_open,
1316         .release  = ll_dir_release,
1317         .read     = generic_read_dir,
1318         .readdir  = ll_readdir,
1319         .ioctl    = ll_dir_ioctl
1320 };