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