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