Whamcloud - gitweb
LU-4788 lfsck: replace cfs_list_t with list_head
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/dir.c
37  *
38  * Directory code for lustre client.
39  */
40
41 #include <linux/fs.h>
42 #include <linux/pagemap.h>
43 #include <linux/mm.h>
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/buffer_head.h>   // for wait_on_buffer
47 #include <linux/pagevec.h>
48
49 #define DEBUG_SUBSYSTEM S_LLITE
50
51 #include <lustre/lustre_idl.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_ioctl.h>
55 #include <lustre_lib.h>
56 #include <lustre_lite.h>
57 #include <lustre_dlm.h>
58 #include <lustre_fid.h>
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_CACHE_SIZE (if PAGE_CACHE_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 != NULL && S_ISDIR(inode->i_mode) &&
165             ll_i2info(inode)->lli_lsm_md != NULL) {
166                 __free_page(page);
167                 return;
168         }
169
170         if (remove) {
171                 lock_page(page);
172                 if (likely(page->mapping != NULL))
173                         truncate_complete_page(page->mapping, page);
174                 unlock_page(page);
175         }
176         page_cache_release(page);
177 }
178
179 #ifdef HAVE_DIR_CONTEXT
180 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
181                 struct dir_context *ctx)
182 {
183 #else
184 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
185                 void *cookie, filldir_t filldir)
186 {
187 #endif
188         struct ll_sb_info    *sbi        = ll_i2sbi(inode);
189         __u64                 pos        = *ppos;
190         bool                  is_api32 = ll_need_32bit_api(sbi);
191         bool                  is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
192         struct page          *page;
193         struct ll_dir_chain   chain;
194         bool                  done = false;
195         int                   rc = 0;
196         ENTRY;
197
198         ll_dir_chain_init(&chain);
199
200         page = ll_get_dir_page(inode, op_data, pos, &chain);
201
202         while (rc == 0 && !done) {
203                 struct lu_dirpage *dp;
204                 struct lu_dirent  *ent;
205                 __u64 hash;
206                 __u64 next;
207
208                 if (IS_ERR(page)) {
209                         rc = PTR_ERR(page);
210                         break;
211                 }
212
213                 hash = MDS_DIR_END_OFF;
214                 dp = page_address(page);
215                 for (ent = lu_dirent_start(dp); ent != NULL && !done;
216                      ent = lu_dirent_next(ent)) {
217                         __u16          type;
218                         int            namelen;
219                         struct lu_fid  fid;
220                         __u64          lhash;
221                         __u64          ino;
222
223                         hash = le64_to_cpu(ent->lde_hash);
224                         if (hash < pos)
225                                 /*
226                                  * Skip until we find target hash
227                                  * value.
228                                  */
229                                 continue;
230
231                         namelen = le16_to_cpu(ent->lde_namelen);
232                         if (namelen == 0)
233                                 /*
234                                  * Skip dummy record.
235                                  */
236                                 continue;
237
238                         if (is_api32 && is_hash64)
239                                 lhash = hash >> 32;
240                         else
241                                 lhash = hash;
242                         fid_le_to_cpu(&fid, &ent->lde_fid);
243                         ino = cl_fid_build_ino(&fid, is_api32);
244                         type = ll_dirent_type_get(ent);
245                         /* For 'll_nfs_get_name_filldir()', it will try
246                          * to access the 'ent' through its 'lde_name',
247                          * so the parameter 'name' for 'filldir()' must
248                          * be part of the 'ent'. */
249 #ifdef HAVE_DIR_CONTEXT
250                         ctx->pos = lhash;
251                         done = !dir_emit(ctx, ent->lde_name, namelen, ino,
252                                          type);
253 #else
254                         done = filldir(cookie, ent->lde_name, namelen, lhash,
255                                        ino, type);
256 #endif
257                 }
258
259                 if (done) {
260                         pos = hash;
261                         ll_release_page(inode, page, false);
262                         break;
263                 }
264
265                 next = le64_to_cpu(dp->ldp_hash_end);
266                 pos = next;
267                 if (pos == MDS_DIR_END_OFF) {
268                         /*
269                          * End of directory reached.
270                          */
271                         done = 1;
272                         ll_release_page(inode, page, false);
273                 } else {
274                         /*
275                          * Normal case: continue to the next
276                          * page.
277                          */
278                         ll_release_page(inode, page,
279                                         le32_to_cpu(dp->ldp_flags) &
280                                         LDF_COLLIDE);
281                         next = pos;
282                         page = ll_get_dir_page(inode, op_data, pos,
283                                                &chain);
284                 }
285         }
286 #ifdef HAVE_DIR_CONTEXT
287         ctx->pos = pos;
288 #else
289         *ppos = pos;
290 #endif
291         ll_dir_chain_fini(&chain);
292         RETURN(rc);
293 }
294
295 #ifdef HAVE_DIR_CONTEXT
296 static int ll_iterate(struct file *filp, struct dir_context *ctx)
297 #else
298 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
299 #endif
300 {
301         struct inode            *inode  = filp->f_dentry->d_inode;
302         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
303         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
304         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
305         int                     api32   = ll_need_32bit_api(sbi);
306         struct md_op_data       *op_data;
307         __u64                   pos;
308         int                     rc;
309         ENTRY;
310
311         if (lfd != NULL)
312                 pos = lfd->lfd_pos;
313         else
314                 pos = 0;
315
316         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
317                "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
318                inode, (unsigned long)pos, i_size_read(inode), api32);
319
320         if (pos == MDS_DIR_END_OFF)
321                 /*
322                  * end-of-file.
323                  */
324                 GOTO(out, rc = 0);
325
326         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
327                                      LUSTRE_OPC_ANY, inode);
328         if (IS_ERR(op_data))
329                 GOTO(out, rc = PTR_ERR(op_data));
330
331         if (unlikely(op_data->op_mea1 != NULL)) {
332                 /* This is only needed for striped dir to fill ..,
333                  * see lmv_read_entry */
334                 if (filp->f_dentry->d_parent != NULL &&
335                     filp->f_dentry->d_parent->d_inode != NULL) {
336                         __u64 ibits = MDS_INODELOCK_UPDATE;
337                         struct inode *parent =
338                                 filp->f_dentry->d_parent->d_inode;
339
340                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
341                                 op_data->op_fid3 = *ll_inode2fid(parent);
342                 }
343
344                 /* If it can not find in cache, do lookup .. on the master
345                  * object */
346                 if (fid_is_zero(&op_data->op_fid3)) {
347                         rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
348                         if (rc != 0) {
349                                 ll_finish_md_op_data(op_data);
350                                 RETURN(rc);
351                         }
352                 }
353         }
354         op_data->op_max_pages = sbi->ll_md_brw_pages;
355 #ifdef HAVE_DIR_CONTEXT
356         ctx->pos = pos;
357         rc = ll_dir_read(inode, &pos, op_data, ctx);
358         pos = ctx->pos;
359 #else
360         rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
361 #endif
362         if (lfd != NULL)
363                 lfd->lfd_pos = pos;
364
365         if (pos == MDS_DIR_END_OFF) {
366                 if (api32)
367                         pos = LL_DIR_END_OFF_32BIT;
368                 else
369                         pos = LL_DIR_END_OFF;
370         } else {
371                 if (api32 && hash64)
372                         pos = pos >> 32;
373         }
374 #ifdef HAVE_DIR_CONTEXT
375         ctx->pos = pos;
376 #else
377         filp->f_pos = pos;
378 #endif
379         ll_finish_md_op_data(op_data);
380         filp->f_version = inode->i_version;
381
382 out:
383         if (!rc)
384                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
385
386         RETURN(rc);
387 }
388
389 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
390 {
391         struct mgs_send_param *msp;
392         int rc = 0;
393
394         OBD_ALLOC_PTR(msp);
395         if (!msp)
396                 return -ENOMEM;
397
398         strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
399         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
400                                 sizeof(struct mgs_send_param), msp, NULL);
401         if (rc)
402                 CERROR("Failed to set parameter: %d\n", rc);
403         OBD_FREE_PTR(msp);
404
405         return rc;
406 }
407
408 /**
409  * Create striped directory with specified stripe(@lump)
410  *
411  * param[in]parent      the parent of the directory.
412  * param[in]lump        the specified stripes.
413  * param[in]dirname     the name of the directory.
414  * param[in]mode        the specified mode of the directory.
415  *
416  * retval               =0 if striped directory is being created successfully.
417  *                      <0 if the creation is failed.
418  */
419 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
420                                const char *dirname, umode_t mode)
421 {
422         struct ptlrpc_request *request = NULL;
423         struct md_op_data *op_data;
424         struct ll_sb_info *sbi = ll_i2sbi(parent);
425         int err;
426         ENTRY;
427
428         if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
429                 RETURN(-EINVAL);
430
431         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) name %s"
432                "stripe_offset %d, stripe_count: %u\n",
433                PFID(ll_inode2fid(parent)), parent, dirname,
434                (int)lump->lum_stripe_offset, lump->lum_stripe_count);
435
436         if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
437                 lustre_swab_lmv_user_md(lump);
438
439         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
440                 mode &= ~current_umask();
441         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
442         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
443                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
444                                      lump);
445         if (IS_ERR(op_data))
446                 GOTO(err_exit, err = PTR_ERR(op_data));
447
448         op_data->op_cli_flags |= CLI_SET_MEA;
449         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
450                         from_kuid(&init_user_ns, current_fsuid()),
451                         from_kgid(&init_user_ns, current_fsgid()),
452                         cfs_curproc_cap_pack(), 0, &request);
453         ll_finish_md_op_data(op_data);
454         if (err)
455                 GOTO(err_exit, err);
456 err_exit:
457         ptlrpc_req_finished(request);
458         return err;
459 }
460
461 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
462                      int set_default)
463 {
464         struct ll_sb_info *sbi = ll_i2sbi(inode);
465         struct md_op_data *op_data;
466         struct ptlrpc_request *req = NULL;
467         int rc = 0;
468         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
469         struct obd_device *mgc = lsi->lsi_mgc;
470         int lum_size;
471         ENTRY;
472
473         if (lump != NULL) {
474                 /*
475                  * This is coming from userspace, so should be in
476                  * local endian.  But the MDS would like it in little
477                  * endian, so we swab it before we send it.
478                  */
479                 switch (lump->lmm_magic) {
480                 case LOV_USER_MAGIC_V1: {
481                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
482                                 lustre_swab_lov_user_md_v1(lump);
483                         lum_size = sizeof(struct lov_user_md_v1);
484                         break;
485                 }
486                 case LOV_USER_MAGIC_V3: {
487                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
488                                 lustre_swab_lov_user_md_v3(
489                                         (struct lov_user_md_v3 *)lump);
490                         lum_size = sizeof(struct lov_user_md_v3);
491                         break;
492                 }
493                 case LMV_USER_MAGIC: {
494                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
495                                 lustre_swab_lmv_user_md(
496                                         (struct lmv_user_md *)lump);
497                         lum_size = sizeof(struct lmv_user_md);
498                         break;
499                 }
500                 default: {
501                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
502                                         " %#08x != %#08x nor %#08x\n",
503                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
504                                         LOV_USER_MAGIC_V3);
505                         RETURN(-EINVAL);
506                 }
507                 }
508         } else {
509                 lum_size = sizeof(struct lov_user_md_v1);
510         }
511
512         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
513                                      LUSTRE_OPC_ANY, NULL);
514         if (IS_ERR(op_data))
515                 RETURN(PTR_ERR(op_data));
516
517         /* swabbing is done in lov_setstripe() on server side */
518         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
519                         NULL, 0, &req, NULL);
520         ll_finish_md_op_data(op_data);
521         ptlrpc_req_finished(req);
522         if (rc) {
523                 if (rc != -EPERM && rc != -EACCES)
524                         CERROR("mdc_setattr fails: rc = %d\n", rc);
525         }
526
527         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
528          LOV_USER_MAGIC_V3 have the same initial fields so we do not
529          need the make the distiction between the 2 versions */
530         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
531                 char *param = NULL;
532                 char *buf;
533
534                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
535                 if (param == NULL)
536                         GOTO(end, rc = -ENOMEM);
537
538                 buf = param;
539                 /* Get fsname and assume devname to be -MDT0000. */
540                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
541                 strcat(buf, "-MDT0000.lov");
542                 buf += strlen(buf);
543
544                 /* Set root stripesize */
545                 sprintf(buf, ".stripesize=%u",
546                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
547                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
548                 if (rc)
549                         GOTO(end, rc);
550
551                 /* Set root stripecount */
552                 sprintf(buf, ".stripecount=%hd",
553                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
554                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
555                 if (rc)
556                         GOTO(end, rc);
557
558                 /* Set root stripeoffset */
559                 sprintf(buf, ".stripeoffset=%hd",
560                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
561                         (typeof(lump->lmm_stripe_offset))(-1));
562                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
563
564 end:
565                 if (param != NULL)
566                         OBD_FREE(param, MGS_PARAM_MAXLEN);
567         }
568         RETURN(rc);
569 }
570
571 /**
572  * This function will be used to get default LOV/LMV/Default LMV
573  * @valid will be used to indicate which stripe it will retrieve
574  *      OBD_MD_MEA              LMV stripe EA
575  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
576  *      otherwise               Default LOV EA.
577  * Each time, it can only retrieve 1 stripe EA
578  **/
579 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
580                      struct ptlrpc_request **request, obd_valid valid)
581 {
582         struct ll_sb_info *sbi = ll_i2sbi(inode);
583         struct mdt_body   *body;
584         struct lov_mds_md *lmm = NULL;
585         struct ptlrpc_request *req = NULL;
586         int rc, lmm_size;
587         struct md_op_data *op_data;
588         ENTRY;
589
590         rc = ll_get_default_mdsize(sbi, &lmm_size);
591         if (rc)
592                 RETURN(rc);
593
594         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
595                                      0, lmm_size, LUSTRE_OPC_ANY,
596                                      NULL);
597         if (IS_ERR(op_data))
598                 RETURN(PTR_ERR(op_data));
599
600         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
601         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
602         ll_finish_md_op_data(op_data);
603         if (rc < 0) {
604                 CDEBUG(D_INFO, "md_getattr failed on inode "
605                        DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
606                 GOTO(out, rc);
607         }
608
609         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
610         LASSERT(body != NULL);
611
612         lmm_size = body->mbo_eadatasize;
613
614         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
615             lmm_size == 0) {
616                 GOTO(out, rc = -ENODATA);
617         }
618
619         lmm = req_capsule_server_sized_get(&req->rq_pill,
620                                            &RMF_MDT_MD, lmm_size);
621         LASSERT(lmm != NULL);
622
623         /*
624          * This is coming from the MDS, so is probably in
625          * little endian.  We convert it to host endian before
626          * passing it to userspace.
627          */
628         /* We don't swab objects for directories */
629         switch (le32_to_cpu(lmm->lmm_magic)) {
630         case LOV_MAGIC_V1:
631                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
632                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
633                 break;
634         case LOV_MAGIC_V3:
635                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
636                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
637                 break;
638         case LMV_MAGIC_V1:
639                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
640                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
641                 break;
642         case LMV_USER_MAGIC:
643                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
644                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
645                 break;
646         default:
647                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
648                 rc = -EPROTO;
649         }
650 out:
651         *plmm = lmm;
652         *plmm_size = lmm_size;
653         *request = req;
654         return rc;
655 }
656
657 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
658 {
659         struct md_op_data       *op_data;
660         int                     rc;
661         int                     mdt_index;
662         ENTRY;
663
664         OBD_ALLOC_PTR(op_data);
665         if (op_data == NULL)
666                 RETURN(-ENOMEM);
667
668         op_data->op_flags |= MF_GET_MDT_IDX;
669         op_data->op_fid1 = *fid;
670         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
671         mdt_index = op_data->op_mds;
672         OBD_FREE_PTR(op_data);
673         if (rc < 0)
674                 RETURN(rc);
675
676         RETURN(mdt_index);
677 }
678
679 /*
680  *  Get MDT index for the inode.
681  */
682 int ll_get_mdt_idx(struct inode *inode)
683 {
684         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
685 }
686
687 /**
688  * Generic handler to do any pre-copy work.
689  *
690  * It send a first hsm_progress (with extent length == 0) to coordinator as a
691  * first information for it that real work has started.
692  *
693  * Moreover, for a ARCHIVE request, it will sample the file data version and
694  * store it in \a copy.
695  *
696  * \return 0 on success.
697  */
698 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
699 {
700         struct ll_sb_info               *sbi = ll_s2sbi(sb);
701         struct hsm_progress_kernel       hpk;
702         int                              rc;
703         ENTRY;
704
705         /* Forge a hsm_progress based on data from copy. */
706         hpk.hpk_fid = copy->hc_hai.hai_fid;
707         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
708         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
709         hpk.hpk_extent.length = 0;
710         hpk.hpk_flags = 0;
711         hpk.hpk_errval = 0;
712         hpk.hpk_data_version = 0;
713
714
715         /* For archive request, we need to read the current file version. */
716         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
717                 struct inode    *inode;
718                 __u64            data_version = 0;
719
720                 /* Get inode for this fid */
721                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
722                 if (IS_ERR(inode)) {
723                         hpk.hpk_flags |= HP_FLAG_RETRY;
724                         /* hpk_errval is >= 0 */
725                         hpk.hpk_errval = -PTR_ERR(inode);
726                         GOTO(progress, rc = PTR_ERR(inode));
727                 }
728
729                 /* Read current file data version */
730                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
731                 iput(inode);
732                 if (rc != 0) {
733                         CDEBUG(D_HSM, "Could not read file data version of "
734                                       DFID" (rc = %d). Archive request ("
735                                       LPX64") could not be done.\n",
736                                       PFID(&copy->hc_hai.hai_fid), rc,
737                                       copy->hc_hai.hai_cookie);
738                         hpk.hpk_flags |= HP_FLAG_RETRY;
739                         /* hpk_errval must be >= 0 */
740                         hpk.hpk_errval = -rc;
741                         GOTO(progress, rc);
742                 }
743
744                 /* Store it the hsm_copy for later copytool use.
745                  * Always modified even if no lsm. */
746                 copy->hc_data_version = data_version;
747         }
748
749 progress:
750         /* On error, the request should be considered as completed */
751         if (hpk.hpk_errval > 0)
752                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
753         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
754                            &hpk, NULL);
755
756         RETURN(rc);
757 }
758
759 /**
760  * Generic handler to do any post-copy work.
761  *
762  * It will send the last hsm_progress update to coordinator to inform it
763  * that copy is finished and whether it was successful or not.
764  *
765  * Moreover,
766  * - for ARCHIVE request, it will sample the file data version and compare it
767  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
768  *   will be considered as failed.
769  * - for RESTORE request, it will sample the file data version and send it to
770  *   coordinator which is useful if the file was imported as 'released'.
771  *
772  * \return 0 on success.
773  */
774 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
775 {
776         struct ll_sb_info               *sbi = ll_s2sbi(sb);
777         struct hsm_progress_kernel       hpk;
778         int                              rc;
779         ENTRY;
780
781         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
782         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
783          * initialized if copy_end was called with copy == NULL.
784          */
785
786         /* Forge a hsm_progress based on data from copy. */
787         hpk.hpk_fid = copy->hc_hai.hai_fid;
788         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
789         hpk.hpk_extent = copy->hc_hai.hai_extent;
790         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
791         hpk.hpk_errval = copy->hc_errval;
792         hpk.hpk_data_version = 0;
793
794         /* For archive request, we need to check the file data was not changed.
795          *
796          * For restore request, we need to send the file data version, this is
797          * useful when the file was created using hsm_import.
798          */
799         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
800              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
801             (copy->hc_errval == 0)) {
802                 struct inode    *inode;
803                 __u64            data_version = 0;
804
805                 /* Get lsm for this fid */
806                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
807                 if (IS_ERR(inode)) {
808                         hpk.hpk_flags |= HP_FLAG_RETRY;
809                         /* hpk_errval must be >= 0 */
810                         hpk.hpk_errval = -PTR_ERR(inode);
811                         GOTO(progress, rc = PTR_ERR(inode));
812                 }
813
814                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
815                 iput(inode);
816                 if (rc) {
817                         CDEBUG(D_HSM, "Could not read file data version. "
818                                       "Request could not be confirmed.\n");
819                         if (hpk.hpk_errval == 0)
820                                 hpk.hpk_errval = -rc;
821                         GOTO(progress, rc);
822                 }
823
824                 /* Store it the hsm_copy for later copytool use.
825                  * Always modified even if no lsm. */
826                 hpk.hpk_data_version = data_version;
827
828                 /* File could have been stripped during archiving, so we need
829                  * to check anyway. */
830                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
831                     (copy->hc_data_version != data_version)) {
832                         CDEBUG(D_HSM, "File data version mismatched. "
833                               "File content was changed during archiving. "
834                                DFID", start:"LPX64" current:"LPX64"\n",
835                                PFID(&copy->hc_hai.hai_fid),
836                                copy->hc_data_version, data_version);
837                         /* File was changed, send error to cdt. Do not ask for
838                          * retry because if a file is modified frequently,
839                          * the cdt will loop on retried archive requests.
840                          * The policy engine will ask for a new archive later
841                          * when the file will not be modified for some tunable
842                          * time */
843                         /* we do not notify caller */
844                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
845                         /* hpk_errval must be >= 0 */
846                         hpk.hpk_errval = EBUSY;
847                 }
848
849         }
850
851 progress:
852         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
853                            &hpk, NULL);
854
855         RETURN(rc);
856 }
857
858
859 static int copy_and_ioctl(int cmd, struct obd_export *exp,
860                           const void __user *data, size_t size)
861 {
862         void *copy;
863         int rc;
864
865         OBD_ALLOC(copy, size);
866         if (copy == NULL)
867                 return -ENOMEM;
868
869         if (copy_from_user(copy, data, size)) {
870                 rc = -EFAULT;
871                 goto out;
872         }
873
874         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
875 out:
876         OBD_FREE(copy, size);
877
878         return rc;
879 }
880
881 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
882 {
883         int cmd = qctl->qc_cmd;
884         int type = qctl->qc_type;
885         int id = qctl->qc_id;
886         int valid = qctl->qc_valid;
887         int rc = 0;
888         ENTRY;
889
890         switch (cmd) {
891         case LUSTRE_Q_INVALIDATE:
892         case LUSTRE_Q_FINVALIDATE:
893         case Q_QUOTAON:
894         case Q_QUOTAOFF:
895         case Q_SETQUOTA:
896         case Q_SETINFO:
897                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
898                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
899                         RETURN(-EPERM);
900                 break;
901         case Q_GETQUOTA:
902                 if (((type == USRQUOTA &&
903                       !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
904                      (type == GRPQUOTA &&
905                       !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
906                     (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
907                      sbi->ll_flags & LL_SBI_RMT_CLIENT))
908                         RETURN(-EPERM);
909                 break;
910         case Q_GETINFO:
911                 break;
912         default:
913                 CERROR("unsupported quotactl op: %#x\n", cmd);
914                 RETURN(-ENOTTY);
915         }
916
917         if (valid != QC_GENERAL) {
918                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
919                         RETURN(-EOPNOTSUPP);
920
921                 if (cmd == Q_GETINFO)
922                         qctl->qc_cmd = Q_GETOINFO;
923                 else if (cmd == Q_GETQUOTA)
924                         qctl->qc_cmd = Q_GETOQUOTA;
925                 else
926                         RETURN(-EINVAL);
927
928                 switch (valid) {
929                 case QC_MDTIDX:
930                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
931                                            sizeof(*qctl), qctl, NULL);
932                         break;
933                 case QC_OSTIDX:
934                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
935                                            sizeof(*qctl), qctl, NULL);
936                         break;
937                 case QC_UUID:
938                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
939                                            sizeof(*qctl), qctl, NULL);
940                         if (rc == -EAGAIN)
941                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
942                                                    sbi->ll_dt_exp,
943                                                    sizeof(*qctl), qctl, NULL);
944                         break;
945                 default:
946                         rc = -EINVAL;
947                         break;
948                 }
949
950                 if (rc)
951                         RETURN(rc);
952
953                 qctl->qc_cmd = cmd;
954         } else {
955                 struct obd_quotactl *oqctl;
956
957                 OBD_ALLOC_PTR(oqctl);
958                 if (oqctl == NULL)
959                         RETURN(-ENOMEM);
960
961                 QCTL_COPY(oqctl, qctl);
962                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
963                 if (rc) {
964                         if (rc != -EALREADY && cmd == Q_QUOTAON) {
965                                 oqctl->qc_cmd = Q_QUOTAOFF;
966                                 obd_quotactl(sbi->ll_md_exp, oqctl);
967                         }
968                         OBD_FREE_PTR(oqctl);
969                         RETURN(rc);
970                 }
971                 /* If QIF_SPACE is not set, client should collect the
972                  * space usage from OSSs by itself */
973                 if (cmd == Q_GETQUOTA &&
974                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
975                     !oqctl->qc_dqblk.dqb_curspace) {
976                         struct obd_quotactl *oqctl_tmp;
977
978                         OBD_ALLOC_PTR(oqctl_tmp);
979                         if (oqctl_tmp == NULL)
980                                 GOTO(out, rc = -ENOMEM);
981
982                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
983                         oqctl_tmp->qc_id = oqctl->qc_id;
984                         oqctl_tmp->qc_type = oqctl->qc_type;
985
986                         /* collect space usage from OSTs */
987                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
988                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
989                         if (!rc || rc == -EREMOTEIO) {
990                                 oqctl->qc_dqblk.dqb_curspace =
991                                         oqctl_tmp->qc_dqblk.dqb_curspace;
992                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
993                         }
994
995                         /* collect space & inode usage from MDTs */
996                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
997                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
998                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
999                         if (!rc || rc == -EREMOTEIO) {
1000                                 oqctl->qc_dqblk.dqb_curspace +=
1001                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1002                                 oqctl->qc_dqblk.dqb_curinodes =
1003                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1004                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1005                         } else {
1006                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1007                         }
1008
1009                         OBD_FREE_PTR(oqctl_tmp);
1010                 }
1011 out:
1012                 QCTL_COPY(qctl, oqctl);
1013                 OBD_FREE_PTR(oqctl);
1014         }
1015
1016         RETURN(rc);
1017 }
1018
1019 static char *
1020 ll_getname(const char __user *filename)
1021 {
1022         int ret = 0, len;
1023         char *tmp = __getname();
1024
1025         if (!tmp)
1026                 return ERR_PTR(-ENOMEM);
1027
1028         len = strncpy_from_user(tmp, filename, PATH_MAX);
1029         if (len == 0)
1030                 ret = -ENOENT;
1031         else if (len > PATH_MAX)
1032                 ret = -ENAMETOOLONG;
1033
1034         if (ret) {
1035                 __putname(tmp);
1036                 tmp =  ERR_PTR(ret);
1037         }
1038         return tmp;
1039 }
1040
1041 #define ll_putname(filename) __putname(filename)
1042
1043 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1044 {
1045         struct inode *inode = file->f_dentry->d_inode;
1046         struct ll_sb_info *sbi = ll_i2sbi(inode);
1047         struct obd_ioctl_data *data;
1048         int rc = 0;
1049         ENTRY;
1050
1051         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1052                PFID(ll_inode2fid(inode)), inode, cmd);
1053
1054         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1055         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1056                 return -ENOTTY;
1057
1058         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1059         switch(cmd) {
1060         case FSFILT_IOC_GETFLAGS:
1061         case FSFILT_IOC_SETFLAGS:
1062                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1063         case FSFILT_IOC_GETVERSION_OLD:
1064         case FSFILT_IOC_GETVERSION:
1065                 RETURN(put_user(inode->i_generation, (int *)arg));
1066         /* We need to special case any other ioctls we want to handle,
1067          * to send them to the MDS/OST as appropriate and to properly
1068          * network encode the arg field.
1069         case FSFILT_IOC_SETVERSION_OLD:
1070         case FSFILT_IOC_SETVERSION:
1071         */
1072         case LL_IOC_GET_MDTIDX: {
1073                 int mdtidx;
1074
1075                 mdtidx = ll_get_mdt_idx(inode);
1076                 if (mdtidx < 0)
1077                         RETURN(mdtidx);
1078
1079                 if (put_user((int)mdtidx, (int*)arg))
1080                         RETURN(-EFAULT);
1081
1082                 return 0;
1083         }
1084         case IOC_MDC_LOOKUP: {
1085                 int namelen, len = 0;
1086                 char *buf = NULL;
1087                 char *filename;
1088
1089                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1090                 if (rc != 0)
1091                         RETURN(rc);
1092                 data = (void *)buf;
1093
1094                 filename = data->ioc_inlbuf1;
1095                 namelen = strlen(filename);
1096                 if (namelen < 1) {
1097                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1098                         GOTO(out_free, rc = -EINVAL);
1099                 }
1100
1101                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL);
1102                 if (rc < 0) {
1103                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1104                                ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1105                                filename, rc);
1106                         GOTO(out_free, rc);
1107                 }
1108 out_free:
1109                 obd_ioctl_freedata(buf, len);
1110                 return rc;
1111         }
1112         case LL_IOC_LMV_SETSTRIPE: {
1113                 struct lmv_user_md  *lum;
1114                 char            *buf = NULL;
1115                 char            *filename;
1116                 int              namelen = 0;
1117                 int              lumlen = 0;
1118                 umode_t          mode;
1119                 int              len;
1120                 int              rc;
1121
1122                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1123                 if (rc)
1124                         RETURN(rc);
1125
1126                 data = (void *)buf;
1127                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1128                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1129                         GOTO(lmv_out_free, rc = -EINVAL);
1130
1131                 filename = data->ioc_inlbuf1;
1132                 namelen = data->ioc_inllen1;
1133
1134                 if (namelen < 1) {
1135                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1136                         GOTO(lmv_out_free, rc = -EINVAL);
1137                 }
1138                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1139                 lumlen = data->ioc_inllen2;
1140
1141                 if (lum->lum_magic != LMV_USER_MAGIC ||
1142                     lumlen != sizeof(*lum)) {
1143                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1144                                filename, lum->lum_magic, lumlen, -EFAULT);
1145                         GOTO(lmv_out_free, rc = -EINVAL);
1146                 }
1147
1148 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1149                 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1150 #else
1151                 mode = data->ioc_type;
1152 #endif
1153                 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1154 lmv_out_free:
1155                 obd_ioctl_freedata(buf, len);
1156                 RETURN(rc);
1157
1158         }
1159         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1160                 struct lmv_user_md        lum;
1161                 struct lmv_user_md __user *ulump =
1162                                         (struct lmv_user_md __user *)arg;
1163                 int                       rc;
1164
1165                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1166                         RETURN(-EFAULT);
1167
1168                 if (lum.lum_magic != LMV_USER_MAGIC)
1169                         RETURN(-EINVAL);
1170
1171                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1172
1173                 RETURN(rc);
1174         }
1175         case LL_IOC_LOV_SETSTRIPE: {
1176                 struct lov_user_md_v3 lumv3;
1177                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1178                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1179                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1180
1181                 int set_default = 0;
1182
1183                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1184                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1185                         sizeof(lumv3p->lmm_objects[0]));
1186                 /* first try with v1 which is smaller than v3 */
1187                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1188                         RETURN(-EFAULT);
1189
1190                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1191                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1192                                 RETURN(-EFAULT);
1193                 }
1194
1195                 if (inode->i_sb->s_root == file->f_dentry)
1196                         set_default = 1;
1197
1198                 /* in v1 and v3 cases lumv1 points to data */
1199                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1200
1201                 RETURN(rc);
1202         }
1203         case LL_IOC_LMV_GETSTRIPE: {
1204                 struct lmv_user_md __user *ulmv =
1205                                         (struct lmv_user_md __user *)arg;
1206                 struct lmv_user_md      lum;
1207                 struct ptlrpc_request   *request = NULL;
1208                 union lmv_mds_md        *lmm = NULL;
1209                 int                     lmmsize;
1210                 obd_valid               valid = 0;
1211                 struct lmv_user_md      *tmp = NULL;
1212                 int                     mdt_index;
1213                 int                     lum_size;
1214                 int                     stripe_count;
1215                 int                     i;
1216                 int                     rc;
1217
1218                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1219                         RETURN(-EFAULT);
1220
1221                 /* lum_magic will indicate which stripe the ioctl will like
1222                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1223                  * is for default LMV stripe */
1224                 if (lum.lum_magic == LMV_MAGIC_V1)
1225                         valid |= OBD_MD_MEA;
1226                 else if (lum.lum_magic == LMV_USER_MAGIC)
1227                         valid |= OBD_MD_DEFAULT_MEA;
1228                 else
1229                         RETURN(-EINVAL);
1230
1231                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1232                                       valid);
1233                 if (rc != 0)
1234                         GOTO(finish_req, rc);
1235
1236                 /* Get default LMV EA */
1237                 if (lum.lum_magic == LMV_USER_MAGIC) {
1238                         if (rc != 0)
1239                                 GOTO(finish_req, rc);
1240
1241                         if (lmmsize > sizeof(*ulmv))
1242                                 GOTO(finish_req, rc = -EINVAL);
1243
1244                         if (copy_to_user(ulmv, lmm, lmmsize))
1245                                 GOTO(finish_req, rc = -EFAULT);
1246
1247                         GOTO(finish_req, rc);
1248                 }
1249
1250                 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1251                 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1252                 OBD_ALLOC(tmp, lum_size);
1253                 if (tmp == NULL)
1254                         GOTO(finish_req, rc = -ENOMEM);
1255
1256                 mdt_index = ll_get_mdt_idx(inode);
1257                 if (mdt_index < 0)
1258                         GOTO(out_tmp, rc = -ENOMEM);
1259
1260                 tmp->lum_magic = LMV_MAGIC_V1;
1261                 tmp->lum_stripe_count = 0;
1262                 tmp->lum_stripe_offset = mdt_index;
1263                 for (i = 0; i < stripe_count; i++) {
1264                         struct lu_fid   fid;
1265
1266                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1267                         mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1268                         if (mdt_index < 0)
1269                                 GOTO(out_tmp, rc = mdt_index);
1270
1271                         tmp->lum_objects[i].lum_mds = mdt_index;
1272                         tmp->lum_objects[i].lum_fid = fid;
1273                         tmp->lum_stripe_count++;
1274                 }
1275
1276                 if (copy_to_user(ulmv, tmp, lum_size))
1277                         GOTO(out_tmp, rc = -EFAULT);
1278 out_tmp:
1279                 OBD_FREE(tmp, lum_size);
1280 finish_req:
1281                 ptlrpc_req_finished(request);
1282                 return rc;
1283         }
1284
1285         case LL_IOC_REMOVE_ENTRY: {
1286                 char            *filename = NULL;
1287                 int              namelen = 0;
1288                 int              rc;
1289
1290                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1291                  * unsupported server, which might crash the server(LU-2730),
1292                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1293                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1294                  * server will support REINT_RMENTRY XXX*/
1295                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1296                         RETURN(-ENOTSUPP);
1297
1298                 filename = ll_getname((const char *)arg);
1299                 if (IS_ERR(filename))
1300                         RETURN(PTR_ERR(filename));
1301
1302                 namelen = strlen(filename);
1303                 if (namelen < 1)
1304                         GOTO(out_rmdir, rc = -EINVAL);
1305
1306                 rc = ll_rmdir_entry(inode, filename, namelen);
1307 out_rmdir:
1308                 if (filename)
1309                         ll_putname(filename);
1310                 RETURN(rc);
1311         }
1312         case LL_IOC_LOV_SWAP_LAYOUTS:
1313                 RETURN(-EPERM);
1314         case IOC_OBD_STATFS:
1315                 RETURN(ll_obd_statfs(inode, (void *)arg));
1316         case LL_IOC_LOV_GETSTRIPE:
1317         case LL_IOC_MDC_GETINFO:
1318         case IOC_MDC_GETFILEINFO:
1319         case IOC_MDC_GETFILESTRIPE: {
1320                 struct ptlrpc_request *request = NULL;
1321                 struct lov_user_md *lump;
1322                 struct lov_mds_md *lmm = NULL;
1323                 struct mdt_body *body;
1324                 char *filename = NULL;
1325                 int lmmsize;
1326
1327                 if (cmd == IOC_MDC_GETFILEINFO ||
1328                     cmd == IOC_MDC_GETFILESTRIPE) {
1329                         filename = ll_getname((const char *)arg);
1330                         if (IS_ERR(filename))
1331                                 RETURN(PTR_ERR(filename));
1332
1333                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1334                                                       &lmmsize, &request);
1335                 } else {
1336                         rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1337                                               &request, 0);
1338                 }
1339
1340                 if (request) {
1341                         body = req_capsule_server_get(&request->rq_pill,
1342                                                       &RMF_MDT_BODY);
1343                         LASSERT(body != NULL);
1344                 } else {
1345                         GOTO(out_req, rc);
1346                 }
1347
1348                 if (rc < 0) {
1349                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1350                                                cmd == LL_IOC_MDC_GETINFO))
1351                                 GOTO(skip_lmm, rc = 0);
1352                         else
1353                                 GOTO(out_req, rc);
1354                 }
1355
1356                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1357                     cmd == LL_IOC_LOV_GETSTRIPE) {
1358                         lump = (struct lov_user_md *)arg;
1359                 } else {
1360                         struct lov_user_mds_data *lmdp;
1361                         lmdp = (struct lov_user_mds_data *)arg;
1362                         lump = &lmdp->lmd_lmm;
1363                 }
1364                 if (copy_to_user(lump, lmm, lmmsize)) {
1365                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1366                                 GOTO(out_req, rc = -EFAULT);
1367                         rc = -EOVERFLOW;
1368                 }
1369         skip_lmm:
1370                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1371                         struct lov_user_mds_data *lmdp;
1372                         lstat_t st = { 0 };
1373
1374                         st.st_dev       = inode->i_sb->s_dev;
1375                         st.st_mode      = body->mbo_mode;
1376                         st.st_nlink     = body->mbo_nlink;
1377                         st.st_uid       = body->mbo_uid;
1378                         st.st_gid       = body->mbo_gid;
1379                         st.st_rdev      = body->mbo_rdev;
1380                         st.st_size      = body->mbo_size;
1381                         st.st_blksize   = PAGE_CACHE_SIZE;
1382                         st.st_blocks    = body->mbo_blocks;
1383                         st.st_atime     = body->mbo_atime;
1384                         st.st_mtime     = body->mbo_mtime;
1385                         st.st_ctime     = body->mbo_ctime;
1386                         st.st_ino       = inode->i_ino;
1387
1388                         lmdp = (struct lov_user_mds_data *)arg;
1389                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1390                                 GOTO(out_req, rc = -EFAULT);
1391                 }
1392
1393                 EXIT;
1394         out_req:
1395                 ptlrpc_req_finished(request);
1396                 if (filename)
1397                         ll_putname(filename);
1398                 return rc;
1399         }
1400         case IOC_LOV_GETINFO: {
1401                 struct lov_user_mds_data *lumd;
1402                 struct lov_stripe_md *lsm;
1403                 struct lov_user_md *lum;
1404                 struct lov_mds_md *lmm;
1405                 int lmmsize;
1406                 lstat_t st;
1407
1408                 lumd = (struct lov_user_mds_data *)arg;
1409                 lum = &lumd->lmd_lmm;
1410
1411                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1412                 if (rc)
1413                         RETURN(rc);
1414
1415                 OBD_ALLOC_LARGE(lmm, lmmsize);
1416                 if (lmm == NULL)
1417                         RETURN(-ENOMEM);
1418
1419                 if (copy_from_user(lmm, lum, lmmsize))
1420                         GOTO(free_lmm, rc = -EFAULT);
1421
1422                 switch (lmm->lmm_magic) {
1423                 case LOV_USER_MAGIC_V1:
1424                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1425                                 break;
1426                         /* swab objects first so that stripes num will be sane */
1427                         lustre_swab_lov_user_md_objects(
1428                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1429                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1430                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1431                         break;
1432                 case LOV_USER_MAGIC_V3:
1433                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1434                                 break;
1435                         /* swab objects first so that stripes num will be sane */
1436                         lustre_swab_lov_user_md_objects(
1437                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1438                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1439                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1440                         break;
1441                 default:
1442                         GOTO(free_lmm, rc = -EINVAL);
1443                 }
1444
1445                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1446                 if (rc < 0)
1447                         GOTO(free_lmm, rc = -ENOMEM);
1448
1449                 /* Perform glimpse_size operation. */
1450                 memset(&st, 0, sizeof(st));
1451
1452                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1453                 if (rc)
1454                         GOTO(free_lsm, rc);
1455
1456                 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1457                         GOTO(free_lsm, rc = -EFAULT);
1458
1459                 EXIT;
1460         free_lsm:
1461                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1462         free_lmm:
1463                 OBD_FREE_LARGE(lmm, lmmsize);
1464                 return rc;
1465         }
1466         case OBD_IOC_QUOTACHECK: {
1467                 struct obd_quotactl *oqctl;
1468                 int error = 0;
1469
1470                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1471                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1472                         RETURN(-EPERM);
1473
1474                 OBD_ALLOC_PTR(oqctl);
1475                 if (!oqctl)
1476                         RETURN(-ENOMEM);
1477                 oqctl->qc_type = arg;
1478                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1479                 if (rc < 0) {
1480                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1481                         error = rc;
1482                 }
1483
1484                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1485                 if (rc < 0)
1486                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1487
1488                 OBD_FREE_PTR(oqctl);
1489                 return error ?: rc;
1490         }
1491         case OBD_IOC_POLL_QUOTACHECK: {
1492                 struct if_quotacheck *check;
1493
1494                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1495                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1496                         RETURN(-EPERM);
1497
1498                 OBD_ALLOC_PTR(check);
1499                 if (!check)
1500                         RETURN(-ENOMEM);
1501
1502                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1503                                    NULL);
1504                 if (rc) {
1505                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1506                         if (copy_to_user((void *)arg, check,
1507                                              sizeof(*check)))
1508                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1509                         GOTO(out_poll, rc);
1510                 }
1511
1512                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1513                                    NULL);
1514                 if (rc) {
1515                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1516                         if (copy_to_user((void *)arg, check,
1517                                              sizeof(*check)))
1518                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1519                         GOTO(out_poll, rc);
1520                 }
1521         out_poll:
1522                 OBD_FREE_PTR(check);
1523                 RETURN(rc);
1524         }
1525 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 53, 0)
1526         case LL_IOC_QUOTACTL_18: {
1527                 /* copy the old 1.x quota struct for internal use, then copy
1528                  * back into old format struct.  For 1.8 compatibility. */
1529                 struct if_quotactl_18 *qctl_18;
1530                 struct if_quotactl *qctl_20;
1531
1532                 OBD_ALLOC_PTR(qctl_18);
1533                 if (!qctl_18)
1534                         RETURN(-ENOMEM);
1535
1536                 OBD_ALLOC_PTR(qctl_20);
1537                 if (!qctl_20)
1538                         GOTO(out_quotactl_18, rc = -ENOMEM);
1539
1540                 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1541                         GOTO(out_quotactl_20, rc = -ENOMEM);
1542
1543                 QCTL_COPY(qctl_20, qctl_18);
1544                 qctl_20->qc_idx = 0;
1545
1546                 /* XXX: dqb_valid was borrowed as a flag to mark that
1547                  *      only mds quota is wanted */
1548                 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1549                     qctl_18->qc_dqblk.dqb_valid) {
1550                         qctl_20->qc_valid = QC_MDTIDX;
1551                         qctl_20->qc_dqblk.dqb_valid = 0;
1552                 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1553                         qctl_20->qc_valid = QC_UUID;
1554                         qctl_20->obd_uuid = qctl_18->obd_uuid;
1555                 } else {
1556                         qctl_20->qc_valid = QC_GENERAL;
1557                 }
1558
1559                 rc = quotactl_ioctl(sbi, qctl_20);
1560
1561                 if (rc == 0) {
1562                         QCTL_COPY(qctl_18, qctl_20);
1563                         qctl_18->obd_uuid = qctl_20->obd_uuid;
1564
1565                         if (copy_to_user((void *)arg, qctl_18,
1566                                              sizeof(*qctl_18)))
1567                                 rc = -EFAULT;
1568                 }
1569
1570         out_quotactl_20:
1571                 OBD_FREE_PTR(qctl_20);
1572         out_quotactl_18:
1573                 OBD_FREE_PTR(qctl_18);
1574                 RETURN(rc);
1575         }
1576 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 53, 0) */
1577         case OBD_IOC_QUOTACTL: {
1578                 struct if_quotactl *qctl;
1579
1580                 OBD_ALLOC_PTR(qctl);
1581                 if (!qctl)
1582                         RETURN(-ENOMEM);
1583
1584                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1585                         GOTO(out_quotactl, rc = -EFAULT);
1586
1587                 rc = quotactl_ioctl(sbi, qctl);
1588
1589                 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1590                         rc = -EFAULT;
1591
1592         out_quotactl:
1593                 OBD_FREE_PTR(qctl);
1594                 RETURN(rc);
1595         }
1596         case OBD_IOC_GETDTNAME:
1597         case OBD_IOC_GETMDNAME:
1598                 RETURN(ll_get_obd_name(inode, cmd, arg));
1599         case LL_IOC_FLUSHCTX:
1600                 RETURN(ll_flush_ctx(inode));
1601 #ifdef CONFIG_FS_POSIX_ACL
1602         case LL_IOC_RMTACL: {
1603             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1604                 inode == inode->i_sb->s_root->d_inode) {
1605                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1606
1607                 LASSERT(fd != NULL);
1608                 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1609                 if (!rc)
1610                         fd->fd_flags |= LL_FILE_RMTACL;
1611                 RETURN(rc);
1612             } else
1613                 RETURN(0);
1614         }
1615 #endif
1616         case LL_IOC_GETOBDCOUNT: {
1617                 int count, vallen;
1618                 struct obd_export *exp;
1619
1620                 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1621                         RETURN(-EFAULT);
1622
1623                 /* get ost count when count is zero, get mdt count otherwise */
1624                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1625                 vallen = sizeof(count);
1626                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1627                                   KEY_TGT_COUNT, &vallen, &count, NULL);
1628                 if (rc) {
1629                         CERROR("get target count failed: %d\n", rc);
1630                         RETURN(rc);
1631                 }
1632
1633                 if (copy_to_user((int *)arg, &count, sizeof(int)))
1634                         RETURN(-EFAULT);
1635
1636                 RETURN(0);
1637         }
1638         case LL_IOC_PATH2FID:
1639                 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1640                                      sizeof(struct lu_fid)))
1641                         RETURN(-EFAULT);
1642                 RETURN(0);
1643         case LL_IOC_GET_CONNECT_FLAGS: {
1644                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1645         }
1646         case OBD_IOC_CHANGELOG_SEND:
1647         case OBD_IOC_CHANGELOG_CLEAR:
1648                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1649                                     sizeof(struct ioc_changelog));
1650                 RETURN(rc);
1651         case OBD_IOC_FID2PATH:
1652                 RETURN(ll_fid2path(inode, (void *)arg));
1653         case LL_IOC_FID2MDTIDX: {
1654                 struct obd_export *exp = ll_i2mdexp(inode);
1655                 struct lu_fid     fid;
1656                 __u32             index;
1657
1658                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1659                                    sizeof(fid)))
1660                         RETURN(-EFAULT);
1661
1662                 /* Call mdc_iocontrol */
1663                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1664                                    &index);
1665                 if (rc != 0)
1666                         RETURN(rc);
1667
1668                 RETURN(index);
1669         }
1670         case LL_IOC_HSM_REQUEST: {
1671                 struct hsm_user_request *hur;
1672                 ssize_t                  totalsize;
1673
1674                 OBD_ALLOC_PTR(hur);
1675                 if (hur == NULL)
1676                         RETURN(-ENOMEM);
1677
1678                 /* We don't know the true size yet; copy the fixed-size part */
1679                 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1680                         OBD_FREE_PTR(hur);
1681                         RETURN(-EFAULT);
1682                 }
1683
1684                 /* Compute the whole struct size */
1685                 totalsize = hur_len(hur);
1686                 OBD_FREE_PTR(hur);
1687                 if (totalsize < 0)
1688                         RETURN(-E2BIG);
1689
1690                 /* Final size will be more than double totalsize */
1691                 if (totalsize >= MDS_MAXREQSIZE / 3)
1692                         RETURN(-E2BIG);
1693
1694                 OBD_ALLOC_LARGE(hur, totalsize);
1695                 if (hur == NULL)
1696                         RETURN(-ENOMEM);
1697
1698                 /* Copy the whole struct */
1699                 if (copy_from_user(hur, (void *)arg, totalsize)) {
1700                         OBD_FREE_LARGE(hur, totalsize);
1701                         RETURN(-EFAULT);
1702                 }
1703
1704                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1705                         const struct lu_fid *fid;
1706                         struct inode *f;
1707                         int i;
1708
1709                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1710                                 fid = &hur->hur_user_item[i].hui_fid;
1711                                 f = search_inode_for_lustre(inode->i_sb, fid);
1712                                 if (IS_ERR(f)) {
1713                                         rc = PTR_ERR(f);
1714                                         break;
1715                                 }
1716
1717                                 rc = ll_hsm_release(f);
1718                                 iput(f);
1719                                 if (rc != 0)
1720                                         break;
1721                         }
1722                 } else {
1723                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1724                                            hur, NULL);
1725                 }
1726
1727                 OBD_FREE_LARGE(hur, totalsize);
1728
1729                 RETURN(rc);
1730         }
1731         case LL_IOC_HSM_PROGRESS: {
1732                 struct hsm_progress_kernel      hpk;
1733                 struct hsm_progress             hp;
1734
1735                 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1736                         RETURN(-EFAULT);
1737
1738                 hpk.hpk_fid = hp.hp_fid;
1739                 hpk.hpk_cookie = hp.hp_cookie;
1740                 hpk.hpk_extent = hp.hp_extent;
1741                 hpk.hpk_flags = hp.hp_flags;
1742                 hpk.hpk_errval = hp.hp_errval;
1743                 hpk.hpk_data_version = 0;
1744
1745                 /* File may not exist in Lustre; all progress
1746                  * reported to Lustre root */
1747                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1748                                    NULL);
1749                 RETURN(rc);
1750         }
1751         case LL_IOC_HSM_CT_START:
1752                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1753                         RETURN(-EPERM);
1754
1755                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1756                                     sizeof(struct lustre_kernelcomm));
1757                 RETURN(rc);
1758
1759         case LL_IOC_HSM_COPY_START: {
1760                 struct hsm_copy *copy;
1761                 int              rc;
1762
1763                 OBD_ALLOC_PTR(copy);
1764                 if (copy == NULL)
1765                         RETURN(-ENOMEM);
1766                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1767                         OBD_FREE_PTR(copy);
1768                         RETURN(-EFAULT);
1769                 }
1770
1771                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1772                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1773                         rc = -EFAULT;
1774
1775                 OBD_FREE_PTR(copy);
1776                 RETURN(rc);
1777         }
1778         case LL_IOC_HSM_COPY_END: {
1779                 struct hsm_copy *copy;
1780                 int              rc;
1781
1782                 OBD_ALLOC_PTR(copy);
1783                 if (copy == NULL)
1784                         RETURN(-ENOMEM);
1785                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1786                         OBD_FREE_PTR(copy);
1787                         RETURN(-EFAULT);
1788                 }
1789
1790                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1791                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1792                         rc = -EFAULT;
1793
1794                 OBD_FREE_PTR(copy);
1795                 RETURN(rc);
1796         }
1797         case LL_IOC_MIGRATE: {
1798                 char            *buf = NULL;
1799                 const char      *filename;
1800                 int             namelen = 0;
1801                 int             len;
1802                 int             rc;
1803                 int             mdtidx;
1804
1805                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1806                 if (rc < 0)
1807                         RETURN(rc);
1808
1809                 data = (struct obd_ioctl_data *)buf;
1810                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1811                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1812                         GOTO(migrate_free, rc = -EINVAL);
1813
1814                 filename = data->ioc_inlbuf1;
1815                 namelen = data->ioc_inllen1;
1816                 /* \0 is packed at the end of filename */
1817                 if (namelen < 1 || namelen != strlen(filename) + 1)
1818                         GOTO(migrate_free, rc = -EINVAL);
1819
1820                 if (data->ioc_inllen2 != sizeof(mdtidx))
1821                         GOTO(migrate_free, rc = -EINVAL);
1822                 mdtidx = *(int *)data->ioc_inlbuf2;
1823
1824                 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1825 migrate_free:
1826                 obd_ioctl_freedata(buf, len);
1827
1828                 RETURN(rc);
1829         }
1830         default:
1831                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1832                                      (void *)arg));
1833         }
1834 }
1835
1836 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1837 {
1838         struct inode *inode = file->f_mapping->host;
1839         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1840         struct ll_sb_info *sbi = ll_i2sbi(inode);
1841         int api32 = ll_need_32bit_api(sbi);
1842         loff_t ret = -EINVAL;
1843         ENTRY;
1844
1845         mutex_lock(&inode->i_mutex);
1846         switch (origin) {
1847                 case SEEK_SET:
1848                         break;
1849                 case SEEK_CUR:
1850                         offset += file->f_pos;
1851                         break;
1852                 case SEEK_END:
1853                         if (offset > 0)
1854                                 GOTO(out, ret);
1855                         if (api32)
1856                                 offset += LL_DIR_END_OFF_32BIT;
1857                         else
1858                                 offset += LL_DIR_END_OFF;
1859                         break;
1860                 default:
1861                         GOTO(out, ret);
1862         }
1863
1864         if (offset >= 0 &&
1865             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1866              (!api32 && offset <= LL_DIR_END_OFF))) {
1867                 if (offset != file->f_pos) {
1868                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1869                             (!api32 && offset == LL_DIR_END_OFF))
1870                                 fd->lfd_pos = MDS_DIR_END_OFF;
1871                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1872                                 fd->lfd_pos = offset << 32;
1873                         else
1874                                 fd->lfd_pos = offset;
1875                         file->f_pos = offset;
1876                         file->f_version = 0;
1877                 }
1878                 ret = offset;
1879         }
1880         GOTO(out, ret);
1881
1882 out:
1883         mutex_unlock(&inode->i_mutex);
1884         return ret;
1885 }
1886
1887 static int ll_dir_open(struct inode *inode, struct file *file)
1888 {
1889         ENTRY;
1890         RETURN(ll_file_open(inode, file));
1891 }
1892
1893 static int ll_dir_release(struct inode *inode, struct file *file)
1894 {
1895         ENTRY;
1896         RETURN(ll_file_release(inode, file));
1897 }
1898
1899 const struct file_operations ll_dir_operations = {
1900         .llseek         = ll_dir_seek,
1901         .open           = ll_dir_open,
1902         .release        = ll_dir_release,
1903         .read           = generic_read_dir,
1904 #ifdef HAVE_DIR_CONTEXT
1905         .iterate        = ll_iterate,
1906 #else
1907         .readdir        = ll_readdir,
1908 #endif
1909         .unlocked_ioctl = ll_dir_ioctl,
1910         .fsync          = ll_fsync,
1911 };