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