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