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