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