Whamcloud - gitweb
b=22766 cascading_rw: take lmm_stripe_count returned by ioctl(LL_IOC_LOV_SETSTRIPE)
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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 ll_sb_info    *sbi  = ll_i2sbi(inode);
403         struct page          *page;
404         struct ll_dir_chain   chain;
405         int rc, need_32bit;
406         int done;
407         int shift;
408         __u16 type;
409         ENTRY;
410
411         need_32bit = ll_need_32bit_api(sbi);
412         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu 32bit_api %d\n",
413                inode->i_ino, inode->i_generation, inode,
414                (unsigned long)pos, i_size_read(inode), need_32bit);
415
416         if (pos == DIR_END_OFF)
417                 /*
418                  * end-of-file.
419                  */
420                 RETURN(0);
421
422         rc    = 0;
423         done  = 0;
424         shift = 0;
425         ll_dir_chain_init(&chain);
426
427         page = ll_get_dir_page(inode, pos, 0, &chain);
428
429         while (rc == 0 && !done) {
430                 struct lu_dirpage *dp;
431                 struct lu_dirent  *ent;
432
433                 if (!IS_ERR(page)) {
434                         /*
435                          * If page is empty (end of directory is reached),
436                          * use this value.
437                          */
438                         __u64 hash = DIR_END_OFF;
439                         __u64 next;
440
441                         dp = page_address(page);
442                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
443                              ent = lu_dirent_next(ent)) {
444                                 char          *name;
445                                 int            namelen;
446                                 struct lu_fid  fid;
447                                 __u64          ino;
448
449                                 /*
450                                  * XXX: implement correct swabbing here.
451                                  */
452
453                                 hash    = le64_to_cpu(ent->lde_hash);
454                                 namelen = le16_to_cpu(ent->lde_namelen);
455
456                                 if (hash < pos)
457                                         /*
458                                          * Skip until we find target hash
459                                          * value.
460                                          */
461                                         continue;
462
463                                 if (namelen == 0)
464                                         /*
465                                          * Skip dummy record.
466                                          */
467                                         continue;
468
469                                 fid  = ent->lde_fid;
470                                 name = ent->lde_name;
471                                 fid_le_to_cpu(&fid, &fid);
472                                 if (need_32bit)
473                                         ino = cl_fid_build_ino32(&fid);
474                                 else
475                                         ino = cl_fid_build_ino(&fid);
476                                 type = ll_dirent_type_get(ent);
477                                 done = filldir(cookie, name, namelen,
478                                                (loff_t)hash, ino, type);
479                         }
480                         next = le64_to_cpu(dp->ldp_hash_end);
481                         ll_put_page(page);
482                         if (!done) {
483                                 pos = next;
484                                 if (pos == DIR_END_OFF)
485                                         /*
486                                          * End of directory reached.
487                                          */
488                                         done = 1;
489                                 else if (1 /* chain is exhausted*/)
490                                         /*
491                                          * Normal case: continue to the next
492                                          * page.
493                                          */
494                                         page = ll_get_dir_page(inode, pos, 1,
495                                                                &chain);
496                                 else {
497                                         /*
498                                          * go into overflow page.
499                                          */
500                                 }
501                         } else
502                                 pos = hash;
503                 } else {
504                         rc = PTR_ERR(page);
505                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
506                                PFID(&info->lli_fid), (unsigned long)pos, rc);
507                 }
508         }
509
510         filp->f_pos = (loff_t)pos;
511         filp->f_version = inode->i_version;
512         touch_atime(filp->f_vfsmnt, filp->f_dentry);
513
514         ll_dir_chain_fini(&chain);
515
516         RETURN(rc);
517 }
518
519 int ll_send_mgc_param(struct obd_export *mgc, char *string)
520 {
521         struct mgs_send_param *msp;
522         int rc = 0;
523
524         OBD_ALLOC_PTR(msp);
525         if (!msp)
526                 return -ENOMEM;
527
528         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
529         rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
530                                 sizeof(struct mgs_send_param), msp, NULL);
531         if (rc)
532                 CERROR("Failed to set parameter: %d\n", rc);
533         OBD_FREE_PTR(msp);
534
535         return rc;
536 }
537
538 char *ll_get_fsname(struct inode *inode)
539 {
540         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
541         char *ptr, *fsname;
542         int len;
543
544         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
545         len = strlen(lsi->lsi_lmd->lmd_profile);
546         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
547         if (ptr && (strcmp(ptr, "-client") == 0))
548                 len -= 7;
549         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
550         fsname[len] = '\0';
551
552         return fsname;
553 }
554
555 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
556                      int set_default)
557 {
558         struct ll_sb_info *sbi = ll_i2sbi(inode);
559         struct md_op_data *op_data;
560         struct ptlrpc_request *req = NULL;
561         int rc = 0;
562         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
563         struct obd_device *mgc = lsi->lsi_mgc;
564         char *fsname = NULL, *param = NULL;
565         int lum_size;
566
567         if (lump != NULL) {
568                 /*
569                  * This is coming from userspace, so should be in
570                  * local endian.  But the MDS would like it in little
571                  * endian, so we swab it before we send it.
572                  */
573                 switch (lump->lmm_magic) {
574                 case LOV_USER_MAGIC_V1: {
575                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
576                                 lustre_swab_lov_user_md_v1(lump);
577                         lum_size = sizeof(struct lov_user_md_v1);
578                         break;
579                         }
580                 case LOV_USER_MAGIC_V3: {
581                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
582                                 lustre_swab_lov_user_md_v3(
583                                         (struct lov_user_md_v3 *)lump);
584                         lum_size = sizeof(struct lov_user_md_v3);
585                         break;
586                         }
587                 default: {
588                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
589                                         " %#08x != %#08x nor %#08x\n",
590                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
591                                         LOV_USER_MAGIC_V3);
592                         RETURN(-EINVAL);
593                         }
594                }
595         } else {
596                 lum_size = sizeof(struct lov_user_md_v1);
597         }
598
599         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
600                                      LUSTRE_OPC_ANY, NULL);
601         if (IS_ERR(op_data))
602                 RETURN(PTR_ERR(op_data));
603
604         /* swabbing is done in lov_setstripe() on server side */
605         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
606                         NULL, 0, &req, NULL);
607         ll_finish_md_op_data(op_data);
608         ptlrpc_req_finished(req);
609         if (rc) {
610                 if (rc != -EPERM && rc != -EACCES)
611                         CERROR("mdc_setattr fails: rc = %d\n", rc);
612         }
613
614         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
615          LOV_USER_MAGIC_V3 have the same initial fields so we do not
616          need the make the distiction between the 2 versions */
617         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
618                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
619
620                 /* Get fsname and assume devname to be -MDT0000. */
621                 fsname = ll_get_fsname(inode);
622                 /* Set root stripesize */
623                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
624                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
625                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
626                 if (rc)
627                         goto end;
628
629                 /* Set root stripecount */
630                 sprintf(param, "%s-MDT0000.lov.stripecount=%hd", fsname,
631                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
632                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
633                 if (rc)
634                         goto end;
635
636                 /* Set root stripeoffset */
637                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%hd", fsname,
638                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
639                         (typeof(lump->lmm_stripe_offset))(-1));
640                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
641                 if (rc)
642                         goto end;
643 end:
644                 if (fsname)
645                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
646                 if (param)
647                         OBD_FREE(param, MGS_PARAM_MAXLEN);
648         }
649         return rc;
650 }
651
652 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
653                      int *lmm_size, struct ptlrpc_request **request)
654 {
655         struct ll_sb_info *sbi = ll_i2sbi(inode);
656         struct mdt_body   *body;
657         struct lov_mds_md *lmm = NULL;
658         struct ptlrpc_request *req = NULL;
659         int rc, lmmsize;
660         struct md_op_data *op_data;
661
662         rc = ll_get_max_mdsize(sbi, &lmmsize);
663         if (rc)
664                 RETURN(rc);
665
666         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
667                                      0, lmmsize, LUSTRE_OPC_ANY,
668                                      NULL);
669         if (op_data == NULL)
670                 RETURN(-ENOMEM);
671
672         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
673         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
674         ll_finish_md_op_data(op_data);
675         if (rc < 0) {
676                 CDEBUG(D_INFO, "md_getattr failed on inode "
677                        "%lu/%u: rc %d\n", inode->i_ino,
678                        inode->i_generation, rc);
679                 GOTO(out, rc);
680         }
681
682         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
683         LASSERT(body != NULL);
684
685         lmmsize = body->eadatasize;
686
687         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
688             lmmsize == 0) {
689                 GOTO(out, rc = -ENODATA);
690         }
691
692         lmm = req_capsule_server_sized_get(&req->rq_pill,
693                                            &RMF_MDT_MD, lmmsize);
694         LASSERT(lmm != NULL);
695
696         /*
697          * This is coming from the MDS, so is probably in
698          * little endian.  We convert it to host endian before
699          * passing it to userspace.
700          */
701         /* We don't swab objects for directories */
702         switch (le32_to_cpu(lmm->lmm_magic)) {
703         case LOV_MAGIC_V1:
704                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
705                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
706                 break;
707         case LOV_MAGIC_V3:
708                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
709                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
710                 break;
711         default:
712                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
713                 rc = -EPROTO;
714         }
715 out:
716         *lmmp = lmm;
717         *lmm_size = lmmsize;
718         *request = req;
719         return rc;
720 }
721
722 /*
723  *  Get MDT index for the inode.
724  */
725 int ll_get_mdt_idx(struct inode *inode)
726 {
727         struct ll_sb_info *sbi = ll_i2sbi(inode);
728         struct md_op_data *op_data;
729         int rc, mdtidx;
730         ENTRY;
731
732         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
733                                      0, LUSTRE_OPC_ANY, NULL);
734         if (op_data == NULL)
735                 RETURN(-ENOMEM);
736
737         op_data->op_valid |= OBD_MD_MDTIDX;
738         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
739         mdtidx = op_data->op_mds;
740         ll_finish_md_op_data(op_data);
741         if (rc < 0) {
742                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
743                 RETURN(rc);
744         }
745         return mdtidx;
746 }
747
748 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
749 {
750         void *ptr;
751         int rc;
752
753         OBD_ALLOC(ptr, len);
754         if (ptr == NULL)
755                 return -ENOMEM;
756         if (cfs_copy_from_user(ptr, data, len)) {
757                 OBD_FREE(ptr, len);
758                 return -EFAULT;
759         }
760         rc = obd_iocontrol(cmd, exp, len, data, NULL);
761         OBD_FREE(ptr, len);
762         return rc;
763 }
764
765 static int ll_dir_ioctl(struct inode *inode, struct file *file,
766                         unsigned int cmd, unsigned long arg)
767 {
768         struct ll_sb_info *sbi = ll_i2sbi(inode);
769         struct obd_ioctl_data *data;
770         int rc = 0;
771         ENTRY;
772
773         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
774                inode->i_ino, inode->i_generation, inode, cmd);
775
776         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
777         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
778                 return -ENOTTY;
779
780         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
781         switch(cmd) {
782         case FSFILT_IOC_GETFLAGS:
783         case FSFILT_IOC_SETFLAGS:
784                 RETURN(ll_iocontrol(inode, file, cmd, arg));
785         case FSFILT_IOC_GETVERSION_OLD:
786         case FSFILT_IOC_GETVERSION:
787                 RETURN(put_user(inode->i_generation, (int *)arg));
788         /* We need to special case any other ioctls we want to handle,
789          * to send them to the MDS/OST as appropriate and to properly
790          * network encode the arg field.
791         case FSFILT_IOC_SETVERSION_OLD:
792         case FSFILT_IOC_SETVERSION:
793         */
794         case LL_IOC_GET_MDTIDX: {
795                 int mdtidx;
796
797                 mdtidx = ll_get_mdt_idx(inode);
798                 if (mdtidx < 0)
799                         RETURN(mdtidx);
800
801                 if (put_user((int)mdtidx, (int*)arg))
802                         RETURN(-EFAULT);
803
804                 return 0;
805         }
806         case IOC_MDC_LOOKUP: {
807                 struct ptlrpc_request *request = NULL;
808                 int namelen, len = 0;
809                 char *buf = NULL;
810                 char *filename;
811                 struct md_op_data *op_data;
812
813                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
814                 if (rc)
815                         RETURN(rc);
816                 data = (void *)buf;
817
818                 filename = data->ioc_inlbuf1;
819                 namelen = strlen(filename);
820
821                 if (namelen < 1) {
822                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
823                         GOTO(out_free, rc = -EINVAL);
824                 }
825
826                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
827                                              0, LUSTRE_OPC_ANY, NULL);
828                 if (op_data == NULL)
829                         GOTO(out_free, rc = -ENOMEM);
830
831                 op_data->op_valid = OBD_MD_FLID;
832                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
833                 ll_finish_md_op_data(op_data);
834                 if (rc < 0) {
835                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
836                         GOTO(out_free, rc);
837                 }
838                 ptlrpc_req_finished(request);
839                 EXIT;
840 out_free:
841                 obd_ioctl_freedata(buf, len);
842                 return rc;
843         }
844         case LL_IOC_LOV_SETSTRIPE: {
845                 struct lov_user_md_v3 lumv3;
846                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
847                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
848                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
849
850                 int set_default = 0;
851
852                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
853                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
854                         sizeof(lumv3p->lmm_objects[0]));
855                 /* first try with v1 which is smaller than v3 */
856                 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
857                         RETURN(-EFAULT);
858
859                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
860                         if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
861                                 RETURN(-EFAULT);
862                 }
863
864                 if (inode->i_sb->s_root == file->f_dentry)
865                         set_default = 1;
866
867                 /* in v1 and v3 cases lumv1 points to data */
868                 rc = ll_dir_setstripe(inode, lumv1, set_default);
869
870                 RETURN(rc);
871         }
872         case LL_IOC_OBD_STATFS:
873                 RETURN(ll_obd_statfs(inode, (void *)arg));
874         case LL_IOC_LOV_GETSTRIPE:
875         case LL_IOC_MDC_GETINFO:
876         case IOC_MDC_GETFILEINFO:
877         case IOC_MDC_GETFILESTRIPE: {
878                 struct ptlrpc_request *request = NULL;
879                 struct lov_user_md *lump;
880                 struct lov_mds_md *lmm = NULL;
881                 struct mdt_body *body;
882                 char *filename = NULL;
883                 int lmmsize;
884
885                 if (cmd == IOC_MDC_GETFILEINFO ||
886                     cmd == IOC_MDC_GETFILESTRIPE) {
887                         filename = getname((const char *)arg);
888                         if (IS_ERR(filename))
889                                 RETURN(PTR_ERR(filename));
890
891                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
892                                                       &lmmsize, &request);
893                 } else {
894                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
895                 }
896
897                 if (request) {
898                         body = req_capsule_server_get(&request->rq_pill,
899                                                       &RMF_MDT_BODY);
900                         LASSERT(body != NULL);
901                 } else {
902                         GOTO(out_req, rc);
903                 }
904
905                 if (rc < 0) {
906                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
907                                                cmd == LL_IOC_MDC_GETINFO))
908                                 GOTO(skip_lmm, rc = 0);
909                         else
910                                 GOTO(out_req, rc);
911                 }
912
913                 if (cmd == IOC_MDC_GETFILESTRIPE ||
914                     cmd == LL_IOC_LOV_GETSTRIPE) {
915                         lump = (struct lov_user_md *)arg;
916                 } else {
917                         struct lov_user_mds_data *lmdp;
918                         lmdp = (struct lov_user_mds_data *)arg;
919                         lump = &lmdp->lmd_lmm;
920                 }
921                 if (cfs_copy_to_user(lump, lmm, lmmsize)) {
922                         if (cfs_copy_to_user(lump, lmm, sizeof(*lump)))
923                                 GOTO(out_req, rc = -EFAULT);
924                         rc = -EOVERFLOW;
925                 }
926         skip_lmm:
927                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
928                         struct lov_user_mds_data *lmdp;
929                         lstat_t st = { 0 };
930
931                         st.st_dev     = inode->i_sb->s_dev;
932                         st.st_mode    = body->mode;
933                         st.st_nlink   = body->nlink;
934                         st.st_uid     = body->uid;
935                         st.st_gid     = body->gid;
936                         st.st_rdev    = body->rdev;
937                         st.st_size    = body->size;
938                         st.st_blksize = CFS_PAGE_SIZE;
939                         st.st_blocks  = body->blocks;
940                         st.st_atime   = body->atime;
941                         st.st_mtime   = body->mtime;
942                         st.st_ctime   = body->ctime;
943                         st.st_ino     = inode->i_ino;
944
945                         lmdp = (struct lov_user_mds_data *)arg;
946                         if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
947                                 GOTO(out_req, rc = -EFAULT);
948                 }
949
950                 EXIT;
951         out_req:
952                 ptlrpc_req_finished(request);
953                 if (filename)
954                         putname(filename);
955                 return rc;
956         }
957         case IOC_LOV_GETINFO: {
958                 struct lov_user_mds_data *lumd;
959                 struct lov_stripe_md *lsm;
960                 struct lov_user_md *lum;
961                 struct lov_mds_md *lmm;
962                 int lmmsize;
963                 lstat_t st;
964
965                 lumd = (struct lov_user_mds_data *)arg;
966                 lum = &lumd->lmd_lmm;
967
968                 rc = ll_get_max_mdsize(sbi, &lmmsize);
969                 if (rc)
970                         RETURN(rc);
971
972                 OBD_ALLOC(lmm, lmmsize);
973                 if (cfs_copy_from_user(lmm, lum, lmmsize))
974                         GOTO(free_lmm, rc = -EFAULT);
975
976                 switch (lmm->lmm_magic) {
977                 case LOV_USER_MAGIC_V1:
978                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
979                                 break;
980                         /* swab objects first so that stripes num will be sane */
981                         lustre_swab_lov_user_md_objects(
982                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
983                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
984                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
985                         break;
986                 case LOV_USER_MAGIC_V3:
987                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
988                                 break;
989                         /* swab objects first so that stripes num will be sane */
990                         lustre_swab_lov_user_md_objects(
991                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
992                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
993                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
994                         break;
995                 default:
996                         GOTO(free_lmm, rc = -EINVAL);
997                 }
998
999                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1000                 if (rc < 0)
1001                         GOTO(free_lmm, rc = -ENOMEM);
1002
1003                 /* Perform glimpse_size operation. */
1004                 memset(&st, 0, sizeof(st));
1005
1006                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1007                 if (rc)
1008                         GOTO(free_lsm, rc);
1009
1010                 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1011                         GOTO(free_lsm, rc = -EFAULT);
1012
1013                 EXIT;
1014         free_lsm:
1015                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1016         free_lmm:
1017                 OBD_FREE(lmm, lmmsize);
1018                 return rc;
1019         }
1020         case OBD_IOC_LLOG_CATINFO: {
1021                 struct ptlrpc_request *req = NULL;
1022                 char                  *buf = NULL;
1023                 char                  *str;
1024                 int                    len = 0;
1025
1026                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1027                 if (rc)
1028                         RETURN(rc);
1029                 data = (void *)buf;
1030
1031                 if (!data->ioc_inlbuf1) {
1032                         obd_ioctl_freedata(buf, len);
1033                         RETURN(-EINVAL);
1034                 }
1035
1036                 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
1037                                            &RQF_LLOG_CATINFO);
1038                 if (req == NULL)
1039                         GOTO(out_catinfo, rc = -ENOMEM);
1040
1041                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
1042                                      data->ioc_inllen1);
1043                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
1044                                      data->ioc_inllen2);
1045
1046                 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
1047                 if (rc) {
1048                         ptlrpc_request_free(req);
1049                         GOTO(out_catinfo, rc);
1050                 }
1051
1052                 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
1053                 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
1054                 if (data->ioc_inllen2) {
1055                         str = req_capsule_client_get(&req->rq_pill,
1056                                                      &RMF_STRING);
1057                         memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
1058                 }
1059
1060                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
1061                                      data->ioc_plen1);
1062                 ptlrpc_request_set_replen(req);
1063
1064                 rc = ptlrpc_queue_wait(req);
1065                 if (!rc) {
1066                         str = req_capsule_server_get(&req->rq_pill,
1067                                                      &RMF_STRING);
1068                         if (cfs_copy_to_user(data->ioc_pbuf1, str,
1069                                              data->ioc_plen1))
1070                                 rc = -EFAULT;
1071                 }
1072                 ptlrpc_req_finished(req);
1073         out_catinfo:
1074                 obd_ioctl_freedata(buf, len);
1075                 RETURN(rc);
1076         }
1077         case OBD_IOC_QUOTACHECK: {
1078                 struct obd_quotactl *oqctl;
1079                 int error = 0;
1080
1081                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1082                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1083                         RETURN(-EPERM);
1084
1085                 OBD_ALLOC_PTR(oqctl);
1086                 if (!oqctl)
1087                         RETURN(-ENOMEM);
1088                 oqctl->qc_type = arg;
1089                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1090                 if (rc < 0) {
1091                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1092                         error = rc;
1093                 }
1094
1095                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1096                 if (rc < 0)
1097                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1098
1099                 OBD_FREE_PTR(oqctl);
1100                 return error ?: rc;
1101         }
1102         case OBD_IOC_POLL_QUOTACHECK: {
1103                 struct if_quotacheck *check;
1104
1105                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1106                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1107                         RETURN(-EPERM);
1108
1109                 OBD_ALLOC_PTR(check);
1110                 if (!check)
1111                         RETURN(-ENOMEM);
1112
1113                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1114                                    NULL);
1115                 if (rc) {
1116                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1117                         if (cfs_copy_to_user((void *)arg, check,
1118                                              sizeof(*check)))
1119                                 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1120                         GOTO(out_poll, rc);
1121                 }
1122
1123                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1124                                    NULL);
1125                 if (rc) {
1126                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1127                         if (cfs_copy_to_user((void *)arg, check,
1128                                              sizeof(*check)))
1129                                 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1130                         GOTO(out_poll, rc);
1131                 }
1132         out_poll:
1133                 OBD_FREE_PTR(check);
1134                 RETURN(rc);
1135         }
1136         case OBD_IOC_QUOTACTL: {
1137                 struct if_quotactl *qctl;
1138                 int cmd, type, id, valid;
1139
1140                 OBD_ALLOC_PTR(qctl);
1141                 if (!qctl)
1142                         RETURN(-ENOMEM);
1143
1144                 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1145                         GOTO(out_quotactl, rc = -EFAULT);
1146
1147                 cmd = qctl->qc_cmd;
1148                 type = qctl->qc_type;
1149                 id = qctl->qc_id;
1150                 valid = qctl->qc_valid;
1151
1152                 switch (cmd) {
1153                 case LUSTRE_Q_INVALIDATE:
1154                 case LUSTRE_Q_FINVALIDATE:
1155                 case Q_QUOTAON:
1156                 case Q_QUOTAOFF:
1157                 case Q_SETQUOTA:
1158                 case Q_SETINFO:
1159                         if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1160                             sbi->ll_flags & LL_SBI_RMT_CLIENT)
1161                                 GOTO(out_quotactl, rc = -EPERM);
1162                         break;
1163                 case Q_GETQUOTA:
1164                         if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1165                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1166                             (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1167                              sbi->ll_flags & LL_SBI_RMT_CLIENT))
1168                                 GOTO(out_quotactl, rc = -EPERM);
1169                         break;
1170                 case Q_GETINFO:
1171                         break;
1172                 default:
1173                         CERROR("unsupported quotactl op: %#x\n", cmd);
1174                         GOTO(out_quotactl, rc = -ENOTTY);
1175                 }
1176
1177                 if (valid != QC_GENERAL) {
1178                         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1179                                 GOTO(out_quotactl, rc = -EOPNOTSUPP);
1180
1181                         if (cmd == Q_GETINFO)
1182                                 qctl->qc_cmd = Q_GETOINFO;
1183                         else if (cmd == Q_GETQUOTA)
1184                                 qctl->qc_cmd = Q_GETOQUOTA;
1185                         else
1186                                 GOTO(out_quotactl, rc = -EINVAL);
1187
1188                         switch (valid) {
1189                         case QC_MDTIDX:
1190                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1191                                                    sbi->ll_md_exp,
1192                                                    sizeof(*qctl), qctl, NULL);
1193                                 break;
1194                         case QC_OSTIDX:
1195                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1196                                                    sbi->ll_dt_exp,
1197                                                    sizeof(*qctl), qctl, NULL);
1198                                 break;
1199                         case QC_UUID:
1200                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1201                                                    sbi->ll_md_exp,
1202                                                    sizeof(*qctl), qctl, NULL);
1203                                 if (rc == -EAGAIN)
1204                                         rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1205                                                            sbi->ll_dt_exp,
1206                                                            sizeof(*qctl), qctl,
1207                                                            NULL);
1208                                 break;
1209                         default:
1210                                 rc = -EINVAL;
1211                                 break;
1212                         }
1213
1214                         if (rc)
1215                                 GOTO(out_quotactl, rc);
1216                         else
1217                                 qctl->qc_cmd = cmd;
1218                 } else {
1219                         struct obd_quotactl *oqctl;
1220
1221                         OBD_ALLOC_PTR(oqctl);
1222                         if (!oqctl)
1223                                 GOTO(out_quotactl, rc = -ENOMEM);
1224
1225                         QCTL_COPY(oqctl, qctl);
1226                         rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1227                         if (rc) {
1228                                 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1229                                         oqctl->qc_cmd = Q_QUOTAOFF;
1230                                         obd_quotactl(sbi->ll_md_exp, oqctl);
1231                                 }
1232                                 OBD_FREE_PTR(oqctl);
1233                                 GOTO(out_quotactl, rc);
1234                         } else {
1235                                 QCTL_COPY(qctl, oqctl);
1236                                 OBD_FREE_PTR(oqctl);
1237                         }
1238                 }
1239
1240                 if (cfs_copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1241                         rc = -EFAULT;
1242
1243         out_quotactl:
1244                 OBD_FREE_PTR(qctl);
1245                 RETURN(rc);
1246         }
1247         case OBD_IOC_GETNAME: {
1248                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1249                 if (!obd)
1250                         RETURN(-EFAULT);
1251                 if (cfs_copy_to_user((void *)arg, obd->obd_name,
1252                                      strlen(obd->obd_name) + 1))
1253                         RETURN (-EFAULT);
1254                 RETURN(0);
1255         }
1256         case LL_IOC_FLUSHCTX:
1257                 RETURN(ll_flush_ctx(inode));
1258 #ifdef CONFIG_FS_POSIX_ACL
1259         case LL_IOC_RMTACL: {
1260             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1261                 inode == inode->i_sb->s_root->d_inode) {
1262                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1263
1264                 LASSERT(fd != NULL);
1265                 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1266                 if (!rc)
1267                         fd->fd_flags |= LL_FILE_RMTACL;
1268                 RETURN(rc);
1269             } else
1270                 RETURN(0);
1271         }
1272 #endif
1273         case LL_IOC_GETOBDCOUNT: {
1274                 int count;
1275
1276                 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1277                         RETURN(-EFAULT);
1278
1279                 if (!count) {
1280                         /* get ost count */
1281                         struct lov_obd *lov = &sbi->ll_dt_exp->exp_obd->u.lov;
1282                         count = lov->desc.ld_tgt_count;
1283                 } else {
1284                         /* get mdt count */
1285                         struct lmv_obd *lmv = &sbi->ll_md_exp->exp_obd->u.lmv;
1286                         count = lmv->desc.ld_tgt_count;
1287                 }
1288
1289                 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1290                         RETURN(-EFAULT);
1291
1292                 RETURN(0);
1293         }
1294         case LL_IOC_PATH2FID:
1295                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1296                                      sizeof(struct lu_fid)))
1297                         RETURN(-EFAULT);
1298                 RETURN(0);
1299         case LL_IOC_GET_CONNECT_FLAGS: {
1300                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1301         }
1302         case OBD_IOC_CHANGELOG_SEND:
1303         case OBD_IOC_CHANGELOG_CLEAR:
1304                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1305                                     sizeof(struct ioc_changelog));
1306                 RETURN(rc);
1307         case OBD_IOC_FID2PATH:
1308                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1309         case LL_IOC_HSM_CT_START:
1310                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1311                                     sizeof(struct lustre_kernelcomm));
1312                 RETURN(rc);
1313
1314         default:
1315                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1316         }
1317 }
1318
1319 int ll_dir_open(struct inode *inode, struct file *file)
1320 {
1321         ENTRY;
1322         RETURN(ll_file_open(inode, file));
1323 }
1324
1325 int ll_dir_release(struct inode *inode, struct file *file)
1326 {
1327         ENTRY;
1328         RETURN(ll_file_release(inode, file));
1329 }
1330
1331 struct file_operations ll_dir_operations = {
1332         .open     = ll_dir_open,
1333         .release  = ll_dir_release,
1334         .read     = generic_read_dir,
1335         .readdir  = ll_readdir,
1336         .ioctl    = ll_dir_ioctl
1337 };