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