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