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