Whamcloud - gitweb
LU-417 llite: report non-zero blocks on writing client
[fs/lustre-release.git] / lustre / lclient / lcommon_cl.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * cl code shared between vvp and liblustre (and other Lustre clients in the
40  * future).
41  *
42  *   Author: Nikita Danilov <nikita.danilov@sun.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 # include <linux/fs.h>
50 # include <linux/sched.h>
51 # include <linux/mm.h>
52 # include <linux/smp_lock.h>
53 # include <linux/quotaops.h>
54 # include <linux/highmem.h>
55 # include <linux/pagemap.h>
56 # include <linux/rbtree.h>
57 #else /* __KERNEL__ */
58 #include <stdlib.h>
59 #include <string.h>
60 #include <assert.h>
61 #include <time.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/queue.h>
65 #include <fcntl.h>
66 # include <liblustre.h>
67 #endif
68
69 #include <obd.h>
70 #include <obd_support.h>
71 #include <lustre_fid.h>
72 #include <lustre_lite.h>
73 #include <lustre_dlm.h>
74 #include <lustre_ver.h>
75 #include <lustre_mdc.h>
76 #include <cl_object.h>
77
78 #include <lclient.h>
79
80 #ifdef __KERNEL__
81 #include "../llite/llite_internal.h"
82 #else
83 #include "../liblustre/llite_lib.h"
84 #endif
85
86 const struct cl_req_operations ccc_req_ops;
87
88 /*
89  * ccc_ prefix stands for "Common Client Code".
90  */
91
92 static cfs_mem_cache_t *ccc_lock_kmem;
93 static cfs_mem_cache_t *ccc_object_kmem;
94 static cfs_mem_cache_t *ccc_thread_kmem;
95 static cfs_mem_cache_t *ccc_session_kmem;
96 static cfs_mem_cache_t *ccc_req_kmem;
97
98 static struct lu_kmem_descr ccc_caches[] = {
99         {
100                 .ckd_cache = &ccc_lock_kmem,
101                 .ckd_name  = "ccc_lock_kmem",
102                 .ckd_size  = sizeof (struct ccc_lock)
103         },
104         {
105                 .ckd_cache = &ccc_object_kmem,
106                 .ckd_name  = "ccc_object_kmem",
107                 .ckd_size  = sizeof (struct ccc_object)
108         },
109         {
110                 .ckd_cache = &ccc_thread_kmem,
111                 .ckd_name  = "ccc_thread_kmem",
112                 .ckd_size  = sizeof (struct ccc_thread_info),
113         },
114         {
115                 .ckd_cache = &ccc_session_kmem,
116                 .ckd_name  = "ccc_session_kmem",
117                 .ckd_size  = sizeof (struct ccc_session)
118         },
119         {
120                 .ckd_cache = &ccc_req_kmem,
121                 .ckd_name  = "ccc_req_kmem",
122                 .ckd_size  = sizeof (struct ccc_req)
123         },
124         {
125                 .ckd_cache = NULL
126         }
127 };
128
129 /*****************************************************************************
130  *
131  * Vvp device and device type functions.
132  *
133  */
134
135 void *ccc_key_init(const struct lu_context *ctx,
136                           struct lu_context_key *key)
137 {
138         struct ccc_thread_info *info;
139
140         OBD_SLAB_ALLOC_PTR_GFP(info, ccc_thread_kmem, CFS_ALLOC_IO);
141         if (info == NULL)
142                 info = ERR_PTR(-ENOMEM);
143         return info;
144 }
145
146 void ccc_key_fini(const struct lu_context *ctx,
147                          struct lu_context_key *key, void *data)
148 {
149         struct ccc_thread_info *info = data;
150         OBD_SLAB_FREE_PTR(info, ccc_thread_kmem);
151 }
152
153 void *ccc_session_key_init(const struct lu_context *ctx,
154                                   struct lu_context_key *key)
155 {
156         struct ccc_session *session;
157
158         OBD_SLAB_ALLOC_PTR_GFP(session, ccc_session_kmem, CFS_ALLOC_IO);
159         if (session == NULL)
160                 session = ERR_PTR(-ENOMEM);
161         return session;
162 }
163
164 void ccc_session_key_fini(const struct lu_context *ctx,
165                                  struct lu_context_key *key, void *data)
166 {
167         struct ccc_session *session = data;
168         OBD_SLAB_FREE_PTR(session, ccc_session_kmem);
169 }
170
171 struct lu_context_key ccc_key = {
172         .lct_tags = LCT_CL_THREAD,
173         .lct_init = ccc_key_init,
174         .lct_fini = ccc_key_fini
175 };
176
177 struct lu_context_key ccc_session_key = {
178         .lct_tags = LCT_SESSION,
179         .lct_init = ccc_session_key_init,
180         .lct_fini = ccc_session_key_fini
181 };
182
183
184 /* type constructor/destructor: ccc_type_{init,fini,start,stop}(). */
185 // LU_TYPE_INIT_FINI(ccc, &ccc_key, &ccc_session_key);
186
187 int ccc_device_init(const struct lu_env *env, struct lu_device *d,
188                            const char *name, struct lu_device *next)
189 {
190         struct ccc_device  *vdv;
191         int rc;
192         ENTRY;
193
194         vdv = lu2ccc_dev(d);
195         vdv->cdv_next = lu2cl_dev(next);
196
197         LASSERT(d->ld_site != NULL && next->ld_type != NULL);
198         next->ld_site = d->ld_site;
199         rc = next->ld_type->ldt_ops->ldto_device_init(
200                         env, next, next->ld_type->ldt_name, NULL);
201         if (rc == 0) {
202                 lu_device_get(next);
203                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
204         }
205         RETURN(rc);
206 }
207
208 struct lu_device *ccc_device_fini(const struct lu_env *env,
209                                          struct lu_device *d)
210 {
211         return cl2lu_dev(lu2ccc_dev(d)->cdv_next);
212 }
213
214 struct lu_device *ccc_device_alloc(const struct lu_env *env,
215                                    struct lu_device_type *t,
216                                    struct lustre_cfg *cfg,
217                                    const struct lu_device_operations *luops,
218                                    const struct cl_device_operations *clops)
219 {
220         struct ccc_device *vdv;
221         struct lu_device  *lud;
222         struct cl_site    *site;
223         int rc;
224         ENTRY;
225
226         OBD_ALLOC_PTR(vdv);
227         if (vdv == NULL)
228                 RETURN(ERR_PTR(-ENOMEM));
229
230         lud = &vdv->cdv_cl.cd_lu_dev;
231         cl_device_init(&vdv->cdv_cl, t);
232         ccc2lu_dev(vdv)->ld_ops = luops;
233         vdv->cdv_cl.cd_ops = clops;
234
235         OBD_ALLOC_PTR(site);
236         if (site != NULL) {
237                 rc = cl_site_init(site, &vdv->cdv_cl);
238                 if (rc == 0)
239                         rc = lu_site_init_finish(&site->cs_lu);
240                 else {
241                         LASSERT(lud->ld_site == NULL);
242                         CERROR("Cannot init lu_site, rc %d.\n", rc);
243                         OBD_FREE_PTR(site);
244                 }
245         } else
246                 rc = -ENOMEM;
247         if (rc != 0) {
248                 ccc_device_free(env, lud);
249                 lud = ERR_PTR(rc);
250         }
251         RETURN(lud);
252 }
253
254 struct lu_device *ccc_device_free(const struct lu_env *env,
255                                          struct lu_device *d)
256 {
257         struct ccc_device *vdv  = lu2ccc_dev(d);
258         struct cl_site    *site = lu2cl_site(d->ld_site);
259         struct lu_device  *next = cl2lu_dev(vdv->cdv_next);
260
261         if (d->ld_site != NULL) {
262                 cl_site_fini(site);
263                 OBD_FREE_PTR(site);
264         }
265         cl_device_fini(lu2cl_dev(d));
266         OBD_FREE_PTR(vdv);
267         return next;
268 }
269
270 int ccc_req_init(const struct lu_env *env, struct cl_device *dev,
271                         struct cl_req *req)
272 {
273         struct ccc_req *vrq;
274         int result;
275
276         OBD_SLAB_ALLOC_PTR_GFP(vrq, ccc_req_kmem, CFS_ALLOC_IO);
277         if (vrq != NULL) {
278                 cl_req_slice_add(req, &vrq->crq_cl, dev, &ccc_req_ops);
279                 result = 0;
280         } else
281                 result = -ENOMEM;
282         return result;
283 }
284
285 /**
286  * An `emergency' environment used by ccc_inode_fini() when cl_env_get()
287  * fails. Access to this environment is serialized by ccc_inode_fini_guard
288  * mutex.
289  */
290 static struct lu_env *ccc_inode_fini_env = NULL;
291
292 /**
293  * A mutex serializing calls to slp_inode_fini() under extreme memory
294  * pressure, when environments cannot be allocated.
295  */
296 static CFS_DEFINE_MUTEX(ccc_inode_fini_guard);
297 static int dummy_refcheck;
298
299 int ccc_global_init(struct lu_device_type *device_type)
300 {
301         int result;
302
303         result = lu_kmem_init(ccc_caches);
304         if (result)
305                 return result;
306
307         result = lu_device_type_init(device_type);
308         if (result)
309                 goto out_kmem;
310
311         ccc_inode_fini_env = cl_env_alloc(&dummy_refcheck,
312                                           LCT_REMEMBER|LCT_NOREF);
313         if (IS_ERR(ccc_inode_fini_env)) {
314                 result = PTR_ERR(ccc_inode_fini_env);
315                 goto out_device;
316         }
317
318         ccc_inode_fini_env->le_ctx.lc_cookie = 0x4;
319         return 0;
320 out_device:
321         lu_device_type_fini(device_type);
322 out_kmem:
323         lu_kmem_fini(ccc_caches);
324         return result;
325 }
326
327 void ccc_global_fini(struct lu_device_type *device_type)
328 {
329         if (ccc_inode_fini_env != NULL) {
330                 cl_env_put(ccc_inode_fini_env, &dummy_refcheck);
331                 ccc_inode_fini_env = NULL;
332         }
333         lu_device_type_fini(device_type);
334         lu_kmem_fini(ccc_caches);
335 }
336
337 /*****************************************************************************
338  *
339  * Object operations.
340  *
341  */
342
343 struct lu_object *ccc_object_alloc(const struct lu_env *env,
344                                    const struct lu_object_header *unused,
345                                    struct lu_device *dev,
346                                    const struct cl_object_operations *clops,
347                                    const struct lu_object_operations *luops)
348 {
349         struct ccc_object *vob;
350         struct lu_object  *obj;
351
352         OBD_SLAB_ALLOC_PTR_GFP(vob, ccc_object_kmem, CFS_ALLOC_IO);
353         if (vob != NULL) {
354                 struct cl_object_header *hdr;
355
356                 obj = ccc2lu(vob);
357                 hdr = &vob->cob_header;
358                 cl_object_header_init(hdr);
359                 lu_object_init(obj, &hdr->coh_lu, dev);
360                 lu_object_add_top(&hdr->coh_lu, obj);
361
362                 vob->cob_cl.co_ops = clops;
363                 obj->lo_ops = luops;
364         } else
365                 obj = NULL;
366         return obj;
367 }
368
369 int ccc_object_init0(const struct lu_env *env,
370                             struct ccc_object *vob,
371                             const struct cl_object_conf *conf)
372 {
373         vob->cob_inode = conf->coc_inode;
374         vob->cob_transient_pages = 0;
375         return 0;
376 }
377
378 int ccc_object_init(const struct lu_env *env, struct lu_object *obj,
379                            const struct lu_object_conf *conf)
380 {
381         struct ccc_device *dev = lu2ccc_dev(obj->lo_dev);
382         struct ccc_object *vob = lu2ccc(obj);
383         struct lu_object  *below;
384         struct lu_device  *under;
385         int result;
386
387         under = &dev->cdv_next->cd_lu_dev;
388         below = under->ld_ops->ldo_object_alloc(env, obj->lo_header, under);
389         if (below != NULL) {
390                 const struct cl_object_conf *cconf;
391
392                 cconf = lu2cl_conf(conf);
393                 CFS_INIT_LIST_HEAD(&vob->cob_pending_list);
394                 lu_object_add(obj, below);
395                 result = ccc_object_init0(env, vob, cconf);
396         } else
397                 result = -ENOMEM;
398         return result;
399 }
400
401 void ccc_object_free(const struct lu_env *env, struct lu_object *obj)
402 {
403         struct ccc_object *vob = lu2ccc(obj);
404
405         lu_object_fini(obj);
406         lu_object_header_fini(obj->lo_header);
407         OBD_SLAB_FREE_PTR(vob, ccc_object_kmem);
408 }
409
410 int ccc_lock_init(const struct lu_env *env,
411                   struct cl_object *obj, struct cl_lock *lock,
412                   const struct cl_io *unused,
413                   const struct cl_lock_operations *lkops)
414 {
415         struct ccc_lock *clk;
416         int result;
417
418         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
419
420         OBD_SLAB_ALLOC_PTR_GFP(clk, ccc_lock_kmem, CFS_ALLOC_IO);
421         if (clk != NULL) {
422                 cl_lock_slice_add(lock, &clk->clk_cl, obj, lkops);
423                 result = 0;
424         } else
425                 result = -ENOMEM;
426         return result;
427 }
428
429 int ccc_attr_set(const struct lu_env *env, struct cl_object *obj,
430                  const struct cl_attr *attr, unsigned valid)
431 {
432         return 0;
433 }
434
435 int ccc_object_glimpse(const struct lu_env *env,
436                        const struct cl_object *obj, struct ost_lvb *lvb)
437 {
438         struct inode *inode = ccc_object_inode(obj);
439
440         ENTRY;
441         lvb->lvb_mtime = cl_inode_mtime(inode);
442         lvb->lvb_atime = cl_inode_atime(inode);
443         lvb->lvb_ctime = cl_inode_ctime(inode);
444         /*
445          * LU-417: Add dirty pages block count lest i_blocks reports 0, some
446          * "cp" or "tar" on remote node may think it's a completely sparse file
447          * and skip it.
448          */
449         if (lvb->lvb_size > 0 && lvb->lvb_blocks == 0)
450                 lvb->lvb_blocks = dirty_cnt(inode);
451         RETURN(0);
452 }
453
454
455
456 int ccc_conf_set(const struct lu_env *env, struct cl_object *obj,
457                         const struct cl_object_conf *conf)
458 {
459         /* TODO: destroy all pages attached to this object. */
460         return 0;
461 }
462
463 /*****************************************************************************
464  *
465  * Page operations.
466  *
467  */
468
469 cfs_page_t *ccc_page_vmpage(const struct lu_env *env,
470                             const struct cl_page_slice *slice)
471 {
472         return cl2vm_page(slice);
473 }
474
475 int ccc_page_is_under_lock(const struct lu_env *env,
476                            const struct cl_page_slice *slice,
477                            struct cl_io *io)
478 {
479         struct ccc_io        *cio  = ccc_env_io(env);
480         struct cl_lock_descr *desc = &ccc_env_info(env)->cti_descr;
481         struct cl_page       *page = slice->cpl_page;
482
483         int result;
484
485         ENTRY;
486
487         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
488             io->ci_type == CIT_FAULT) {
489                 if (cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)
490                         result = -EBUSY;
491                 else {
492                         desc->cld_start = page->cp_index;
493                         desc->cld_end   = page->cp_index;
494                         desc->cld_obj   = page->cp_obj;
495                         desc->cld_mode  = CLM_READ;
496                         result = cl_queue_match(&io->ci_lockset.cls_done,
497                                                 desc) ? -EBUSY : 0;
498                 }
499         } else
500                 result = 0;
501         RETURN(result);
502 }
503
504 int ccc_fail(const struct lu_env *env, const struct cl_page_slice *slice)
505 {
506         /*
507          * Cached read?
508          */
509         LBUG();
510         return 0;
511 }
512
513 void ccc_transient_page_verify(const struct cl_page *page)
514 {
515 }
516
517 int ccc_transient_page_own(const struct lu_env *env,
518                                    const struct cl_page_slice *slice,
519                                    struct cl_io *unused,
520                                    int nonblock)
521 {
522         ccc_transient_page_verify(slice->cpl_page);
523         return 0;
524 }
525
526 void ccc_transient_page_assume(const struct lu_env *env,
527                                       const struct cl_page_slice *slice,
528                                       struct cl_io *unused)
529 {
530         ccc_transient_page_verify(slice->cpl_page);
531 }
532
533 void ccc_transient_page_unassume(const struct lu_env *env,
534                                         const struct cl_page_slice *slice,
535                                         struct cl_io *unused)
536 {
537         ccc_transient_page_verify(slice->cpl_page);
538 }
539
540 void ccc_transient_page_disown(const struct lu_env *env,
541                                       const struct cl_page_slice *slice,
542                                       struct cl_io *unused)
543 {
544         ccc_transient_page_verify(slice->cpl_page);
545 }
546
547 void ccc_transient_page_discard(const struct lu_env *env,
548                                        const struct cl_page_slice *slice,
549                                        struct cl_io *unused)
550 {
551         struct cl_page *page = slice->cpl_page;
552
553         ccc_transient_page_verify(slice->cpl_page);
554
555         /*
556          * For transient pages, remove it from the radix tree.
557          */
558         cl_page_delete(env, page);
559 }
560
561 int ccc_transient_page_prep(const struct lu_env *env,
562                                    const struct cl_page_slice *slice,
563                                    struct cl_io *unused)
564 {
565         ENTRY;
566         /* transient page should always be sent. */
567         RETURN(0);
568 }
569
570 /*****************************************************************************
571  *
572  * Lock operations.
573  *
574  */
575
576 void ccc_lock_delete(const struct lu_env *env,
577                      const struct cl_lock_slice *slice)
578 {
579         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
580 }
581
582 void ccc_lock_fini(const struct lu_env *env, struct cl_lock_slice *slice)
583 {
584         struct ccc_lock *clk = cl2ccc_lock(slice);
585         OBD_SLAB_FREE_PTR(clk, ccc_lock_kmem);
586 }
587
588 int ccc_lock_enqueue(const struct lu_env *env,
589                      const struct cl_lock_slice *slice,
590                      struct cl_io *unused, __u32 enqflags)
591 {
592         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
593         return 0;
594 }
595
596 int ccc_lock_unuse(const struct lu_env *env, const struct cl_lock_slice *slice)
597 {
598         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
599         return 0;
600 }
601
602 int ccc_lock_wait(const struct lu_env *env, const struct cl_lock_slice *slice)
603 {
604         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
605         return 0;
606 }
607
608 /**
609  * Implementation of cl_lock_operations::clo_fits_into() methods for ccc
610  * layer. This function is executed every time io finds an existing lock in
611  * the lock cache while creating new lock. This function has to decide whether
612  * cached lock "fits" into io.
613  *
614  * \param slice lock to be checked
615  * \param io    IO that wants a lock.
616  *
617  * \see lov_lock_fits_into().
618  */
619 int ccc_lock_fits_into(const struct lu_env *env,
620                        const struct cl_lock_slice *slice,
621                        const struct cl_lock_descr *need,
622                        const struct cl_io *io)
623 {
624         const struct cl_lock       *lock  = slice->cls_lock;
625         const struct cl_lock_descr *descr = &lock->cll_descr;
626         const struct ccc_io        *cio   = ccc_env_io(env);
627         int                         result;
628
629         ENTRY;
630         /*
631          * Work around DLM peculiarity: it assumes that glimpse
632          * (LDLM_FL_HAS_INTENT) lock is always LCK_PR, and returns reads lock
633          * when asked for LCK_PW lock with LDLM_FL_HAS_INTENT flag set. Make
634          * sure that glimpse doesn't get CLM_WRITE top-lock, so that it
635          * doesn't enqueue CLM_WRITE sub-locks.
636          */
637         if (cio->cui_glimpse)
638                 result = descr->cld_mode != CLM_WRITE;
639
640         /*
641          * Also, don't match incomplete write locks for read, otherwise read
642          * would enqueue missing sub-locks in the write mode.
643          */
644         else if (need->cld_mode != descr->cld_mode)
645                 result = lock->cll_state >= CLS_ENQUEUED;
646         else
647                 result = 1;
648         RETURN(result);
649 }
650
651 /**
652  * Implements cl_lock_operations::clo_state() method for ccc layer, invoked
653  * whenever lock state changes. Transfers object attributes, that might be
654  * updated as a result of lock acquiring into inode.
655  */
656 void ccc_lock_state(const struct lu_env *env,
657                     const struct cl_lock_slice *slice,
658                     enum cl_lock_state state)
659 {
660         struct cl_lock   *lock;
661         struct cl_object *obj;
662         struct inode     *inode;
663         struct cl_attr   *attr;
664
665         ENTRY;
666         lock = slice->cls_lock;
667
668         /*
669          * Refresh inode attributes when the lock is moving into CLS_HELD
670          * state, and only when this is a result of real enqueue, rather than
671          * of finding lock in the cache.
672          */
673         if (state == CLS_HELD && lock->cll_state < CLS_HELD) {
674                 int rc;
675
676                 obj   = slice->cls_obj;
677                 inode = ccc_object_inode(obj);
678                 attr  = ccc_env_thread_attr(env);
679
680                 /* vmtruncate()->ll_truncate() first sets the i_size and then
681                  * the kms under both a DLM lock and the
682                  * ll_inode_size_lock().  If we don't get the
683                  * ll_inode_size_lock() here we can match the DLM lock and
684                  * reset i_size from the kms before the truncating path has
685                  * updated the kms.  generic_file_write can then trust the
686                  * stale i_size when doing appending writes and effectively
687                  * cancel the result of the truncate.  Getting the
688                  * ll_inode_size_lock() after the enqueue maintains the DLM
689                  * -> ll_inode_size_lock() acquiring order. */
690                 cl_isize_lock(inode, 0);
691                 cl_object_attr_lock(obj);
692                 rc = cl_object_attr_get(env, obj, attr);
693                 if (rc == 0) {
694                         if (lock->cll_descr.cld_start == 0 &&
695                             lock->cll_descr.cld_end == CL_PAGE_EOF) {
696                                 cl_isize_write_nolock(inode, attr->cat_kms);
697                                 CDEBUG(D_INODE|D_VFSTRACE,
698                                        DFID" updating i_size "LPU64"\n",
699                                        PFID(lu_object_fid(&obj->co_lu)),
700                                        (__u64)cl_isize_read(inode));
701                         }
702                         cl_inode_mtime(inode) = attr->cat_mtime;
703                         cl_inode_atime(inode) = attr->cat_atime;
704                         cl_inode_ctime(inode) = attr->cat_ctime;
705                 } else {
706                         CL_LOCK_DEBUG(D_INFO, env, lock, "attr_get: %d\n", rc);
707                 }
708                 cl_object_attr_unlock(obj);
709                 cl_isize_unlock(inode, 0);
710         }
711         EXIT;
712 }
713
714 /*****************************************************************************
715  *
716  * io operations.
717  *
718  */
719
720 void ccc_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
721 {
722         struct cl_io *io = ios->cis_io;
723
724         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
725 }
726
727 int ccc_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
728                           __u32 enqflags, enum cl_lock_mode mode,
729                           pgoff_t start, pgoff_t end)
730 {
731         struct ccc_io          *cio   = ccc_env_io(env);
732         struct cl_lock_descr   *descr = &cio->cui_link.cill_descr;
733         struct cl_object       *obj   = io->ci_obj;
734
735         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
736         ENTRY;
737
738         CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
739
740         memset(&cio->cui_link, 0, sizeof cio->cui_link);
741
742         if (cio->cui_fd && (cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
743                 descr->cld_mode = CLM_GROUP;
744                 descr->cld_gid  = cio->cui_fd->fd_grouplock.cg_gid;
745         } else {
746                 descr->cld_mode  = mode;
747         }
748         descr->cld_obj   = obj;
749         descr->cld_start = start;
750         descr->cld_end   = end;
751         descr->cld_enq_flags = enqflags;
752
753         cl_io_lock_add(env, io, &cio->cui_link);
754         RETURN(0);
755 }
756
757 void ccc_io_update_iov(const struct lu_env *env,
758                        struct ccc_io *cio, struct cl_io *io)
759 {
760         int i;
761         size_t size = io->u.ci_rw.crw_count;
762
763         cio->cui_iov_olen = 0;
764         if (!cl_is_normalio(env, io))
765                 return;
766
767         for (i = 0; i < cio->cui_tot_nrsegs; i++) {
768                 struct iovec *iv = &cio->cui_iov[i];
769
770                 if (iv->iov_len < size)
771                         size -= iv->iov_len;
772                 else {
773                         if (iv->iov_len > size) {
774                                 cio->cui_iov_olen = iv->iov_len;
775                                 iv->iov_len = size;
776                         }
777                         break;
778                 }
779         }
780
781         cio->cui_nrsegs = i + 1;
782 }
783
784 int ccc_io_one_lock(const struct lu_env *env, struct cl_io *io,
785                     __u32 enqflags, enum cl_lock_mode mode,
786                     loff_t start, loff_t end)
787 {
788         struct cl_object *obj = io->ci_obj;
789         return ccc_io_one_lock_index(env, io, enqflags, mode,
790                                      cl_index(obj, start), cl_index(obj, end));
791 }
792
793 void ccc_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
794 {
795         CLOBINVRNT(env, ios->cis_io->ci_obj,
796                    ccc_object_invariant(ios->cis_io->ci_obj));
797 }
798
799 void ccc_io_advance(const struct lu_env *env,
800                     const struct cl_io_slice *ios,
801                     size_t nob)
802 {
803         struct ccc_io    *cio = cl2ccc_io(env, ios);
804         struct cl_io     *io  = ios->cis_io;
805         struct cl_object *obj = ios->cis_io->ci_obj;
806
807         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
808
809         if (cl_is_normalio(env, io) && io->ci_continue) {
810                 /* update the iov */
811                 LASSERT(cio->cui_tot_nrsegs >= cio->cui_nrsegs);
812                 LASSERT(cio->cui_tot_count  >= nob);
813
814                 cio->cui_iov        += cio->cui_nrsegs;
815                 cio->cui_tot_nrsegs -= cio->cui_nrsegs;
816                 cio->cui_tot_count  -= nob;
817
818                 if (cio->cui_iov_olen) {
819                         struct iovec *iv;
820
821                         cio->cui_iov--;
822                         cio->cui_tot_nrsegs++;
823                         iv = &cio->cui_iov[0];
824                         iv->iov_base += iv->iov_len;
825                         LASSERT(cio->cui_iov_olen > iv->iov_len);
826                         iv->iov_len = cio->cui_iov_olen - iv->iov_len;
827                 }
828         }
829 }
830
831 static void ccc_object_size_lock(struct cl_object *obj, int vfslock)
832 {
833         struct inode *inode = ccc_object_inode(obj);
834
835         if (vfslock)
836                 cl_isize_lock(inode, 0);
837         cl_object_attr_lock(obj);
838 }
839
840 static void ccc_object_size_unlock(struct cl_object *obj, int vfslock)
841 {
842         struct inode *inode = ccc_object_inode(obj);
843
844         cl_object_attr_unlock(obj);
845         if (vfslock)
846                 cl_isize_unlock(inode, 0);
847 }
848
849 /**
850  * Helper function that if necessary adjusts file size (inode->i_size), when
851  * position at the offset \a pos is accessed. File size can be arbitrary stale
852  * on a Lustre client, but client at least knows KMS. If accessed area is
853  * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
854  *
855  * Locking: cl_isize_lock is used to serialize changes to inode size and to
856  * protect consistency between inode size and cl_object
857  * attributes. cl_object_size_lock() protects consistency between cl_attr's of
858  * top-object and sub-objects.
859  *
860  * In page fault path cl_isize_lock cannot be taken, client has to live with
861  * the resulting races.
862  */
863 int ccc_prep_size(const struct lu_env *env, struct cl_object *obj,
864                   struct cl_io *io, loff_t start, size_t count, int vfslock,
865                   int *exceed)
866 {
867         struct cl_attr *attr  = ccc_env_thread_attr(env);
868         struct inode   *inode = ccc_object_inode(obj);
869         loff_t          pos   = start + count - 1;
870         loff_t kms;
871         int result;
872
873         /*
874          * Consistency guarantees: following possibilities exist for the
875          * relation between region being accessed and real file size at this
876          * moment:
877          *
878          *  (A): the region is completely inside of the file;
879          *
880          *  (B-x): x bytes of region are inside of the file, the rest is
881          *  outside;
882          *
883          *  (C): the region is completely outside of the file.
884          *
885          * This classification is stable under DLM lock already acquired by
886          * the caller, because to change the class, other client has to take
887          * DLM lock conflicting with our lock. Also, any updates to ->i_size
888          * by other threads on this client are serialized by
889          * ll_inode_size_lock(). This guarantees that short reads are handled
890          * correctly in the face of concurrent writes and truncates.
891          */
892         ccc_object_size_lock(obj, vfslock);
893         result = cl_object_attr_get(env, obj, attr);
894         if (result == 0) {
895                 kms = attr->cat_kms;
896                 if (pos > kms) {
897                         /*
898                          * A glimpse is necessary to determine whether we
899                          * return a short read (B) or some zeroes at the end
900                          * of the buffer (C)
901                          */
902                         ccc_object_size_unlock(obj, vfslock);
903                         result = cl_glimpse_lock(env, io, inode, obj);
904                         if (result == 0 && exceed != NULL) {
905                                 /* If objective page index exceed end-of-file
906                                  * page index, return directly. Do not expect
907                                  * kernel will check such case correctly.
908                                  * linux-2.6.18-128.1.1 miss to do that.
909                                  * --bug 17336 */
910                                 loff_t size = cl_isize_read(inode);
911                                 unsigned long cur_index = start >> CFS_PAGE_SHIFT;
912
913                                 if ((size == 0 && cur_index != 0) ||
914                                     (((size - 1) >> CFS_PAGE_SHIFT) < cur_index))
915                                 *exceed = 1;
916                         }
917                         return result;
918                 } else {
919                         /*
920                          * region is within kms and, hence, within real file
921                          * size (A). We need to increase i_size to cover the
922                          * read region so that generic_file_read() will do its
923                          * job, but that doesn't mean the kms size is
924                          * _correct_, it is only the _minimum_ size. If
925                          * someone does a stat they will get the correct size
926                          * which will always be >= the kms value here.
927                          * b=11081
928                          */
929                         /*
930                          * XXX in a page fault path, change inode size without
931                          * ll_inode_size_lock() held!  there is a race
932                          * condition with truncate path. (see ll_extent_lock)
933                          */
934                         /*
935                          * XXX i_size_write() is not used because it is not
936                          * safe to take the ll_inode_size_lock() due to a
937                          * potential lock inversion (bug 6077).  And since
938                          * it's not safe to use i_size_write() without a
939                          * covering mutex we do the assignment directly.  It
940                          * is not critical that the size be correct.
941                          */
942                         if (cl_isize_read(inode) < kms) {
943                                 if (vfslock)
944                                         cl_isize_write_nolock(inode, kms);
945                                 else
946                                         cl_isize_write(inode, kms);
947                                 CDEBUG(D_VFSTRACE,
948                                        DFID" updating i_size "LPU64"\n",
949                                        PFID(lu_object_fid(&obj->co_lu)),
950                                        (__u64)cl_isize_read(inode));
951
952                         }
953                 }
954         }
955         ccc_object_size_unlock(obj, vfslock);
956         return result;
957 }
958
959 /*****************************************************************************
960  *
961  * Transfer operations.
962  *
963  */
964
965 void ccc_req_completion(const struct lu_env *env,
966                         const struct cl_req_slice *slice, int ioret)
967 {
968         struct ccc_req *vrq;
969
970         if (ioret > 0)
971                 cl_stats_tally(slice->crs_dev, slice->crs_req->crq_type, ioret);
972
973         vrq = cl2ccc_req(slice);
974         OBD_SLAB_FREE_PTR(vrq, ccc_req_kmem);
975 }
976
977 /**
978  * Implementation of struct cl_req_operations::cro_attr_set() for ccc
979  * layer. ccc is responsible for
980  *
981  *    - o_[mac]time
982  *
983  *    - o_mode
984  *
985  *    - o_parent_seq
986  *
987  *    - o_[ug]id
988  *
989  *    - o_parent_oid
990  *
991  *    - o_parent_ver
992  *
993  *    - o_ioepoch,
994  *
995  *  and capability.
996  */
997 void ccc_req_attr_set(const struct lu_env *env,
998                       const struct cl_req_slice *slice,
999                       const struct cl_object *obj,
1000                       struct cl_req_attr *attr, obd_valid flags)
1001 {
1002         struct inode *inode;
1003         struct obdo  *oa;
1004         obd_flag      valid_flags;
1005
1006         oa = attr->cra_oa;
1007         inode = ccc_object_inode(obj);
1008         valid_flags = OBD_MD_FLTYPE|OBD_MD_FLATIME;
1009
1010         if (flags != (obd_valid)~0ULL)
1011                 valid_flags |= OBD_MD_FLMTIME|OBD_MD_FLCTIME|OBD_MD_FLATIME;
1012         else {
1013                 LASSERT(attr->cra_capa == NULL);
1014                 attr->cra_capa = cl_capa_lookup(inode,
1015                                                 slice->crs_req->crq_type);
1016         }
1017
1018         if (slice->crs_req->crq_type == CRT_WRITE) {
1019                 if (flags & OBD_MD_FLEPOCH) {
1020                         oa->o_valid |= OBD_MD_FLEPOCH;
1021                         oa->o_ioepoch = cl_i2info(inode)->lli_ioepoch;
1022                         valid_flags |= OBD_MD_FLMTIME|OBD_MD_FLCTIME|
1023                                 OBD_MD_FLUID|OBD_MD_FLGID;
1024                 }
1025         }
1026         obdo_from_inode(oa, inode, &cl_i2info(inode)->lli_fid,
1027                         valid_flags & flags);
1028 #ifdef __KERNEL__
1029         /* Bug11742 - set the OBD_FL_MMAP flag for memory mapped files */
1030         if (cfs_atomic_read(&(cl_inode2ccc(inode)->cob_mmap_cnt)) != 0) {
1031                 if (!(oa->o_valid & OBD_MD_FLFLAGS)) {
1032                         oa->o_valid |= OBD_MD_FLFLAGS;
1033                         oa->o_flags = OBD_FL_MMAP;
1034                 } else {
1035                         oa->o_flags |= OBD_FL_MMAP;
1036                 }
1037         }
1038 #endif
1039 }
1040
1041 const struct cl_req_operations ccc_req_ops = {
1042         .cro_attr_set   = ccc_req_attr_set,
1043         .cro_completion = ccc_req_completion
1044 };
1045
1046 int cl_setattr_ost(struct inode *inode, const struct iattr *attr,
1047                    struct obd_capa *capa)
1048 {
1049         struct lu_env *env;
1050         struct cl_io  *io;
1051         int            result;
1052         int            refcheck;
1053
1054         ENTRY;
1055
1056         env = cl_env_get(&refcheck);
1057         if (IS_ERR(env))
1058                 RETURN(PTR_ERR(env));
1059
1060         io = ccc_env_thread_io(env);
1061         io->ci_obj = cl_i2info(inode)->lli_clob;
1062
1063         io->u.ci_setattr.sa_attr.lvb_atime = LTIME_S(attr->ia_atime);
1064         io->u.ci_setattr.sa_attr.lvb_mtime = LTIME_S(attr->ia_mtime);
1065         io->u.ci_setattr.sa_attr.lvb_ctime = LTIME_S(attr->ia_ctime);
1066         io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
1067         io->u.ci_setattr.sa_valid = attr->ia_valid;
1068         io->u.ci_setattr.sa_capa = capa;
1069
1070         if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0)
1071                 result = cl_io_loop(env, io);
1072         else
1073                 result = io->ci_result;
1074         cl_io_fini(env, io);
1075         cl_env_put(env, &refcheck);
1076         RETURN(result);
1077 }
1078
1079 /*****************************************************************************
1080  *
1081  * Type conversions.
1082  *
1083  */
1084
1085 struct lu_device *ccc2lu_dev(struct ccc_device *vdv)
1086 {
1087         return &vdv->cdv_cl.cd_lu_dev;
1088 }
1089
1090 struct ccc_device *lu2ccc_dev(const struct lu_device *d)
1091 {
1092         return container_of0(d, struct ccc_device, cdv_cl.cd_lu_dev);
1093 }
1094
1095 struct ccc_device *cl2ccc_dev(const struct cl_device *d)
1096 {
1097         return container_of0(d, struct ccc_device, cdv_cl);
1098 }
1099
1100 struct lu_object *ccc2lu(struct ccc_object *vob)
1101 {
1102         return &vob->cob_cl.co_lu;
1103 }
1104
1105 struct ccc_object *lu2ccc(const struct lu_object *obj)
1106 {
1107         return container_of0(obj, struct ccc_object, cob_cl.co_lu);
1108 }
1109
1110 struct ccc_object *cl2ccc(const struct cl_object *obj)
1111 {
1112         return container_of0(obj, struct ccc_object, cob_cl);
1113 }
1114
1115 struct ccc_lock *cl2ccc_lock(const struct cl_lock_slice *slice)
1116 {
1117         return container_of(slice, struct ccc_lock, clk_cl);
1118 }
1119
1120 struct ccc_io *cl2ccc_io(const struct lu_env *env,
1121                          const struct cl_io_slice *slice)
1122 {
1123         struct ccc_io *cio;
1124
1125         cio = container_of(slice, struct ccc_io, cui_cl);
1126         LASSERT(cio == ccc_env_io(env));
1127         return cio;
1128 }
1129
1130 struct ccc_req *cl2ccc_req(const struct cl_req_slice *slice)
1131 {
1132         return container_of0(slice, struct ccc_req, crq_cl);
1133 }
1134
1135 cfs_page_t *cl2vm_page(const struct cl_page_slice *slice)
1136 {
1137         return cl2ccc_page(slice)->cpg_page;
1138 }
1139
1140 /*****************************************************************************
1141  *
1142  * Accessors.
1143  *
1144  */
1145 int ccc_object_invariant(const struct cl_object *obj)
1146 {
1147         struct inode         *inode = ccc_object_inode(obj);
1148         struct cl_inode_info *lli   = cl_i2info(inode);
1149
1150         return (S_ISREG(cl_inode_mode(inode)) ||
1151                 /* i_mode of unlinked inode is zeroed. */
1152                 cl_inode_mode(inode) == 0) && lli->lli_clob == obj;
1153 }
1154
1155 struct inode *ccc_object_inode(const struct cl_object *obj)
1156 {
1157         return cl2ccc(obj)->cob_inode;
1158 }
1159
1160 /**
1161  * Returns a pointer to cl_page associated with \a vmpage, without acquiring
1162  * additional reference to the resulting page. This is an unsafe version of
1163  * cl_vmpage_page() that can only be used under vmpage lock.
1164  */
1165 struct cl_page *ccc_vmpage_page_transient(cfs_page_t *vmpage)
1166 {
1167         KLASSERT(PageLocked(vmpage));
1168         return (struct cl_page *)vmpage->private;
1169 }
1170
1171 /**
1172  * Initializes or updates CLIO part when new meta-data arrives from the
1173  * server.
1174  *
1175  *     - allocates cl_object if necessary,
1176  *     - updated layout, if object was already here.
1177  */
1178 int cl_inode_init(struct inode *inode, struct lustre_md *md)
1179 {
1180         struct lu_env        *env;
1181         struct cl_inode_info *lli;
1182         struct cl_object     *clob;
1183         struct lu_site       *site;
1184         struct lu_fid        *fid;
1185         struct cl_object_conf conf = {
1186                 .coc_inode = inode,
1187                 .u = {
1188                         .coc_md    = md
1189                 }
1190         };
1191         int result = 0;
1192         int refcheck;
1193
1194         LASSERT(md->body->valid & OBD_MD_FLID);
1195
1196         if (!S_ISREG(cl_inode_mode(inode)))
1197                 return 0;
1198
1199         env = cl_env_get(&refcheck);
1200         if (IS_ERR(env))
1201                 return PTR_ERR(env);
1202
1203         site = cl_i2sbi(inode)->ll_site;
1204         lli  = cl_i2info(inode);
1205         fid  = &lli->lli_fid;
1206         LASSERT(fid_is_sane(fid));
1207
1208         if (lli->lli_clob == NULL) {
1209                 /* clob is slave of inode, empty lli_clob means for new inode,
1210                  * there is no clob in cache with the given fid, so it is
1211                  * unnecessary to perform lookup-alloc-lookup-insert, just
1212                  * alloc and insert directly. */
1213 #ifdef __KERNEL__
1214                 LASSERT(inode->i_state & I_NEW);
1215 #endif
1216                 conf.coc_lu.loc_flags = LOC_F_NEW;
1217                 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
1218                                       fid, &conf);
1219                 if (!IS_ERR(clob)) {
1220                         /*
1221                          * No locking is necessary, as new inode is
1222                          * locked by I_NEW bit.
1223                          *
1224                          * XXX not true for call from ll_update_inode().
1225                          */
1226                         lli->lli_clob = clob;
1227                         lu_object_ref_add(&clob->co_lu, "inode", inode);
1228                 } else
1229                         result = PTR_ERR(clob);
1230         } else
1231                 result = cl_conf_set(env, lli->lli_clob, &conf);
1232         cl_env_put(env, &refcheck);
1233
1234         if (result != 0)
1235                 CERROR("Failure to initialize cl object "DFID": %d\n",
1236                        PFID(fid), result);
1237         return result;
1238 }
1239
1240 /**
1241  * Wait for others drop their references of the object at first, then we drop
1242  * the last one, which will lead to the object be destroyed immediately.
1243  * Must be called after cl_object_kill() against this object.
1244  *
1245  * The reason we want to do this is: destroying top object will wait for sub
1246  * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
1247  * to initiate top object destroying which may deadlock. See bz22520.
1248  */
1249 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
1250 {
1251         struct lu_object_header *header = obj->co_lu.lo_header;
1252         cfs_waitlink_t           waiter;
1253
1254         if (unlikely(cfs_atomic_read(&header->loh_ref) != 1)) {
1255                 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
1256                 struct lu_site_bkt_data *bkt;
1257
1258                 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
1259
1260                 cfs_waitlink_init(&waiter);
1261                 cfs_waitq_add(&bkt->lsb_marche_funebre, &waiter);
1262
1263                 while (1) {
1264                         cfs_set_current_state(CFS_TASK_UNINT);
1265                         if (cfs_atomic_read(&header->loh_ref) == 1)
1266                                 break;
1267                         cfs_waitq_wait(&waiter, CFS_TASK_UNINT);
1268                 }
1269
1270                 cfs_set_current_state(CFS_TASK_RUNNING);
1271                 cfs_waitq_del(&bkt->lsb_marche_funebre, &waiter);
1272         }
1273
1274         cl_object_put(env, obj);
1275 }
1276
1277 void cl_inode_fini(struct inode *inode)
1278 {
1279         struct lu_env           *env;
1280         struct cl_inode_info    *lli  = cl_i2info(inode);
1281         struct cl_object        *clob = lli->lli_clob;
1282         int refcheck;
1283         int emergency;
1284
1285         if (clob != NULL) {
1286                 void                    *cookie;
1287
1288                 cookie = cl_env_reenter();
1289                 env = cl_env_get(&refcheck);
1290                 emergency = IS_ERR(env);
1291                 if (emergency) {
1292                         cfs_mutex_lock(&ccc_inode_fini_guard);
1293                         LASSERT(ccc_inode_fini_env != NULL);
1294                         cl_env_implant(ccc_inode_fini_env, &refcheck);
1295                         env = ccc_inode_fini_env;
1296                 }
1297                 /*
1298                  * cl_object cache is a slave to inode cache (which, in turn
1299                  * is a slave to dentry cache), don't keep cl_object in memory
1300                  * when its master is evicted.
1301                  */
1302                 cl_object_kill(env, clob);
1303                 lu_object_ref_del(&clob->co_lu, "inode", inode);
1304                 cl_object_put_last(env, clob);
1305                 lli->lli_clob = NULL;
1306                 if (emergency) {
1307                         cl_env_unplant(ccc_inode_fini_env, &refcheck);
1308                         cfs_mutex_unlock(&ccc_inode_fini_guard);
1309                 } else
1310                         cl_env_put(env, &refcheck);
1311                 cl_env_reexit(cookie);
1312         }
1313 }
1314
1315 /**
1316  * return IF_* type for given lu_dirent entry.
1317  * IF_* flag shld be converted to particular OS file type in
1318  * platform llite module.
1319  */
1320 __u16 ll_dirent_type_get(struct lu_dirent *ent)
1321 {
1322         __u16 type = 0;
1323         struct luda_type *lt;
1324         int len = 0;
1325
1326         if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
1327                 const unsigned align = sizeof(struct luda_type) - 1;
1328
1329                 len = le16_to_cpu(ent->lde_namelen);
1330                 len = (len + align) & ~align;
1331                 lt = (void *) ent->lde_name + len;
1332                 type = CFS_IFTODT(le16_to_cpu(lt->lt_type));
1333         }
1334         return type;
1335 }
1336
1337 /**
1338  * build inode number from passed @fid */
1339 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
1340 {
1341         if (BITS_PER_LONG == 32 || api32)
1342                 RETURN(fid_flatten32(fid));
1343         else
1344                 RETURN(fid_flatten(fid));
1345 }
1346
1347 /**
1348  * build inode generation from passed @fid.  If our FID overflows the 32-bit
1349  * inode number then return a non-zero generation to distinguish them. */
1350 __u32 cl_fid_build_gen(const struct lu_fid *fid)
1351 {
1352         __u32 gen;
1353         ENTRY;
1354
1355         if (fid_is_igif(fid)) {
1356                 gen = lu_igif_gen(fid);
1357                 RETURN(gen);
1358         }
1359
1360         gen = (fid_flatten(fid) >> 32);
1361         RETURN(gen);
1362 }