Whamcloud - gitweb
LU-11025 dne: directory restripe and auto split
[fs/lustre-release.git] / lustre / llite / dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/dir.c
33  *
34  * Directory code for lustre client.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mm.h>
40 #include <linux/version.h>
41 #include <linux/security.h>
42 #include <linux/user_namespace.h>
43 #include <linux/uidgid.h>
44 #include <linux/uaccess.h>
45 #include <linux/buffer_head.h>   // for wait_on_buffer
46 #include <linux/pagevec.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lib.h>
54 #include <lustre_dlm.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
58
59 #include "llite_internal.h"
60
61 /*
62  * (new) readdir implementation overview.
63  *
64  * Original lustre readdir implementation cached exact copy of raw directory
65  * pages on the client. These pages were indexed in client page cache by
66  * logical offset in the directory file. This design, while very simple and
67  * intuitive had some inherent problems:
68  *
69  *     . it implies that byte offset to the directory entry serves as a
70  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71  *     ext3/htree directory entries may move due to splits, and more
72  *     importantly,
73  *
74  *     . it is incompatible with the design of split directories for cmd3,
75  *     that assumes that names are distributed across nodes based on their
76  *     hash, and so readdir should be done in hash order.
77  *
78  * New readdir implementation does readdir in hash order, and uses hash of a
79  * file name as a telldir/seekdir cookie. This led to number of complications:
80  *
81  *     . hash is not unique, so it cannot be used to index cached directory
82  *     pages on the client (note, that it requires a whole pageful of hash
83  *     collided entries to cause two pages to have identical hashes);
84  *
85  *     . hash is not unique, so it cannot, strictly speaking, be used as an
86  *     entry cookie. ext3/htree has the same problem and lustre implementation
87  *     mimics their solution: seekdir(hash) positions directory at the first
88  *     entry with the given hash.
89  *
90  * Client side.
91  *
92  * 0. caching
93  *
94  * Client caches directory pages using hash of the first entry as an index. As
95  * noted above hash is not unique, so this solution doesn't work as is:
96  * special processing is needed for "page hash chains" (i.e., sequences of
97  * pages filled with entries all having the same hash value).
98  *
99  * First, such chains have to be detected. To this end, server returns to the
100  * client the hash of the first entry on the page next to one returned. When
101  * client detects that this hash is the same as hash of the first entry on the
102  * returned page, page hash collision has to be handled. Pages in the
103  * hash chain, except first one, are termed "overflow pages".
104  *
105  * Solution to index uniqueness problem is to not cache overflow
106  * pages. Instead, when page hash collision is detected, all overflow pages
107  * from emerging chain are immediately requested from the server and placed in
108  * a special data structure (struct ll_dir_chain). This data structure is used
109  * by ll_readdir() to process entries from overflow pages. When readdir
110  * invocation finishes, overflow pages are discarded. If page hash collision
111  * chain weren't completely processed, next call to readdir will again detect
112  * page hash collision, again read overflow pages in, process next portion of
113  * entries and again discard the pages. This is not as wasteful as it looks,
114  * because, given reasonable hash, page hash collisions are extremely rare.
115  *
116  * 1. directory positioning
117  *
118  * When seekdir(hash) is called, original
119  *
120  *
121  *
122  *
123  *
124  *
125  *
126  *
127  * Server.
128  *
129  * identification of and access to overflow pages
130  *
131  * page format
132  *
133  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
134  * a header lu_dirpage which describes the start/end hash, and whether this
135  * page is empty (contains no dir entry) or hash collide with next page.
136  * After client receives reply, several pages will be integrated into dir page
137  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
138  * lu_dirpage for this integrated page will be adjusted. See
139  * mdc_adjust_dirpages().
140  *
141  */
142 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
143                              __u64 offset, struct ll_dir_chain *chain)
144 {
145         struct md_callback      cb_op;
146         struct page             *page;
147         int                     rc;
148
149         cb_op.md_blocking_ast = ll_md_blocking_ast;
150         rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
151         if (rc != 0)
152                 return ERR_PTR(rc);
153
154         return page;
155 }
156
157 void ll_release_page(struct inode *inode, struct page *page,
158                      bool remove)
159 {
160         kunmap(page);
161
162         /* Always remove the page for striped dir, because the page is
163          * built from temporarily in LMV layer */
164         if (inode && ll_dir_striped(inode)) {
165                 __free_page(page);
166                 return;
167         }
168
169         if (remove) {
170                 lock_page(page);
171                 if (likely(page->mapping != NULL))
172                         delete_from_page_cache(page);
173                 unlock_page(page);
174         }
175         put_page(page);
176 }
177
178 #ifdef HAVE_DIR_CONTEXT
179 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
180                 struct dir_context *ctx)
181 {
182 #else
183 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
184                 void *cookie, filldir_t filldir)
185 {
186 #endif
187         struct ll_sb_info    *sbi        = ll_i2sbi(inode);
188         __u64                 pos        = *ppos;
189         bool                  is_api32 = ll_need_32bit_api(sbi);
190         bool                  is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
191         struct page          *page;
192         struct ll_dir_chain   chain;
193         bool                  done = false;
194         int                   rc = 0;
195         ENTRY;
196
197         ll_dir_chain_init(&chain);
198
199         page = ll_get_dir_page(inode, op_data, pos, &chain);
200
201         while (rc == 0 && !done) {
202                 struct lu_dirpage *dp;
203                 struct lu_dirent  *ent;
204                 __u64 hash;
205                 __u64 next;
206
207                 if (IS_ERR(page)) {
208                         rc = PTR_ERR(page);
209                         break;
210                 }
211
212                 hash = MDS_DIR_END_OFF;
213                 dp = page_address(page);
214                 for (ent = lu_dirent_start(dp); ent != NULL && !done;
215                      ent = lu_dirent_next(ent)) {
216                         __u16          type;
217                         int            namelen;
218                         struct lu_fid  fid;
219                         __u64          lhash;
220                         __u64          ino;
221
222                         hash = le64_to_cpu(ent->lde_hash);
223                         if (hash < pos) /* Skip until we find target hash */
224                                 continue;
225
226                         namelen = le16_to_cpu(ent->lde_namelen);
227                         if (namelen == 0) /* Skip dummy record */
228                                 continue;
229
230                         if (is_api32 && is_hash64)
231                                 lhash = hash >> 32;
232                         else
233                                 lhash = hash;
234                         fid_le_to_cpu(&fid, &ent->lde_fid);
235                         ino = cl_fid_build_ino(&fid, is_api32);
236                         type = IFTODT(lu_dirent_type_get(ent));
237                         /* For ll_nfs_get_name_filldir(), it will try to access
238                          * 'ent' through 'lde_name', so the parameter 'name'
239                          * for 'filldir()' must be part of the 'ent'. */
240 #ifdef HAVE_DIR_CONTEXT
241                         ctx->pos = lhash;
242                         done = !dir_emit(ctx, ent->lde_name, namelen, ino,
243                                          type);
244 #else
245                         done = filldir(cookie, ent->lde_name, namelen, lhash,
246                                        ino, type);
247 #endif
248                 }
249
250                 if (done) {
251                         pos = hash;
252                         ll_release_page(inode, page, false);
253                         break;
254                 }
255
256                 next = le64_to_cpu(dp->ldp_hash_end);
257                 pos = next;
258                 if (pos == MDS_DIR_END_OFF) {
259                         /*
260                          * End of directory reached.
261                          */
262                         done = 1;
263                         ll_release_page(inode, page, false);
264                 } else {
265                         /*
266                          * Normal case: continue to the next
267                          * page.
268                          */
269                         ll_release_page(inode, page,
270                                         le32_to_cpu(dp->ldp_flags) &
271                                         LDF_COLLIDE);
272                         next = pos;
273                         page = ll_get_dir_page(inode, op_data, pos,
274                                                &chain);
275                 }
276         }
277 #ifdef HAVE_DIR_CONTEXT
278         ctx->pos = pos;
279 #else
280         *ppos = pos;
281 #endif
282         ll_dir_chain_fini(&chain);
283         RETURN(rc);
284 }
285
286 #ifdef HAVE_DIR_CONTEXT
287 static int ll_iterate(struct file *filp, struct dir_context *ctx)
288 #else
289 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
290 #endif
291 {
292         struct inode *inode = file_inode(filp);
293         struct ll_file_data *lfd = filp->private_data;
294         struct ll_sb_info *sbi = ll_i2sbi(inode);
295         int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
296         int api32 = ll_need_32bit_api(sbi);
297         struct md_op_data *op_data;
298         struct lu_fid pfid = { 0 };
299         ktime_t kstart = ktime_get();
300         __u64 pos;
301         int rc;
302         ENTRY;
303
304         if (lfd != NULL)
305                 pos = lfd->lfd_pos;
306         else
307                 pos = 0;
308
309         CDEBUG(D_VFSTRACE,
310                "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
311                PFID(ll_inode2fid(inode)),
312                inode, (unsigned long)pos, i_size_read(inode), api32);
313
314         if (pos == MDS_DIR_END_OFF)
315                 /*
316                  * end-of-file.
317                  */
318                 GOTO(out, rc = 0);
319
320         if (unlikely(ll_dir_striped(inode))) {
321                 /*
322                  * This is only needed for striped dir to fill ..,
323                  * see lmv_read_page()
324                  */
325                 if (file_dentry(filp)->d_parent != NULL &&
326                     file_dentry(filp)->d_parent->d_inode != NULL) {
327                         __u64 ibits = MDS_INODELOCK_LOOKUP;
328                         struct inode *parent =
329                                 file_dentry(filp)->d_parent->d_inode;
330
331                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
332                                 pfid = *ll_inode2fid(parent);
333                 }
334
335                 /* If it can not find in cache, do lookup .. on the master
336                  * object */
337                 if (fid_is_zero(&pfid)) {
338                         rc = ll_dir_get_parent_fid(inode, &pfid);
339                         if (rc != 0)
340                                 RETURN(rc);
341                 }
342         }
343
344         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
345                                      LUSTRE_OPC_ANY, inode);
346         if (IS_ERR(op_data))
347                 GOTO(out, rc = PTR_ERR(op_data));
348
349         /* foreign dirs are browsed out of Lustre */
350         if (unlikely(op_data->op_mea1 != NULL &&
351                      op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
352                 ll_finish_md_op_data(op_data);
353                 RETURN(-ENODATA);
354         }
355
356         op_data->op_fid3 = pfid;
357
358 #ifdef HAVE_DIR_CONTEXT
359         ctx->pos = pos;
360         rc = ll_dir_read(inode, &pos, op_data, ctx);
361         pos = ctx->pos;
362 #else
363         rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
364 #endif
365         if (lfd != NULL)
366                 lfd->lfd_pos = pos;
367
368         if (pos == MDS_DIR_END_OFF) {
369                 if (api32)
370                         pos = LL_DIR_END_OFF_32BIT;
371                 else
372                         pos = LL_DIR_END_OFF;
373         } else {
374                 if (api32 && hash64)
375                         pos = pos >> 32;
376         }
377 #ifdef HAVE_DIR_CONTEXT
378         ctx->pos = pos;
379 #else
380         filp->f_pos = pos;
381 #endif
382         ll_finish_md_op_data(op_data);
383
384 out:
385         if (!rc)
386                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
387                                    ktime_us_delta(ktime_get(), kstart));
388
389         RETURN(rc);
390 }
391
392 /**
393  * Create striped directory with specified stripe(@lump)
394  *
395  * \param[in] dparent   the parent of the directory.
396  * \param[in] lump      the specified stripes.
397  * \param[in] dirname   the name of the directory.
398  * \param[in] mode      the specified mode of the directory.
399  *
400  * \retval              =0 if striped directory is being created successfully.
401  *                      <0 if the creation is failed.
402  */
403 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
404                                size_t len, const char *dirname, umode_t mode)
405 {
406         struct inode *parent = dparent->d_inode;
407         struct ptlrpc_request *request = NULL;
408         struct md_op_data *op_data;
409         struct ll_sb_info *sbi = ll_i2sbi(parent);
410         struct inode *inode = NULL;
411         struct dentry dentry = {
412                 .d_parent = dparent,
413                 .d_name = {
414                         .name = dirname,
415                         .len = strlen(dirname),
416                         .hash = ll_full_name_hash(dparent, dirname,
417                                                   strlen(dirname)),
418                 },
419         };
420         int err;
421         ENTRY;
422
423         if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
424                 RETURN(-EINVAL);
425
426         if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
427                 CDEBUG(D_VFSTRACE,
428                        "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
429                        PFID(ll_inode2fid(parent)), parent, dirname,
430                        (int)lump->lum_stripe_offset, lump->lum_stripe_count);
431         } else {
432                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
433
434                 CDEBUG(D_VFSTRACE,
435                        "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
436                        PFID(ll_inode2fid(parent)), parent, dirname,
437                        lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
438         }
439
440         if (lump->lum_stripe_count > 1 &&
441             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
442                 RETURN(-EINVAL);
443
444         if (IS_DEADDIR(parent) &&
445             !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
446                 RETURN(-ENOENT);
447
448         if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
449                 if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
450                      LMV_HASH_TYPE_CRUSH) {
451                         /* if server doesn't support 'crush' hash type,
452                          * switch to fnv_1a_64.
453                          */
454                         lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
455                         lump->lum_hash_type |= LMV_HASH_TYPE_FNV_1A_64;
456                 } else if ((lump->lum_hash_type & LMV_HASH_TYPE_MASK) ==
457                      LMV_HASH_TYPE_UNKNOWN) {
458                         /* from 2.14 MDT will choose default hash type if client
459                          * doesn't set a valid one, while old server doesn't
460                          * handle it.
461                          */
462                         lump->lum_hash_type &= ~LMV_HASH_TYPE_MASK;
463                         lump->lum_hash_type |= LMV_HASH_TYPE_DEFAULT;
464                 }
465         }
466
467         if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
468                 lustre_swab_lmv_user_md(lump);
469
470         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
471                 mode &= ~current_umask();
472         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
473         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
474                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
475                                      lump);
476         if (IS_ERR(op_data))
477                 RETURN(PTR_ERR(op_data));
478
479         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
480                 /* selinux_dentry_init_security() uses dentry->d_parent and name
481                  * to determine the security context for the file. So our fake
482                  * dentry should be real enough for this purpose. */
483                 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
484                                               &op_data->op_file_secctx_name,
485                                               &op_data->op_file_secctx,
486                                               &op_data->op_file_secctx_size);
487                 if (err < 0)
488                         GOTO(out_op_data, err);
489         }
490
491         op_data->op_cli_flags |= CLI_SET_MEA;
492         err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
493                         from_kuid(&init_user_ns, current_fsuid()),
494                         from_kgid(&init_user_ns, current_fsgid()),
495                         cfs_curproc_cap_pack(), 0, &request);
496         if (err)
497                 GOTO(out_request, err);
498
499         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
500
501         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
502         if (err)
503                 GOTO(out_inode, err);
504
505         dentry.d_inode = inode;
506
507         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
508                 inode_lock(inode);
509                 err = security_inode_notifysecctx(inode,
510                                                   op_data->op_file_secctx,
511                                                   op_data->op_file_secctx_size);
512                 inode_unlock(inode);
513         } else {
514                 err = ll_inode_init_security(&dentry, inode, parent);
515         }
516         if (err)
517                 GOTO(out_inode, err);
518
519 out_inode:
520         if (inode != NULL)
521                 iput(inode);
522 out_request:
523         ptlrpc_req_finished(request);
524 out_op_data:
525         ll_finish_md_op_data(op_data);
526
527         return err;
528 }
529
530 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
531                      int set_default)
532 {
533         struct ll_sb_info *sbi = ll_i2sbi(inode);
534         struct md_op_data *op_data;
535         struct ptlrpc_request *req = NULL;
536         int rc = 0;
537         int lum_size;
538         ENTRY;
539
540         if (lump != NULL) {
541                 switch (lump->lmm_magic) {
542                 case LOV_USER_MAGIC_V1:
543                         lum_size = sizeof(struct lov_user_md_v1);
544                         break;
545                 case LOV_USER_MAGIC_V3:
546                         lum_size = sizeof(struct lov_user_md_v3);
547                         break;
548                 case LOV_USER_MAGIC_COMP_V1:
549                         lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
550                         break;
551                 case LMV_USER_MAGIC:
552                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
553                                 lustre_swab_lmv_user_md(
554                                         (struct lmv_user_md *)lump);
555                         lum_size = sizeof(struct lmv_user_md);
556                         break;
557                 case LOV_USER_MAGIC_SPECIFIC: {
558                         struct lov_user_md_v3 *v3 =
559                                 (struct lov_user_md_v3 *)lump;
560                         if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
561                                 RETURN(-EINVAL);
562                         lum_size = lov_user_md_size(v3->lmm_stripe_count,
563                                                     LOV_USER_MAGIC_SPECIFIC);
564                         break;
565                 }
566                 default:
567                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
568                                         " %#08x != %#08x nor %#08x\n",
569                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
570                                         LOV_USER_MAGIC_V3);
571                         RETURN(-EINVAL);
572                 }
573
574                 /*
575                  * This is coming from userspace, so should be in
576                  * local endian.  But the MDS would like it in little
577                  * endian, so we swab it before we send it.
578                  */
579                 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
580                     le32_to_cpu(LOV_MAGIC_MAGIC))
581                         lustre_swab_lov_user_md(lump, 0);
582         } else {
583                 lum_size = sizeof(struct lov_user_md_v1);
584         }
585
586         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
587                                      LUSTRE_OPC_ANY, NULL);
588         if (IS_ERR(op_data))
589                 RETURN(PTR_ERR(op_data));
590
591         /* swabbing is done in lov_setstripe() on server side */
592         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
593         ll_finish_md_op_data(op_data);
594         ptlrpc_req_finished(req);
595         if (rc)
596                 RETURN(rc);
597
598         RETURN(rc);
599 }
600
601 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
602                                      int *plmm_size,
603                                      struct ptlrpc_request **request, u64 valid,
604                                      enum get_default_layout_type type)
605 {
606         struct ll_sb_info *sbi = ll_i2sbi(inode);
607         struct mdt_body   *body;
608         struct lov_mds_md *lmm = NULL;
609         struct ptlrpc_request *req = NULL;
610         int rc, lmm_size;
611         struct md_op_data *op_data;
612         struct lu_fid fid;
613         ENTRY;
614
615         rc = ll_get_default_mdsize(sbi, &lmm_size);
616         if (rc)
617                 RETURN(rc);
618
619         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
620                                      0, lmm_size, LUSTRE_OPC_ANY,
621                                      NULL);
622         if (IS_ERR(op_data))
623                 RETURN(PTR_ERR(op_data));
624
625         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
626
627         if (type == GET_DEFAULT_LAYOUT_ROOT) {
628                 lu_root_fid(&op_data->op_fid1);
629                 fid = op_data->op_fid1;
630         } else {
631                 fid = *ll_inode2fid(inode);
632         }
633
634         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
635         ll_finish_md_op_data(op_data);
636         if (rc < 0) {
637                 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
638                        PFID(&fid), rc);
639                 GOTO(out, rc);
640         }
641
642         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
643         LASSERT(body != NULL);
644
645         lmm_size = body->mbo_eadatasize;
646
647         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
648             lmm_size == 0) {
649                 GOTO(out, rc = -ENODATA);
650         }
651
652         lmm = req_capsule_server_sized_get(&req->rq_pill,
653                                            &RMF_MDT_MD, lmm_size);
654         LASSERT(lmm != NULL);
655
656         /*
657          * This is coming from the MDS, so is probably in
658          * little endian.  We convert it to host endian before
659          * passing it to userspace.
660          */
661         /* We don't swab objects for directories */
662         switch (le32_to_cpu(lmm->lmm_magic)) {
663         case LOV_MAGIC_V1:
664         case LOV_MAGIC_V3:
665         case LOV_MAGIC_COMP_V1:
666         case LOV_USER_MAGIC_SPECIFIC:
667                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
668                         lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
669                 break;
670         case LMV_MAGIC_V1:
671                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
672                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
673                 break;
674         case LMV_USER_MAGIC:
675                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
676                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
677                 break;
678         case LMV_MAGIC_FOREIGN: {
679                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
680
681                 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
682                         __swab32s(&lfm->lfm_magic);
683                         __swab32s(&lfm->lfm_length);
684                         __swab32s(&lfm->lfm_type);
685                         __swab32s(&lfm->lfm_flags);
686                 }
687                 break;
688         }
689         default:
690                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
691                 rc = -EPROTO;
692         }
693 out:
694         *plmm = lmm;
695         *plmm_size = lmm_size;
696         *request = req;
697         return rc;
698 }
699
700 /**
701  * This function will be used to get default LOV/LMV/Default LMV
702  * @valid will be used to indicate which stripe it will retrieve.
703  * If the directory does not have its own default layout, then the
704  * function will request the default layout from root FID.
705  *      OBD_MD_MEA              LMV stripe EA
706  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
707  *      otherwise               Default LOV EA.
708  * Each time, it can only retrieve 1 stripe EA
709  **/
710 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
711                              struct ptlrpc_request **request,
712                              struct ptlrpc_request **root_request,
713                              u64 valid)
714 {
715         struct ptlrpc_request *req = NULL;
716         struct ptlrpc_request *root_req = NULL;
717         struct lov_mds_md *lmm = NULL;
718         int lmm_size = 0;
719         int rc = 0;
720         ENTRY;
721
722         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
723                                        &req, valid, 0);
724         if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
725             !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
726                 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
727                                                &root_req, valid,
728                                                GET_DEFAULT_LAYOUT_ROOT);
729
730         *plmm = lmm;
731         *plmm_size = lmm_size;
732         *request = req;
733         if (root_request != NULL)
734                 *root_request = root_req;
735
736         RETURN(rc);
737 }
738
739 /**
740  * This function will be used to get default LOV/LMV/Default LMV
741  * @valid will be used to indicate which stripe it will retrieve
742  *      OBD_MD_MEA              LMV stripe EA
743  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
744  *      otherwise               Default LOV EA.
745  * Each time, it can only retrieve 1 stripe EA
746  **/
747 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
748                      struct ptlrpc_request **request, u64 valid)
749 {
750         struct ptlrpc_request *req = NULL;
751         struct lov_mds_md *lmm = NULL;
752         int lmm_size = 0;
753         int rc = 0;
754         ENTRY;
755
756         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
757                                        &req, valid, 0);
758
759         *plmm = lmm;
760         *plmm_size = lmm_size;
761         *request = req;
762
763         RETURN(rc);
764 }
765
766 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
767 {
768         struct md_op_data       *op_data;
769         int                     rc;
770         int                     mdt_index;
771         ENTRY;
772
773         OBD_ALLOC_PTR(op_data);
774         if (op_data == NULL)
775                 RETURN(-ENOMEM);
776
777         op_data->op_flags |= MF_GET_MDT_IDX;
778         op_data->op_fid1 = *fid;
779         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
780         mdt_index = op_data->op_mds;
781         OBD_FREE_PTR(op_data);
782         if (rc < 0)
783                 RETURN(rc);
784
785         RETURN(mdt_index);
786 }
787
788 /*
789  *  Get MDT index for the inode.
790  */
791 int ll_get_mdt_idx(struct inode *inode)
792 {
793         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
794 }
795
796 /**
797  * Generic handler to do any pre-copy work.
798  *
799  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
800  * first information for it that real work has started.
801  *
802  * Moreover, for a ARCHIVE request, it will sample the file data version and
803  * store it in \a copy.
804  *
805  * \return 0 on success.
806  */
807 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
808 {
809         struct ll_sb_info               *sbi = ll_s2sbi(sb);
810         struct hsm_progress_kernel       hpk;
811         int                              rc = 0;
812         int                              rc2;
813         ENTRY;
814
815         /* Forge a hsm_progress based on data from copy. */
816         hpk.hpk_fid = copy->hc_hai.hai_fid;
817         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
818         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
819         hpk.hpk_extent.length = 0;
820         hpk.hpk_flags = 0;
821         hpk.hpk_errval = 0;
822         hpk.hpk_data_version = 0;
823
824
825         /* For archive request, we need to read the current file version. */
826         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
827                 struct inode    *inode;
828                 __u64            data_version = 0;
829
830                 /* Get inode for this fid */
831                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
832                 if (IS_ERR(inode)) {
833                         hpk.hpk_flags |= HP_FLAG_RETRY;
834                         /* hpk_errval is >= 0 */
835                         hpk.hpk_errval = -PTR_ERR(inode);
836                         GOTO(progress, rc = PTR_ERR(inode));
837                 }
838
839                 /* Read current file data version */
840                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
841                 iput(inode);
842                 if (rc != 0) {
843                         CDEBUG(D_HSM, "Could not read file data version of "
844                                       DFID" (rc = %d). Archive request ("
845                                       "%#llx) could not be done.\n",
846                                       PFID(&copy->hc_hai.hai_fid), rc,
847                                       copy->hc_hai.hai_cookie);
848                         hpk.hpk_flags |= HP_FLAG_RETRY;
849                         /* hpk_errval must be >= 0 */
850                         hpk.hpk_errval = -rc;
851                         GOTO(progress, rc);
852                 }
853
854                 /* Store in the hsm_copy for later copytool use.
855                  * Always modified even if no lsm. */
856                 copy->hc_data_version = data_version;
857         }
858
859 progress:
860         /* On error, the request should be considered as completed */
861         if (hpk.hpk_errval > 0)
862                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
863
864         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
865                             &hpk, NULL);
866
867         /* Return first error */
868         RETURN(rc != 0 ? rc : rc2);
869 }
870
871 /**
872  * Generic handler to do any post-copy work.
873  *
874  * It will send the last hsm_progress update to coordinator to inform it
875  * that copy is finished and whether it was successful or not.
876  *
877  * Moreover,
878  * - for ARCHIVE request, it will sample the file data version and compare it
879  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
880  *   will be considered as failed.
881  * - for RESTORE request, it will sample the file data version and send it to
882  *   coordinator which is useful if the file was imported as 'released'.
883  *
884  * \return 0 on success.
885  */
886 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
887 {
888         struct ll_sb_info               *sbi = ll_s2sbi(sb);
889         struct hsm_progress_kernel       hpk;
890         int                              rc = 0;
891         int                              rc2;
892         ENTRY;
893
894         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
895         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
896          * initialized if copy_end was called with copy == NULL.
897          */
898
899         /* Forge a hsm_progress based on data from copy. */
900         hpk.hpk_fid = copy->hc_hai.hai_fid;
901         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
902         hpk.hpk_extent = copy->hc_hai.hai_extent;
903         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
904         hpk.hpk_errval = copy->hc_errval;
905         hpk.hpk_data_version = 0;
906
907         /* For archive request, we need to check the file data was not changed.
908          *
909          * For restore request, we need to send the file data version, this is
910          * useful when the file was created using hsm_import.
911          */
912         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
913              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
914             (copy->hc_errval == 0)) {
915                 struct inode    *inode;
916                 __u64            data_version = 0;
917
918                 /* Get lsm for this fid */
919                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
920                 if (IS_ERR(inode)) {
921                         hpk.hpk_flags |= HP_FLAG_RETRY;
922                         /* hpk_errval must be >= 0 */
923                         hpk.hpk_errval = -PTR_ERR(inode);
924                         GOTO(progress, rc = PTR_ERR(inode));
925                 }
926
927                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
928                 iput(inode);
929                 if (rc) {
930                         CDEBUG(D_HSM, "Could not read file data version. "
931                                       "Request could not be confirmed.\n");
932                         if (hpk.hpk_errval == 0)
933                                 hpk.hpk_errval = -rc;
934                         GOTO(progress, rc);
935                 }
936
937                 /* Store in the hsm_copy for later copytool use.
938                  * Always modified even if no lsm. */
939                 hpk.hpk_data_version = data_version;
940
941                 /* File could have been stripped during archiving, so we need
942                  * to check anyway. */
943                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
944                     (copy->hc_data_version != data_version)) {
945                         CDEBUG(D_HSM, "File data version mismatched. "
946                               "File content was changed during archiving. "
947                                DFID", start:%#llx current:%#llx\n",
948                                PFID(&copy->hc_hai.hai_fid),
949                                copy->hc_data_version, data_version);
950                         /* File was changed, send error to cdt. Do not ask for
951                          * retry because if a file is modified frequently,
952                          * the cdt will loop on retried archive requests.
953                          * The policy engine will ask for a new archive later
954                          * when the file will not be modified for some tunable
955                          * time */
956                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
957                         rc = -EBUSY;
958                         /* hpk_errval must be >= 0 */
959                         hpk.hpk_errval = -rc;
960                         GOTO(progress, rc);
961                 }
962
963         }
964
965 progress:
966         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
967                             &hpk, NULL);
968
969         /* Return first error */
970         RETURN(rc != 0 ? rc : rc2);
971 }
972
973
974 static int copy_and_ct_start(int cmd, struct obd_export *exp,
975                              const struct lustre_kernelcomm __user *data)
976 {
977         struct lustre_kernelcomm *lk;
978         struct lustre_kernelcomm *tmp;
979         size_t size = sizeof(*lk);
980         size_t new_size;
981         int i;
982         int rc;
983
984         /* copy data from userspace to get numbers of archive_id */
985         OBD_ALLOC(lk, size);
986         if (lk == NULL)
987                 return -ENOMEM;
988
989         if (copy_from_user(lk, data, size))
990                 GOTO(out_lk, rc = -EFAULT);
991
992         if (lk->lk_flags & LK_FLG_STOP)
993                 goto do_ioctl;
994
995         if (!(lk->lk_flags & LK_FLG_DATANR)) {
996                 __u32 archive_mask = lk->lk_data_count;
997                 int count;
998
999                 /* old hsm agent to old MDS */
1000                 if (!exp_connect_archive_id_array(exp))
1001                         goto do_ioctl;
1002
1003                 /* old hsm agent to new MDS */
1004                 lk->lk_flags |= LK_FLG_DATANR;
1005
1006                 if (archive_mask == 0)
1007                         goto do_ioctl;
1008
1009                 count = hweight32(archive_mask);
1010                 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1011                 OBD_ALLOC(tmp, new_size);
1012                 if (tmp == NULL)
1013                         GOTO(out_lk, rc = -ENOMEM);
1014
1015                 memcpy(tmp, lk, size);
1016                 tmp->lk_data_count = count;
1017                 OBD_FREE(lk, size);
1018                 lk = tmp;
1019                 size = new_size;
1020
1021                 count = 0;
1022                 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1023                         if (BIT(i) & archive_mask) {
1024                                 lk->lk_data[count] = i + 1;
1025                                 count++;
1026                         }
1027                 }
1028                 goto do_ioctl;
1029         }
1030
1031         /* new hsm agent to new mds */
1032         if (lk->lk_data_count > 0) {
1033                 new_size = offsetof(struct lustre_kernelcomm,
1034                                     lk_data[lk->lk_data_count]);
1035                 OBD_ALLOC(tmp, new_size);
1036                 if (tmp == NULL)
1037                         GOTO(out_lk, rc = -ENOMEM);
1038
1039                 OBD_FREE(lk, size);
1040                 lk = tmp;
1041                 size = new_size;
1042
1043                 if (copy_from_user(lk, data, size))
1044                         GOTO(out_lk, rc = -EFAULT);
1045         }
1046
1047         /* new hsm agent to old MDS */
1048         if (!exp_connect_archive_id_array(exp)) {
1049                 __u32 archives = 0;
1050
1051                 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1052                         GOTO(out_lk, rc = -EINVAL);
1053
1054                 for (i = 0; i < lk->lk_data_count; i++) {
1055                         if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1056                                 rc = -EINVAL;
1057                                 CERROR("%s: archive id %d requested but only "
1058                                        "[0 - %zu] supported: rc = %d\n",
1059                                        exp->exp_obd->obd_name, lk->lk_data[i],
1060                                        LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1061                                 GOTO(out_lk, rc);
1062                         }
1063
1064                         if (lk->lk_data[i] == 0) {
1065                                 archives = 0;
1066                                 break;
1067                         }
1068
1069                         archives |= (1 << (lk->lk_data[i] - 1));
1070                 }
1071                 lk->lk_flags &= ~LK_FLG_DATANR;
1072                 lk->lk_data_count = archives;
1073         }
1074 do_ioctl:
1075         rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1076 out_lk:
1077         OBD_FREE(lk, size);
1078         return rc;
1079 }
1080
1081 static int check_owner(int type, int id)
1082 {
1083         switch (type) {
1084         case USRQUOTA:
1085                 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1086                         return -EPERM;
1087                 break;
1088         case GRPQUOTA:
1089                 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1090                         return -EPERM;
1091                 break;
1092         case PRJQUOTA:
1093                 break;
1094         }
1095         return 0;
1096 }
1097
1098 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1099 {
1100         int cmd = qctl->qc_cmd;
1101         int type = qctl->qc_type;
1102         int id = qctl->qc_id;
1103         int valid = qctl->qc_valid;
1104         int rc = 0;
1105         ENTRY;
1106
1107         switch (cmd) {
1108         case Q_SETQUOTA:
1109         case Q_SETINFO:
1110         case LUSTRE_Q_SETDEFAULT:
1111         case LUSTRE_Q_SETQUOTAPOOL:
1112         case LUSTRE_Q_SETINFOPOOL:
1113                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1114                         RETURN(-EPERM);
1115                 break;
1116         case Q_GETQUOTA:
1117         case LUSTRE_Q_GETDEFAULT:
1118         case LUSTRE_Q_GETQUOTAPOOL:
1119                 if (check_owner(type, id) &&
1120                     (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1121                         RETURN(-EPERM);
1122                 break;
1123         case Q_GETINFO:
1124         case LUSTRE_Q_GETINFOPOOL:
1125                 break;
1126         default:
1127                 CERROR("unsupported quotactl op: %#x\n", cmd);
1128                 RETURN(-ENOTSUPP);
1129         }
1130
1131         if (valid != QC_GENERAL) {
1132                 if (cmd == Q_GETINFO)
1133                         qctl->qc_cmd = Q_GETOINFO;
1134                 else if (cmd == Q_GETQUOTA ||
1135                          cmd == LUSTRE_Q_GETQUOTAPOOL)
1136                         qctl->qc_cmd = Q_GETOQUOTA;
1137                 else
1138                         RETURN(-EINVAL);
1139
1140                 switch (valid) {
1141                 case QC_MDTIDX:
1142                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1143                                            sizeof(*qctl), qctl, NULL);
1144                         break;
1145                 case QC_OSTIDX:
1146                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1147                                            sizeof(*qctl), qctl, NULL);
1148                         break;
1149                 case QC_UUID:
1150                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1151                                            sizeof(*qctl), qctl, NULL);
1152                         if (rc == -EAGAIN)
1153                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1154                                                    sbi->ll_dt_exp,
1155                                                    sizeof(*qctl), qctl, NULL);
1156                         break;
1157                 default:
1158                         rc = -EINVAL;
1159                         break;
1160                 }
1161
1162                 if (rc)
1163                         RETURN(rc);
1164
1165                 qctl->qc_cmd = cmd;
1166         } else {
1167                 struct obd_quotactl *oqctl;
1168                 int oqctl_len = sizeof(*oqctl);
1169
1170                 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1171                         oqctl_len += LOV_MAXPOOLNAME + 1;
1172
1173                 OBD_ALLOC(oqctl, oqctl_len);
1174                 if (oqctl == NULL)
1175                         RETURN(-ENOMEM);
1176
1177                 QCTL_COPY(oqctl, qctl);
1178                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1179                 if (rc) {
1180                         OBD_FREE(oqctl, oqctl_len);
1181                         RETURN(rc);
1182                 }
1183                 /* If QIF_SPACE is not set, client should collect the
1184                  * space usage from OSSs by itself */
1185                 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1186                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1187                     !oqctl->qc_dqblk.dqb_curspace) {
1188                         struct obd_quotactl *oqctl_tmp;
1189
1190                         OBD_ALLOC_PTR(oqctl_tmp);
1191                         if (oqctl_tmp == NULL)
1192                                 GOTO(out, rc = -ENOMEM);
1193
1194                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1195                         oqctl_tmp->qc_id = oqctl->qc_id;
1196                         oqctl_tmp->qc_type = oqctl->qc_type;
1197
1198                         /* collect space usage from OSTs */
1199                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1200                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1201                         if (!rc || rc == -EREMOTEIO) {
1202                                 oqctl->qc_dqblk.dqb_curspace =
1203                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1204                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1205                         }
1206
1207                         /* collect space & inode usage from MDTs */
1208                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1209                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1210                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1211                         if (!rc || rc == -EREMOTEIO) {
1212                                 oqctl->qc_dqblk.dqb_curspace +=
1213                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1214                                 oqctl->qc_dqblk.dqb_curinodes =
1215                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1216                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1217                         } else {
1218                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1219                         }
1220
1221                         OBD_FREE_PTR(oqctl_tmp);
1222                 }
1223 out:
1224                 QCTL_COPY(qctl, oqctl);
1225                 OBD_FREE(oqctl, oqctl_len);
1226         }
1227
1228         RETURN(rc);
1229 }
1230
1231 int ll_rmfid(struct file *file, void __user *arg)
1232 {
1233         const struct fid_array __user *ufa = arg;
1234         struct fid_array *lfa = NULL;
1235         size_t size;
1236         unsigned nr;
1237         int i, rc, *rcs = NULL;
1238         ENTRY;
1239
1240         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1241             !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1242                 RETURN(-EPERM);
1243         /* Only need to get the buflen */
1244         if (get_user(nr, &ufa->fa_nr))
1245                 RETURN(-EFAULT);
1246         /* DoS protection */
1247         if (nr > OBD_MAX_FIDS_IN_ARRAY)
1248                 RETURN(-E2BIG);
1249
1250         size = offsetof(struct fid_array, fa_fids[nr]);
1251         OBD_ALLOC(lfa, size);
1252         if (!lfa)
1253                 RETURN(-ENOMEM);
1254         OBD_ALLOC_PTR_ARRAY(rcs, nr);
1255         if (!rcs)
1256                 GOTO(free_lfa, rc = -ENOMEM);
1257
1258         if (copy_from_user(lfa, arg, size))
1259                 GOTO(free_rcs, rc = -EFAULT);
1260
1261         /* Call mdc_iocontrol */
1262         rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1263         if (!rc) {
1264                 for (i = 0; i < nr; i++)
1265                         if (rcs[i])
1266                                 lfa->fa_fids[i].f_ver = rcs[i];
1267                 if (copy_to_user(arg, lfa, size))
1268                         rc = -EFAULT;
1269         }
1270
1271 free_rcs:
1272         OBD_FREE_PTR_ARRAY(rcs, nr);
1273 free_lfa:
1274         OBD_FREE(lfa, size);
1275
1276         RETURN(rc);
1277 }
1278
1279 /* This function tries to get a single name component,
1280  * to send to the server. No actual path traversal involved,
1281  * so we limit to NAME_MAX */
1282 static char *ll_getname(const char __user *filename)
1283 {
1284         int ret = 0, len;
1285         char *tmp;
1286
1287         OBD_ALLOC(tmp, NAME_MAX + 1);
1288
1289         if (!tmp)
1290                 return ERR_PTR(-ENOMEM);
1291
1292         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1293         if (len < 0)
1294                 ret = -ENOENT;
1295         else if (len > NAME_MAX)
1296                 ret = -ENAMETOOLONG;
1297
1298         if (ret) {
1299                 OBD_FREE(tmp, NAME_MAX + 1);
1300                 tmp =  ERR_PTR(ret);
1301         }
1302         return tmp;
1303 }
1304
1305 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1306
1307 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1308 {
1309         struct dentry *dentry = file_dentry(file);
1310         struct inode *inode = file_inode(file);
1311         struct ll_sb_info *sbi = ll_i2sbi(inode);
1312         struct obd_ioctl_data *data;
1313         int rc = 0;
1314         ENTRY;
1315
1316         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1317                PFID(ll_inode2fid(inode)), inode, cmd);
1318
1319         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1320         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1321                 return -ENOTTY;
1322
1323         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1324         switch (cmd) {
1325         case FS_IOC_GETFLAGS:
1326         case FS_IOC_SETFLAGS:
1327                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1328         case FSFILT_IOC_GETVERSION:
1329         case FS_IOC_GETVERSION:
1330                 RETURN(put_user(inode->i_generation, (int __user *)arg));
1331         /* We need to special case any other ioctls we want to handle,
1332          * to send them to the MDS/OST as appropriate and to properly
1333          * network encode the arg field. */
1334         case FS_IOC_SETVERSION:
1335                 RETURN(-ENOTSUPP);
1336
1337         case LL_IOC_GET_MDTIDX: {
1338                 int mdtidx;
1339
1340                 mdtidx = ll_get_mdt_idx(inode);
1341                 if (mdtidx < 0)
1342                         RETURN(mdtidx);
1343
1344                 if (put_user((int)mdtidx, (int __user *)arg))
1345                         RETURN(-EFAULT);
1346
1347                 return 0;
1348         }
1349         case IOC_MDC_LOOKUP: {
1350                                      int namelen, len = 0;
1351                 char *buf = NULL;
1352                 char *filename;
1353
1354                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1355                 if (rc != 0)
1356                         RETURN(rc);
1357                 data = (void *)buf;
1358
1359                 filename = data->ioc_inlbuf1;
1360                 namelen = strlen(filename);
1361                 if (namelen < 1) {
1362                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1363                         GOTO(out_free, rc = -EINVAL);
1364                 }
1365
1366                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1367                 if (rc < 0) {
1368                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1369                                sbi->ll_fsname, namelen, filename, rc);
1370                         GOTO(out_free, rc);
1371                 }
1372 out_free:
1373                 OBD_FREE_LARGE(buf, len);
1374                 return rc;
1375         }
1376         case LL_IOC_LMV_SETSTRIPE: {
1377                 struct lmv_user_md  *lum;
1378                 char            *buf = NULL;
1379                 char            *filename;
1380                 int              namelen = 0;
1381                 int              lumlen = 0;
1382                 umode_t          mode;
1383                 int              len;
1384                 int              rc;
1385
1386                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1387                 if (rc)
1388                         RETURN(rc);
1389
1390                 data = (void *)buf;
1391                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1392                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1393                         GOTO(lmv_out_free, rc = -EINVAL);
1394
1395                 filename = data->ioc_inlbuf1;
1396                 namelen = data->ioc_inllen1;
1397
1398                 if (namelen < 1) {
1399                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1400                         GOTO(lmv_out_free, rc = -EINVAL);
1401                 }
1402                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1403                 lumlen = data->ioc_inllen2;
1404
1405                 if (!lmv_user_magic_supported(lum->lum_magic)) {
1406                         CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1407                                lum->lum_magic, -EINVAL);
1408                         GOTO(lmv_out_free, rc = -EINVAL);
1409                 }
1410
1411                 if ((lum->lum_magic == LMV_USER_MAGIC ||
1412                      lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1413                     lumlen < sizeof(*lum)) {
1414                         CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1415                                filename, lumlen, lum->lum_magic, -EINVAL);
1416                         GOTO(lmv_out_free, rc = -EINVAL);
1417                 }
1418
1419                 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1420                     lumlen < sizeof(struct lmv_foreign_md)) {
1421                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1422                                filename, lum->lum_magic, lumlen, -EFAULT);
1423                         GOTO(lmv_out_free, rc = -EINVAL);
1424                 }
1425
1426                 mode = data->ioc_type;
1427                 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1428 lmv_out_free:
1429                 OBD_FREE_LARGE(buf, len);
1430                 RETURN(rc);
1431
1432         }
1433         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1434                 struct lmv_user_md        lum;
1435                 struct lmv_user_md __user *ulump =
1436                                         (struct lmv_user_md __user *)arg;
1437                 int                       rc;
1438
1439                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1440                         RETURN(-EFAULT);
1441
1442                 if (lum.lum_magic != LMV_USER_MAGIC)
1443                         RETURN(-EINVAL);
1444
1445                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1446
1447                 RETURN(rc);
1448         }
1449         case LL_IOC_LOV_SETSTRIPE_NEW:
1450         case LL_IOC_LOV_SETSTRIPE: {
1451                 struct lov_user_md_v3 *lumv3 = NULL;
1452                 struct lov_user_md_v1 lumv1;
1453                 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1454                 struct lov_user_md_v1 __user *lumv1p =
1455                         (struct lov_user_md_v1 __user *)arg;
1456                 struct lov_user_md_v3 __user *lumv3p =
1457                         (struct lov_user_md_v3 __user *)arg;
1458                 int lum_size = 0;
1459
1460                 int set_default = 0;
1461
1462                 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1463                              sizeof(struct lov_comp_md_v1));
1464                 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1465                 /* first try with v1 which is smaller than v3 */
1466                 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1467                         RETURN(-EFAULT);
1468
1469                 if (inode->i_sb->s_root == file_dentry(file))
1470                         set_default = 1;
1471
1472                 switch (lumv1.lmm_magic) {
1473                 case LOV_USER_MAGIC_V3:
1474                 case LOV_USER_MAGIC_SPECIFIC:
1475                         lum_size = ll_lov_user_md_size(&lumv1);
1476                         if (lum_size < 0)
1477                                 RETURN(lum_size);
1478                         OBD_ALLOC(lumv3, lum_size);
1479                         if (!lumv3)
1480                                 RETURN(-ENOMEM);
1481                         if (copy_from_user(lumv3, lumv3p, lum_size))
1482                                 GOTO(out, rc = -EFAULT);
1483                         lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1484                         break;
1485                 case LOV_USER_MAGIC_V1:
1486                         break;
1487                 default:
1488                         GOTO(out, rc = -ENOTSUPP);
1489                 }
1490
1491                 /* in v1 and v3 cases lumv1 points to data */
1492                 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1493 out:
1494                 if (lumv3)
1495                         OBD_FREE(lumv3, lum_size);
1496                 RETURN(rc);
1497         }
1498         case LL_IOC_LMV_GETSTRIPE: {
1499                 struct lmv_user_md __user *ulmv =
1500                                         (struct lmv_user_md __user *)arg;
1501                 struct lmv_user_md      lum;
1502                 struct ptlrpc_request   *request = NULL;
1503                 struct ptlrpc_request   *root_request = NULL;
1504                 union lmv_mds_md        *lmm = NULL;
1505                 int                     lmmsize;
1506                 u64                     valid = 0;
1507                 struct lmv_user_md      *tmp = NULL;
1508                 int                     mdt_index;
1509                 int                     lum_size;
1510                 int                     stripe_count;
1511                 int                     max_stripe_count;
1512                 int                     i;
1513                 int                     rc;
1514
1515                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1516                         RETURN(-EFAULT);
1517
1518                 max_stripe_count = lum.lum_stripe_count;
1519                 /* lum_magic will indicate which stripe the ioctl will like
1520                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1521                  * is for default LMV stripe */
1522                 if (lum.lum_magic == LMV_MAGIC_V1)
1523                         valid |= OBD_MD_MEA;
1524                 else if (lum.lum_magic == LMV_USER_MAGIC)
1525                         valid |= OBD_MD_DEFAULT_MEA;
1526                 else
1527                         RETURN(-EINVAL);
1528
1529                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1530                                               &request, &root_request, valid);
1531                 if (rc != 0)
1532                         GOTO(finish_req, rc);
1533
1534                 /* Get default LMV EA */
1535                 if (lum.lum_magic == LMV_USER_MAGIC) {
1536                         if (lmmsize > sizeof(*ulmv))
1537                                 GOTO(finish_req, rc = -EINVAL);
1538
1539                         if (copy_to_user(ulmv, lmm, lmmsize))
1540                                 GOTO(finish_req, rc = -EFAULT);
1541
1542                         GOTO(finish_req, rc);
1543                 }
1544
1545                 /* if foreign LMV case, fake stripes number */
1546                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1547                         struct lmv_foreign_md *lfm;
1548
1549                         lfm = (struct lmv_foreign_md *)lmm;
1550                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1551                             offsetof(typeof(*lfm), lfm_value)) {
1552                                 __u32 size = lfm->lfm_length +
1553                                              offsetof(typeof(*lfm), lfm_value);
1554
1555                                 stripe_count = lmv_foreign_to_md_stripes(size);
1556                         } else {
1557                                 CERROR("invalid %d foreign size returned\n",
1558                                             lfm->lfm_length);
1559                                 return -EINVAL;
1560                         }
1561                 } else {
1562                         stripe_count = lmv_mds_md_stripe_count_get(lmm);
1563                 }
1564                 if (max_stripe_count < stripe_count) {
1565                         lum.lum_stripe_count = stripe_count;
1566                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1567                                 GOTO(finish_req, rc = -EFAULT);
1568                         GOTO(finish_req, rc = -E2BIG);
1569                 }
1570
1571                 /* enough room on user side and foreign case */
1572                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1573                         struct lmv_foreign_md *lfm;
1574                         __u32 size;
1575
1576                         lfm = (struct lmv_foreign_md *)lmm;
1577                         size = lfm->lfm_length +
1578                                offsetof(struct lmv_foreign_md, lfm_value);
1579                         if (copy_to_user(ulmv, lfm, size))
1580                                 GOTO(finish_req, rc = -EFAULT);
1581                         GOTO(finish_req, rc);
1582                 }
1583
1584                 lum_size = lmv_user_md_size(stripe_count,
1585                                             LMV_USER_MAGIC_SPECIFIC);
1586                 OBD_ALLOC(tmp, lum_size);
1587                 if (tmp == NULL)
1588                         GOTO(finish_req, rc = -ENOMEM);
1589
1590                 mdt_index = ll_get_mdt_idx(inode);
1591                 if (mdt_index < 0)
1592                         GOTO(out_tmp, rc = -ENOMEM);
1593
1594                 tmp->lum_magic = LMV_MAGIC_V1;
1595                 tmp->lum_stripe_count = 0;
1596                 tmp->lum_stripe_offset = mdt_index;
1597                 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1598                 for (i = 0; i < stripe_count; i++) {
1599                         struct lu_fid   fid;
1600
1601                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1602                         if (fid_is_sane(&fid)) {
1603                                 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1604                                 if (mdt_index < 0)
1605                                         GOTO(out_tmp, rc = mdt_index);
1606
1607                                 tmp->lum_objects[i].lum_mds = mdt_index;
1608                                 tmp->lum_objects[i].lum_fid = fid;
1609                         }
1610
1611                         tmp->lum_stripe_count++;
1612                 }
1613
1614                 if (copy_to_user(ulmv, tmp, lum_size))
1615                         GOTO(out_tmp, rc = -EFAULT);
1616 out_tmp:
1617                 OBD_FREE(tmp, lum_size);
1618 finish_req:
1619                 ptlrpc_req_finished(request);
1620                 ptlrpc_req_finished(root_request);
1621                 return rc;
1622         }
1623
1624         case LL_IOC_REMOVE_ENTRY: {
1625                 char            *filename = NULL;
1626                 int              namelen = 0;
1627                 int              rc;
1628
1629                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1630                  * unsupported server, which might crash the server(LU-2730),
1631                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1632                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1633                  * server will support REINT_RMENTRY XXX*/
1634                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1635                         RETURN(-ENOTSUPP);
1636
1637                 filename = ll_getname((const char __user *)arg);
1638                 if (IS_ERR(filename))
1639                         RETURN(PTR_ERR(filename));
1640
1641                 namelen = strlen(filename);
1642                 if (namelen < 1)
1643                         GOTO(out_rmdir, rc = -EINVAL);
1644
1645                 rc = ll_rmdir_entry(inode, filename, namelen);
1646 out_rmdir:
1647                 if (filename)
1648                         ll_putname(filename);
1649                 RETURN(rc);
1650         }
1651         case LL_IOC_RMFID:
1652                 RETURN(ll_rmfid(file, (void __user *)arg));
1653         case LL_IOC_LOV_SWAP_LAYOUTS:
1654                 RETURN(-EPERM);
1655         case IOC_OBD_STATFS:
1656                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1657         case LL_IOC_LOV_GETSTRIPE:
1658         case LL_IOC_LOV_GETSTRIPE_NEW:
1659         case LL_IOC_MDC_GETINFO:
1660         case LL_IOC_MDC_GETINFO_OLD:
1661         case IOC_MDC_GETFILEINFO:
1662         case IOC_MDC_GETFILEINFO_OLD:
1663         case IOC_MDC_GETFILESTRIPE: {
1664                 struct ptlrpc_request *request = NULL;
1665                 struct ptlrpc_request *root_request = NULL;
1666                 struct lov_user_md __user *lump;
1667                 struct lov_mds_md *lmm = NULL;
1668                 struct mdt_body *body;
1669                 char *filename = NULL;
1670                 lstat_t __user *statp = NULL;
1671                 lstatx_t __user *stxp = NULL;
1672                 __u64 __user *flagsp = NULL;
1673                 __u32 __user *lmmsizep = NULL;
1674                 struct lu_fid __user *fidp = NULL;
1675                 int lmmsize;
1676
1677                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1678                     cmd == IOC_MDC_GETFILEINFO ||
1679                     cmd == IOC_MDC_GETFILESTRIPE) {
1680                         filename = ll_getname((const char __user *)arg);
1681                         if (IS_ERR(filename))
1682                                 RETURN(PTR_ERR(filename));
1683
1684                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1685                                                       &lmmsize, &request);
1686                 } else {
1687                         rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1688                                                       &lmmsize, &request,
1689                                                       &root_request, 0);
1690                 }
1691
1692                 if (request) {
1693                         body = req_capsule_server_get(&request->rq_pill,
1694                                                       &RMF_MDT_BODY);
1695                         LASSERT(body != NULL);
1696                 } else {
1697                         GOTO(out_req, rc);
1698                 }
1699
1700                 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1701                                        cmd == LL_IOC_MDC_GETINFO ||
1702                                        cmd == IOC_MDC_GETFILEINFO_OLD ||
1703                                        cmd == LL_IOC_MDC_GETINFO_OLD)) {
1704                         lmmsize = 0;
1705                         rc = 0;
1706                 }
1707
1708                 if (rc < 0)
1709                         GOTO(out_req, rc);
1710
1711                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1712                     cmd == LL_IOC_LOV_GETSTRIPE ||
1713                     cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1714                         lump = (struct lov_user_md __user *)arg;
1715                 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1716                            cmd == LL_IOC_MDC_GETINFO_OLD){
1717                         struct lov_user_mds_data_v1 __user *lmdp;
1718
1719                         lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1720                         statp = &lmdp->lmd_st;
1721                         lump = &lmdp->lmd_lmm;
1722                 } else {
1723                         struct lov_user_mds_data __user *lmdp;
1724
1725                         lmdp = (struct lov_user_mds_data __user *)arg;
1726                         fidp = &lmdp->lmd_fid;
1727                         stxp = &lmdp->lmd_stx;
1728                         flagsp = &lmdp->lmd_flags;
1729                         lmmsizep = &lmdp->lmd_lmmsize;
1730                         lump = &lmdp->lmd_lmm;
1731                 }
1732
1733                 if (lmmsize == 0) {
1734                         /* If the file has no striping then zero out *lump so
1735                          * that the caller isn't confused by garbage. */
1736                         if (clear_user(lump, sizeof(*lump)))
1737                                 GOTO(out_req, rc = -EFAULT);
1738                 } else if (copy_to_user(lump, lmm, lmmsize)) {
1739                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1740                                 GOTO(out_req, rc = -EFAULT);
1741                         rc = -EOVERFLOW;
1742                 }
1743
1744                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1745                     cmd == LL_IOC_MDC_GETINFO_OLD) {
1746                         lstat_t st = { 0 };
1747
1748                         st.st_dev       = inode->i_sb->s_dev;
1749                         st.st_mode      = body->mbo_mode;
1750                         st.st_nlink     = body->mbo_nlink;
1751                         st.st_uid       = body->mbo_uid;
1752                         st.st_gid       = body->mbo_gid;
1753                         st.st_rdev      = body->mbo_rdev;
1754                         st.st_size      = body->mbo_size;
1755                         st.st_blksize   = PAGE_SIZE;
1756                         st.st_blocks    = body->mbo_blocks;
1757                         st.st_atime     = body->mbo_atime;
1758                         st.st_mtime     = body->mbo_mtime;
1759                         st.st_ctime     = body->mbo_ctime;
1760                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1761                                                 sbi->ll_flags &
1762                                                 LL_SBI_32BIT_API);
1763
1764                         if (copy_to_user(statp, &st, sizeof(st)))
1765                                 GOTO(out_req, rc = -EFAULT);
1766                 } else if (cmd == IOC_MDC_GETFILEINFO ||
1767                            cmd == LL_IOC_MDC_GETINFO) {
1768                         lstatx_t stx = { 0 };
1769                         __u64 valid = body->mbo_valid;
1770
1771                         stx.stx_blksize = PAGE_SIZE;
1772                         stx.stx_nlink = body->mbo_nlink;
1773                         stx.stx_uid = body->mbo_uid;
1774                         stx.stx_gid = body->mbo_gid;
1775                         stx.stx_mode = body->mbo_mode;
1776                         stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1777                                                        sbi->ll_flags &
1778                                                        LL_SBI_32BIT_API);
1779                         stx.stx_size = body->mbo_size;
1780                         stx.stx_blocks = body->mbo_blocks;
1781                         stx.stx_atime.tv_sec = body->mbo_atime;
1782                         stx.stx_ctime.tv_sec = body->mbo_ctime;
1783                         stx.stx_mtime.tv_sec = body->mbo_mtime;
1784                         stx.stx_btime.tv_sec = body->mbo_btime;
1785                         stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1786                         stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1787                         stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1788                         stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1789                         stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1790
1791                         /*
1792                          * For a striped directory, the size and blocks returned
1793                          * from MDT is not correct.
1794                          * The size and blocks are aggregated by client across
1795                          * all stripes.
1796                          * Thus for a striped directory, do not return the valid
1797                          * FLSIZE and FLBLOCKS flags to the caller.
1798                          * However, this whould be better decided by the MDS
1799                          * instead of the client.
1800                          */
1801                         if (cmd == LL_IOC_MDC_GETINFO &&
1802                             ll_i2info(inode)->lli_lsm_md != NULL)
1803                                 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1804
1805                         if (flagsp && copy_to_user(flagsp, &valid,
1806                                                    sizeof(*flagsp)))
1807                                 GOTO(out_req, rc = -EFAULT);
1808
1809                         if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1810                                                  sizeof(*fidp)))
1811                                 GOTO(out_req, rc = -EFAULT);
1812
1813                         if (!(valid & OBD_MD_FLSIZE))
1814                                 stx.stx_mask &= ~STATX_SIZE;
1815                         if (!(valid & OBD_MD_FLBLOCKS))
1816                                 stx.stx_mask &= ~STATX_BLOCKS;
1817
1818                         if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1819                                 GOTO(out_req, rc = -EFAULT);
1820
1821                         if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1822                                                      sizeof(*lmmsizep)))
1823                                 GOTO(out_req, rc = -EFAULT);
1824                 }
1825
1826                 EXIT;
1827 out_req:
1828                 ptlrpc_req_finished(request);
1829                 ptlrpc_req_finished(root_request);
1830                 if (filename)
1831                         ll_putname(filename);
1832                 return rc;
1833         }
1834         case OBD_IOC_QUOTACTL: {
1835                 struct if_quotactl *qctl;
1836                 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1837
1838                 OBD_ALLOC(qctl, qctl_len);
1839                 if (!qctl)
1840                         RETURN(-ENOMEM);
1841
1842                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1843                         GOTO(out_quotactl, rc = -EFAULT);
1844
1845                 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
1846                         char __user *from = (char __user *)arg +
1847                                         offsetof(typeof(*qctl), qc_poolname);
1848                         if (copy_from_user(qctl->qc_poolname, from,
1849                                            LOV_MAXPOOLNAME + 1))
1850                                 GOTO(out_quotactl, rc = -EFAULT);
1851                 }
1852
1853                 rc = quotactl_ioctl(sbi, qctl);
1854                 if (rc == 0 &&
1855                     copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1856                         rc = -EFAULT;
1857
1858 out_quotactl:
1859                 OBD_FREE(qctl, qctl_len);
1860                 RETURN(rc);
1861         }
1862         case OBD_IOC_GETDTNAME:
1863         case OBD_IOC_GETMDNAME:
1864                 RETURN(ll_get_obd_name(inode, cmd, arg));
1865         case LL_IOC_FLUSHCTX:
1866                 RETURN(ll_flush_ctx(inode));
1867         case LL_IOC_GETOBDCOUNT: {
1868                 u32 count, vallen;
1869                 struct obd_export *exp;
1870
1871                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1872                         RETURN(-EFAULT);
1873
1874                 /* get ost count when count is zero, get mdt count otherwise */
1875                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1876                 vallen = sizeof(count);
1877                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1878                                   KEY_TGT_COUNT, &vallen, &count);
1879                 if (rc) {
1880                         CERROR("get target count failed: %d\n", rc);
1881                         RETURN(rc);
1882                 }
1883
1884                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1885                         RETURN(-EFAULT);
1886
1887                 RETURN(0);
1888         }
1889         case LL_IOC_PATH2FID:
1890                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1891                                      sizeof(struct lu_fid)))
1892                         RETURN(-EFAULT);
1893                 RETURN(0);
1894         case LL_IOC_GET_CONNECT_FLAGS: {
1895                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1896                                      (void __user *)arg));
1897         }
1898         case OBD_IOC_FID2PATH:
1899                 RETURN(ll_fid2path(inode, (void __user *)arg));
1900         case LL_IOC_GETPARENT:
1901                 RETURN(ll_getparent(file, (void __user *)arg));
1902         case LL_IOC_FID2MDTIDX: {
1903                 struct obd_export *exp = ll_i2mdexp(inode);
1904                 struct lu_fid     fid;
1905                 __u32             index;
1906
1907                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1908                                    sizeof(fid)))
1909                         RETURN(-EFAULT);
1910
1911                 /* Call mdc_iocontrol */
1912                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1913                                    (__u32 __user *)&index);
1914                 if (rc != 0)
1915                         RETURN(rc);
1916
1917                 RETURN(index);
1918         }
1919         case LL_IOC_HSM_REQUEST: {
1920                 struct hsm_user_request *hur;
1921                 ssize_t                  totalsize;
1922
1923                 OBD_ALLOC_PTR(hur);
1924                 if (hur == NULL)
1925                         RETURN(-ENOMEM);
1926
1927                 /* We don't know the true size yet; copy the fixed-size part */
1928                 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1929                         OBD_FREE_PTR(hur);
1930                         RETURN(-EFAULT);
1931                 }
1932
1933                 /* Compute the whole struct size */
1934                 totalsize = hur_len(hur);
1935                 OBD_FREE_PTR(hur);
1936                 if (totalsize < 0)
1937                         RETURN(-E2BIG);
1938
1939                 /* Final size will be more than double totalsize */
1940                 if (totalsize >= MDS_MAXREQSIZE / 3)
1941                         RETURN(-E2BIG);
1942
1943                 OBD_ALLOC_LARGE(hur, totalsize);
1944                 if (hur == NULL)
1945                         RETURN(-ENOMEM);
1946
1947                 /* Copy the whole struct */
1948                 if (copy_from_user(hur, (void __user *)arg, totalsize))
1949                         GOTO(out_hur, rc = -EFAULT);
1950
1951                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1952                         const struct lu_fid *fid;
1953                         struct inode *f;
1954                         int i;
1955
1956                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1957                                 fid = &hur->hur_user_item[i].hui_fid;
1958                                 f = search_inode_for_lustre(inode->i_sb, fid);
1959                                 if (IS_ERR(f)) {
1960                                         rc = PTR_ERR(f);
1961                                         break;
1962                                 }
1963
1964                                 rc = ll_hsm_release(f);
1965                                 iput(f);
1966                                 if (rc != 0)
1967                                         break;
1968                         }
1969                 } else {
1970                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1971                                            hur, NULL);
1972                 }
1973
1974 out_hur:
1975                 OBD_FREE_LARGE(hur, totalsize);
1976
1977                 RETURN(rc);
1978         }
1979         case LL_IOC_HSM_PROGRESS: {
1980                 struct hsm_progress_kernel      hpk;
1981                 struct hsm_progress             hp;
1982
1983                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1984                         RETURN(-EFAULT);
1985
1986                 hpk.hpk_fid = hp.hp_fid;
1987                 hpk.hpk_cookie = hp.hp_cookie;
1988                 hpk.hpk_extent = hp.hp_extent;
1989                 hpk.hpk_flags = hp.hp_flags;
1990                 hpk.hpk_errval = hp.hp_errval;
1991                 hpk.hpk_data_version = 0;
1992
1993                 /* File may not exist in Lustre; all progress
1994                  * reported to Lustre root */
1995                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1996                                    NULL);
1997                 RETURN(rc);
1998         }
1999         case LL_IOC_HSM_CT_START:
2000                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2001                         RETURN(-EPERM);
2002
2003                 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2004                                        (struct lustre_kernelcomm __user *)arg);
2005                 RETURN(rc);
2006
2007         case LL_IOC_HSM_COPY_START: {
2008                 struct hsm_copy *copy;
2009                 int              rc;
2010
2011                 OBD_ALLOC_PTR(copy);
2012                 if (copy == NULL)
2013                         RETURN(-ENOMEM);
2014                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2015                         OBD_FREE_PTR(copy);
2016                         RETURN(-EFAULT);
2017                 }
2018
2019                 rc = ll_ioc_copy_start(inode->i_sb, copy);
2020                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2021                         rc = -EFAULT;
2022
2023                 OBD_FREE_PTR(copy);
2024                 RETURN(rc);
2025         }
2026         case LL_IOC_HSM_COPY_END: {
2027                 struct hsm_copy *copy;
2028                 int              rc;
2029
2030                 OBD_ALLOC_PTR(copy);
2031                 if (copy == NULL)
2032                         RETURN(-ENOMEM);
2033                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2034                         OBD_FREE_PTR(copy);
2035                         RETURN(-EFAULT);
2036                 }
2037
2038                 rc = ll_ioc_copy_end(inode->i_sb, copy);
2039                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2040                         rc = -EFAULT;
2041
2042                 OBD_FREE_PTR(copy);
2043                 RETURN(rc);
2044         }
2045         case LL_IOC_MIGRATE: {
2046                 struct lmv_user_md *lum;
2047                 char *buf = NULL;
2048                 int len;
2049                 char *filename;
2050                 int namelen = 0;
2051                 int rc;
2052
2053                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2054                 if (rc)
2055                         RETURN(rc);
2056
2057                 data = (struct obd_ioctl_data *)buf;
2058                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2059                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2060                         GOTO(migrate_free, rc = -EINVAL);
2061
2062                 filename = data->ioc_inlbuf1;
2063                 namelen = data->ioc_inllen1;
2064
2065                 if (namelen < 1 || namelen != strlen(filename) + 1) {
2066                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2067                         GOTO(migrate_free, rc = -EINVAL);
2068                 }
2069
2070                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2071                 if (lum->lum_magic != LMV_USER_MAGIC &&
2072                     lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2073                         rc = -EINVAL;
2074                         CERROR("%s: wrong lum magic %x: rc = %d\n",
2075                                filename, lum->lum_magic, rc);
2076                         GOTO(migrate_free, rc);
2077                 }
2078
2079                 rc = ll_migrate(inode, file, lum, filename);
2080 migrate_free:
2081                 OBD_FREE_LARGE(buf, len);
2082
2083                 RETURN(rc);
2084         }
2085         case LL_IOC_FSGETXATTR:
2086                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2087         case LL_IOC_FSSETXATTR:
2088                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2089         case LL_IOC_PCC_DETACH_BY_FID: {
2090                 struct lu_pcc_detach_fid *detach;
2091                 struct lu_fid *fid;
2092                 struct inode *inode2;
2093                 unsigned long ino;
2094
2095                 OBD_ALLOC_PTR(detach);
2096                 if (detach == NULL)
2097                         RETURN(-ENOMEM);
2098
2099                 if (copy_from_user(detach,
2100                                    (const struct lu_pcc_detach_fid __user *)arg,
2101                                    sizeof(*detach)))
2102                         GOTO(out_detach, rc = -EFAULT);
2103
2104                 fid = &detach->pccd_fid;
2105                 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2106                 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2107                 if (inode2 == NULL)
2108                         /* Target inode is not in inode cache, and PCC file
2109                          * has aleady released, return immdiately.
2110                          */
2111                         GOTO(out_detach, rc = 0);
2112
2113                 if (!S_ISREG(inode2->i_mode))
2114                         GOTO(out_iput, rc = -EINVAL);
2115
2116                 if (!inode_owner_or_capable(inode2))
2117                         GOTO(out_iput, rc = -EPERM);
2118
2119                 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2120 out_iput:
2121                 iput(inode2);
2122 out_detach:
2123                 OBD_FREE_PTR(detach);
2124                 RETURN(rc);
2125         }
2126         default:
2127                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2128                                      (void __user *)arg));
2129         }
2130 }
2131
2132 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2133 {
2134         struct inode *inode = file->f_mapping->host;
2135         struct ll_file_data *fd = file->private_data;
2136         struct ll_sb_info *sbi = ll_i2sbi(inode);
2137         int api32 = ll_need_32bit_api(sbi);
2138         loff_t ret = -EINVAL;
2139         ENTRY;
2140
2141         inode_lock(inode);
2142         switch (origin) {
2143         case SEEK_SET:
2144                 break;
2145         case SEEK_CUR:
2146                 offset += file->f_pos;
2147                 break;
2148         case SEEK_END:
2149                 if (offset > 0)
2150                         GOTO(out, ret);
2151                 if (api32)
2152                         offset += LL_DIR_END_OFF_32BIT;
2153                 else
2154                         offset += LL_DIR_END_OFF;
2155                 break;
2156         default:
2157                 GOTO(out, ret);
2158         }
2159
2160         if (offset >= 0 &&
2161             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2162              (!api32 && offset <= LL_DIR_END_OFF))) {
2163                 if (offset != file->f_pos) {
2164                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2165                             (!api32 && offset == LL_DIR_END_OFF))
2166                                 fd->lfd_pos = MDS_DIR_END_OFF;
2167                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2168                                 fd->lfd_pos = offset << 32;
2169                         else
2170                                 fd->lfd_pos = offset;
2171                         file->f_pos = offset;
2172                         file->f_version = 0;
2173                 }
2174                 ret = offset;
2175         }
2176         GOTO(out, ret);
2177
2178 out:
2179         inode_unlock(inode);
2180         return ret;
2181 }
2182
2183 static int ll_dir_open(struct inode *inode, struct file *file)
2184 {
2185         ENTRY;
2186         RETURN(ll_file_open(inode, file));
2187 }
2188
2189 static int ll_dir_release(struct inode *inode, struct file *file)
2190 {
2191         ENTRY;
2192         RETURN(ll_file_release(inode, file));
2193 }
2194
2195 const struct file_operations ll_dir_operations = {
2196         .llseek         = ll_dir_seek,
2197         .open           = ll_dir_open,
2198         .release        = ll_dir_release,
2199         .read           = generic_read_dir,
2200 #ifdef HAVE_DIR_CONTEXT
2201         .iterate_shared = ll_iterate,
2202 #else
2203         .readdir        = ll_readdir,
2204 #endif
2205         .unlocked_ioctl = ll_dir_ioctl,
2206         .fsync          = ll_fsync,
2207 };