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