Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[fs/lustre-release.git] / lustre / llite / llite_lib.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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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/llite_lib.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <linux/module.h>
44 #include <linux/types.h>
45 #include <linux/random.h>
46 #include <linux/version.h>
47
48 #include <lustre_lite.h>
49 #include <lustre_ha.h>
50 #include <lustre_dlm.h>
51 #include <lprocfs_status.h>
52 #include <lustre_disk.h>
53 #include <lustre_param.h>
54 #include <lustre_cache.h>
55 #include "llite_internal.h"
56
57 cfs_mem_cache_t *ll_file_data_slab;
58
59 LIST_HEAD(ll_super_blocks);
60 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
61
62 extern struct address_space_operations ll_aops;
63 extern struct address_space_operations ll_dir_aops;
64
65 #ifndef log2
66 #define log2(n) ffz(~(n))
67 #endif
68
69 static inline void ll_pglist_fini(struct ll_sb_info *sbi)
70 {
71         struct page *page;
72         int i;
73
74         if (sbi->ll_pglist == NULL)
75                 return;
76
77         for_each_possible_cpu(i) {
78                 page = sbi->ll_pglist[i]->llpd_page;
79                 if (page) {
80                         sbi->ll_pglist[i] = NULL;
81                         __free_page(page);
82                 }
83         }
84
85         OBD_FREE(sbi->ll_pglist, sizeof(void *)*num_possible_cpus());
86         sbi->ll_pglist = NULL;
87 }
88
89 static inline int ll_pglist_init(struct ll_sb_info *sbi)
90 {
91         struct ll_pglist_data *pd;
92         unsigned long budget;
93         int i, color = 0;
94         ENTRY;
95
96         OBD_ALLOC(sbi->ll_pglist, sizeof(void *) * num_possible_cpus());
97         if (sbi->ll_pglist == NULL)
98                 RETURN(-ENOMEM);
99
100         budget = sbi->ll_async_page_max / num_online_cpus();
101         for_each_possible_cpu(i) {
102                 struct page *page = alloc_pages_node(cpu_to_node(i),
103                                                     GFP_KERNEL, 0);
104                 if (page == NULL) {
105                         ll_pglist_fini(sbi);
106                         RETURN(-ENOMEM);
107                 }
108
109                 if (color + L1_CACHE_ALIGN(sizeof(*pd)) > PAGE_SIZE)
110                         color = 0;
111
112                 pd = (struct ll_pglist_data *)(page_address(page) + color);
113                 memset(pd, 0, sizeof(*pd));
114                 spin_lock_init(&pd->llpd_lock);
115                 INIT_LIST_HEAD(&pd->llpd_list);
116                 if (cpu_online(i))
117                         pd->llpd_budget = budget;
118                 pd->llpd_cpu = i;
119                 pd->llpd_page = page;
120                 atomic_set(&pd->llpd_sample_count, 0);
121                 sbi->ll_pglist[i] = pd;
122                 color += L1_CACHE_ALIGN(sizeof(*pd));
123         }
124
125         RETURN(0);
126 }
127
128 static struct ll_sb_info *ll_init_sbi(void)
129 {
130         struct ll_sb_info *sbi = NULL;
131         unsigned long pages;
132         struct sysinfo si;
133         class_uuid_t uuid;
134         int i;
135         ENTRY;
136
137         OBD_ALLOC(sbi, sizeof(*sbi));
138         if (!sbi)
139                 RETURN(NULL);
140
141         OBD_ALLOC(sbi->ll_async_page_sample, sizeof(long)*num_possible_cpus());
142         if (sbi->ll_async_page_sample == NULL)
143                 GOTO(out, 0);
144
145         spin_lock_init(&sbi->ll_lock);
146         spin_lock_init(&sbi->ll_lco.lco_lock);
147         spin_lock_init(&sbi->ll_pp_extent_lock);
148         spin_lock_init(&sbi->ll_process_lock);
149         sbi->ll_rw_stats_on = 0;
150
151         si_meminfo(&si);
152         pages = si.totalram - si.totalhigh;
153         if (pages >> (20 - CFS_PAGE_SHIFT) < 512) {
154 #ifdef HAVE_BGL_SUPPORT
155                 sbi->ll_async_page_max = pages / 4;
156 #else
157                 sbi->ll_async_page_max = pages / 2;
158 #endif
159         } else {
160                 sbi->ll_async_page_max = (pages / 4) * 3;
161         }
162
163         lcounter_init(&sbi->ll_async_page_count);
164         spin_lock_init(&sbi->ll_async_page_reblnc_lock);
165         sbi->ll_async_page_sample_max = 64 * num_online_cpus();
166         sbi->ll_async_page_reblnc_count = 0;
167         sbi->ll_async_page_clock_hand = 0;
168         if (ll_pglist_init(sbi))
169                 GOTO(out, 0);
170
171         sbi->ll_ra_info.ra_max_pages = min(pages / 32,
172                                            SBI_DEFAULT_READAHEAD_MAX);
173         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
174                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
175         sbi->ll_contention_time = SBI_DEFAULT_CONTENTION_SECONDS;
176         sbi->ll_lockless_truncate_enable = SBI_DEFAULT_LOCKLESS_TRUNCATE_ENABLE;
177         INIT_LIST_HEAD(&sbi->ll_conn_chain);
178         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
179
180         ll_generate_random_uuid(uuid);
181         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
182         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
183
184         spin_lock(&ll_sb_lock);
185         list_add_tail(&sbi->ll_list, &ll_super_blocks);
186         spin_unlock(&ll_sb_lock);
187
188 #ifdef ENABLE_CHECKSUM
189         sbi->ll_flags |= LL_SBI_DATA_CHECKSUM;
190 #endif
191 #ifdef ENABLE_LLITE_CHECKSUM
192         sbi->ll_flags |= LL_SBI_LLITE_CHECKSUM;
193 #endif
194
195 #ifdef HAVE_LRU_RESIZE_SUPPORT
196         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
197 #endif
198
199 #ifdef HAVE_EXPORT___IGET
200         INIT_LIST_HEAD(&sbi->ll_deathrow);
201         spin_lock_init(&sbi->ll_deathrow_lock);
202 #endif
203         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
204                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_r_hist.oh_lock);
205                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_w_hist.oh_lock);
206         }
207
208         /* metadata statahead is enabled by default */
209         sbi->ll_sa_max = LL_SA_RPC_DEF;
210
211         RETURN(sbi);
212
213 out:
214         if (sbi->ll_async_page_sample)
215                 OBD_FREE(sbi->ll_async_page_sample,
216                          sizeof(long) * num_possible_cpus());
217         ll_pglist_fini(sbi);
218         OBD_FREE(sbi, sizeof(*sbi));
219         RETURN(NULL);
220 }
221
222 void ll_free_sbi(struct super_block *sb)
223 {
224         struct ll_sb_info *sbi = ll_s2sbi(sb);
225         ENTRY;
226
227         if (sbi != NULL) {
228                 ll_pglist_fini(sbi);
229                 spin_lock(&ll_sb_lock);
230                 list_del(&sbi->ll_list);
231                 spin_unlock(&ll_sb_lock);
232                 lcounter_destroy(&sbi->ll_async_page_count);
233                 OBD_FREE(sbi->ll_async_page_sample,
234                          sizeof(long) * num_possible_cpus());
235                 OBD_FREE(sbi, sizeof(*sbi));
236         }
237         EXIT;
238 }
239
240 static struct dentry_operations ll_d_root_ops = {
241 #ifdef DCACHE_LUSTRE_INVALID
242         .d_compare = ll_dcompare,
243 #endif
244 };
245
246 static int client_common_fill_super(struct super_block *sb,
247                                     char *mdc, char *osc)
248 {
249         struct inode *root = 0;
250         struct ll_sb_info *sbi = ll_s2sbi(sb);
251         struct obd_device *obd;
252         struct ll_fid rootfid;
253         struct obd_statfs osfs;
254         struct ptlrpc_request *request = NULL;
255         struct lustre_handle osc_conn = {0, };
256         struct lustre_handle mdc_conn = {0, };
257         struct lustre_md md;
258         struct obd_connect_data *data = NULL;
259         int err, checksum;
260         ENTRY;
261
262         obd = class_name2obd(mdc);
263         if (!obd) {
264                 CERROR("MDC %s: not setup or attached\n", mdc);
265                 RETURN(-EINVAL);
266         }
267
268         OBD_ALLOC(data, sizeof(*data));
269         if (data == NULL)
270                 RETURN(-ENOMEM);
271
272         if (proc_lustre_fs_root) {
273                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
274                                                   osc, mdc);
275                 if (err < 0)
276                         CERROR("could not register mount in /proc/fs/lustre\n");
277         }
278
279         /* indicate the features supported by this client */
280         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_IBITS      |
281                                   OBD_CONNECT_JOIN    | OBD_CONNECT_ATTRFID    |
282                                   OBD_CONNECT_NODEVOH | OBD_CONNECT_CANCELSET  |
283                                   OBD_CONNECT_AT      | OBD_CONNECT_FID |
284                                   OBD_CONNECT_VBR     | OBD_CONNECT_LOV_V3;
285 #ifdef HAVE_LRU_RESIZE_SUPPORT
286         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
287                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
288 #endif
289 #ifdef CONFIG_FS_POSIX_ACL
290         data->ocd_connect_flags |= OBD_CONNECT_ACL;
291 #endif
292         data->ocd_ibits_known = MDS_INODELOCK_FULL;
293         data->ocd_version = LUSTRE_VERSION_CODE;
294
295         if (sb->s_flags & MS_RDONLY)
296                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
297         if (sbi->ll_flags & LL_SBI_USER_XATTR)
298                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
299
300 #ifdef HAVE_MS_FLOCK_LOCK
301         /* force vfs to use lustre handler for flock() calls - bug 10743 */
302         sb->s_flags |= MS_FLOCK_LOCK;
303 #endif
304
305         if (sbi->ll_flags & LL_SBI_FLOCK)
306                 sbi->ll_fop = &ll_file_operations_flock;
307         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
308                 sbi->ll_fop = &ll_file_operations;
309         else
310                 sbi->ll_fop = &ll_file_operations_noflock;
311
312         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data, NULL);
313         if (err == -EBUSY) {
314                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (mdc %s) is performing "
315                                    "recovery, of which this client is not a "
316                                    "part. Please wait for recovery to complete,"
317                                    " abort, or time out.\n", mdc);
318                 GOTO(out, err);
319         } else if (err) {
320                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
321                 GOTO(out, err);
322         }
323         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
324         err = obd_fid_init(sbi->ll_mdc_exp);
325         if (err)
326                 GOTO(out_mdc, err);
327
328         err = obd_statfs(obd, &osfs, cfs_time_current_64() - HZ, 0);
329         if (err)
330                 GOTO(out_mdc, err);
331
332         /* MDC connect is surely finished by now because we actually sent
333          * a statfs RPC, otherwise obd_connect() is asynchronous. */
334         *data = class_exp2cliimp(sbi->ll_mdc_exp)->imp_connect_data;
335
336         LASSERT(osfs.os_bsize);
337         sb->s_blocksize = osfs.os_bsize;
338         sb->s_blocksize_bits = log2(osfs.os_bsize);
339         sb->s_magic = LL_SUPER_MAGIC;
340
341         /* for bug 11559. in $LINUX/fs/read_write.c, function do_sendfile():
342          *         retval = in_file->f_op->sendfile(...);
343          *         if (*ppos > max)
344          *                 retval = -EOVERFLOW;
345          *
346          * it will check if *ppos is greater than max. However, max equals to
347          * s_maxbytes, which is a negative integer in a x86_64 box since loff_t
348          * has been defined as a signed long long ineger in linux kernel. */
349 #if BITS_PER_LONG == 64
350         sb->s_maxbytes = PAGE_CACHE_MAXBYTES >> 1;
351 #else
352         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
353 #endif
354         sbi->ll_namelen = osfs.os_namelen;
355         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
356
357         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
358             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
359                 LCONSOLE_INFO("Disabling user_xattr feature because "
360                               "it is not supported on the server\n");
361                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
362         }
363
364         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
365 #ifdef MS_POSIXACL
366                 sb->s_flags |= MS_POSIXACL;
367 #endif
368                 sbi->ll_flags |= LL_SBI_ACL;
369         } else
370                 sbi->ll_flags &= ~LL_SBI_ACL;
371
372         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
373                 sbi->ll_flags |= LL_SBI_JOIN;
374
375         obd = class_name2obd(osc);
376         if (!obd) {
377                 CERROR("OSC %s: not setup or attached\n", osc);
378                 GOTO(out_mdc, err = -ENODEV);
379         }
380
381         data->ocd_connect_flags = OBD_CONNECT_VERSION   | OBD_CONNECT_GRANT    |
382                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
383                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_CANCELSET|
384                                   OBD_CONNECT_AT        | OBD_CONNECT_FID      |
385                                   OBD_CONNECT_VBR       | OBD_CONNECT_TRUNCLOCK;
386
387         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
388                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
389                  * disabled by default, because it can still be enabled on the
390                  * fly via /proc. As a consequence, we still need to come to an
391                  * agreement on the supported algorithms at connect time */
392                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
393
394                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
395                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
396                 else
397                         /* send the list of supported checksum types */
398                         data->ocd_cksum_types = OBD_CKSUM_ALL;
399         }
400
401 #ifdef HAVE_LRU_RESIZE_SUPPORT
402         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
403                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
404 #endif
405
406         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
407                "ocd_grant: %d\n", data->ocd_connect_flags,
408                data->ocd_version, data->ocd_grant);
409
410         obd->obd_upcall.onu_owner = &sbi->ll_lco;
411         obd->obd_upcall.onu_upcall = ll_ocd_update;
412         data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT;
413
414         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data, NULL);
415         if (err == -EBUSY) {
416                 LCONSOLE_ERROR_MSG(0x150, "An OST (osc %s) is performing "
417                                    "recovery, of which this client is not a "
418                                    "part.  Please wait for recovery to "
419                                    "complete, abort, or time out.\n", osc);
420                 GOTO(out, err);
421         } else if (err) {
422                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
423                 GOTO(out_mdc, err);
424         }
425         sbi->ll_osc_exp = class_conn2export(&osc_conn);
426         spin_lock(&sbi->ll_lco.lco_lock);
427         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
428         sbi->ll_lco.lco_mdc_exp = sbi->ll_mdc_exp;
429         sbi->ll_lco.lco_osc_exp = sbi->ll_osc_exp;
430         spin_unlock(&sbi->ll_lco.lco_lock);
431
432         err = obd_register_page_removal_cb(sbi->ll_osc_exp,
433                                            ll_page_removal_cb,
434                                            ll_pin_extent_cb);
435         if (err) {
436                 CERROR("cannot register page removal callback: rc = %d\n",err);
437                 GOTO(out_osc, err);
438         }
439         err = obd_register_lock_cancel_cb(sbi->ll_osc_exp,
440                                           ll_extent_lock_cancel_cb);
441         if (err) {
442                 CERROR("cannot register lock cancel callback: rc = %d\n", err);
443                 GOTO(out_page_rm_cb, err);
444         }
445
446         err = mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
447         if (err) {
448                 CERROR("cannot set max EA and cookie sizes: rc = %d\n", err);
449                 GOTO(out_lock_cn_cb, err);
450         }
451
452         err = obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
453                                   0, NULL, NULL, NULL, 0, NULL);
454         if (err < 0) {
455                 LCONSOLE_ERROR_MSG(0x151, "There are no OST's in this "
456                                    "filesystem. There must be at least one "
457                                    "active OST for a client to start.\n");
458                 GOTO(out_lock_cn_cb, err);
459         }
460
461         if (!ll_async_page_slab) {
462                 ll_async_page_slab_size =
463                         size_round(sizeof(struct ll_async_page)) + err;
464                 ll_async_page_slab = cfs_mem_cache_create("ll_async_page",
465                                                           ll_async_page_slab_size,
466                                                           0, 0);
467                 if (!ll_async_page_slab)
468                         GOTO(out_lock_cn_cb, -ENOMEM);
469         }
470
471         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
472         if (err) {
473                 CERROR("cannot mds_connect: rc = %d\n", err);
474                 GOTO(out_lock_cn_cb, err);
475         }
476         CDEBUG(D_SUPER, "rootfid "LPU64":"DFID"\n", rootfid.id,
477                         PFID((struct lu_fid*)&rootfid));
478         sbi->ll_rootino = rootfid.id;
479
480         sb->s_op = &lustre_super_operations;
481 #if THREAD_SIZE >= 8192
482         /* Disable the NFS export because of stack overflow
483          * when THREAD_SIZE < 8192. Please refer to 17630. */
484         sb->s_export_op = &lustre_export_operations;
485 #endif
486
487         /* make root inode
488          * XXX: move this to after cbd setup? */
489         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
490                           OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS |
491                           (sbi->ll_flags & LL_SBI_ACL ? OBD_MD_FLACL : 0),
492                           0, &request);
493         if (err) {
494                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
495                 GOTO(out_lock_cn_cb, err);
496         }
497
498         err = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
499         if (err) {
500                 CERROR("failed to understand root inode md: rc = %d\n",err);
501                 ptlrpc_req_finished (request);
502                 GOTO(out_lock_cn_cb, err);
503         }
504
505         LASSERT(sbi->ll_rootino != 0);
506         root = ll_iget(sb, ll_fid_build_ino(sbi, &rootfid), &md);
507
508         ptlrpc_req_finished(request);
509
510         if (root == NULL || is_bad_inode(root)) {
511                 mdc_free_lustre_md(sbi->ll_osc_exp, &md);
512                 CERROR("lustre_lite: bad iget4 for root\n");
513                 GOTO(out_root, err = -EBADF);
514         }
515
516         err = ll_close_thread_start(&sbi->ll_lcq);
517         if (err) {
518                 CERROR("cannot start close thread: rc %d\n", err);
519                 GOTO(out_root, err);
520         }
521
522         checksum = sbi->ll_flags & LL_SBI_DATA_CHECKSUM;
523         err = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM),
524                                  KEY_CHECKSUM, sizeof(checksum),
525                                  &checksum, NULL);
526
527         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
528            backing dev info assigned to inode mapping is used for
529            determining maximal readahead. */
530 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
531     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
532         /* bug 2805 - set VM readahead to zero */
533         vm_max_readahead = vm_min_readahead = 0;
534 #endif
535
536         sb->s_root = d_alloc_root(root);
537         if (data != NULL)
538                 OBD_FREE(data, sizeof(*data));
539         sb->s_root->d_op = &ll_d_root_ops;
540
541         sbi->ll_sdev_orig = sb->s_dev;
542         /* We set sb->s_dev equal on all lustre clients in order to support
543          * NFS export clustering.  NFSD requires that the FSID be the same
544          * on all clients. */
545         /* s_dev is also used in lt_compare() to compare two fs, but that is
546          * only a node-local comparison. */
547         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_target_uuid.uuid,
548                                  strlen(sbi2mdc(sbi)->cl_target_uuid.uuid));
549
550         RETURN(err);
551
552 out_root:
553         if (root)
554                 iput(root);
555 out_lock_cn_cb:
556         obd_unregister_lock_cancel_cb(sbi->ll_osc_exp,
557                                       ll_extent_lock_cancel_cb);
558 out_page_rm_cb:
559         obd_unregister_page_removal_cb(sbi->ll_osc_exp,
560                                        ll_page_removal_cb);
561 out_osc:
562         obd_disconnect(sbi->ll_osc_exp);
563         sbi->ll_osc_exp = NULL;
564 out_mdc:
565         obd_fid_fini(sbi->ll_mdc_exp);
566         obd_disconnect(sbi->ll_mdc_exp);
567         sbi->ll_mdc_exp = NULL;
568 out:
569         if (data != NULL)
570                 OBD_FREE(data, sizeof(*data));
571         lprocfs_unregister_mountpoint(sbi);
572         RETURN(err);
573 }
574
575 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
576 {
577         int size, rc;
578
579         *lmmsize = obd_size_diskmd(sbi->ll_osc_exp, NULL);
580         size = sizeof(int);
581         rc = obd_get_info(sbi->ll_mdc_exp, sizeof(KEY_MAX_EASIZE),
582                           KEY_MAX_EASIZE, &size, lmmsize, NULL);
583         if (rc)
584                 CERROR("Get max mdsize error rc %d \n", rc);
585
586         RETURN(rc);
587 }
588
589 void ll_dump_inode(struct inode *inode)
590 {
591         struct list_head *tmp;
592         int dentry_count = 0;
593
594         LASSERT(inode != NULL);
595
596         list_for_each(tmp, &inode->i_dentry)
597                 dentry_count++;
598
599         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
600                inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
601                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
602 }
603
604 void lustre_dump_dentry(struct dentry *dentry, int recur)
605 {
606         struct list_head *tmp;
607         int subdirs = 0;
608
609         LASSERT(dentry != NULL);
610
611         list_for_each(tmp, &dentry->d_subdirs)
612                 subdirs++;
613
614         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
615                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
616                dentry->d_name.len, dentry->d_name.name,
617                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
618                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
619                dentry->d_flags, dentry->d_fsdata, subdirs);
620         if (dentry->d_inode != NULL)
621                 ll_dump_inode(dentry->d_inode);
622
623         if (recur == 0)
624                 return;
625
626         list_for_each(tmp, &dentry->d_subdirs) {
627                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
628                 lustre_dump_dentry(d, recur - 1);
629         }
630 }
631
632 #ifdef HAVE_EXPORT___IGET
633 static void prune_dir_dentries(struct inode *inode)
634 {
635         struct dentry *dentry, *prev = NULL;
636
637         /* due to lustre specific logic, a directory
638          * can have few dentries - a bug from VFS POV */
639 restart:
640         spin_lock(&dcache_lock);
641         if (!list_empty(&inode->i_dentry)) {
642                 dentry = list_entry(inode->i_dentry.prev,
643                                     struct dentry, d_alias);
644                 /* in order to prevent infinite loops we
645                  * break if previous dentry is busy */
646                 if (dentry != prev) {
647                         prev = dentry;
648                         dget_locked(dentry);
649                         spin_unlock(&dcache_lock);
650
651                         /* try to kill all child dentries */
652                         shrink_dcache_parent(dentry);
653                         dput(dentry);
654
655                         /* now try to get rid of current dentry */
656                         d_prune_aliases(inode);
657                         goto restart;
658                 }
659         }
660         spin_unlock(&dcache_lock);
661 }
662
663 static void prune_deathrow_one(struct ll_inode_info *lli)
664 {
665         struct inode *inode = ll_info2i(lli);
666
667         /* first, try to drop any dentries - they hold a ref on the inode */
668         if (S_ISDIR(inode->i_mode))
669                 prune_dir_dentries(inode);
670         else
671                 d_prune_aliases(inode);
672
673
674         /* if somebody still uses it, leave it */
675         LASSERT(atomic_read(&inode->i_count) > 0);
676         if (atomic_read(&inode->i_count) > 1)
677                 goto out;
678
679         CDEBUG(D_INODE, "inode %lu/%u(%d) looks a good candidate for prune\n",
680                inode->i_ino,inode->i_generation, atomic_read(&inode->i_count));
681
682         /* seems nobody uses it anymore */
683         inode->i_nlink = 0;
684
685 out:
686         iput(inode);
687         return;
688 }
689
690 static void prune_deathrow(struct ll_sb_info *sbi, int try)
691 {
692         struct ll_inode_info *lli;
693         int empty;
694
695         do {
696                 if (need_resched() && try)
697                         break;
698
699                 if (try) {
700                         if (!spin_trylock(&sbi->ll_deathrow_lock))
701                                 break;
702                 } else {
703                         spin_lock(&sbi->ll_deathrow_lock);
704                 }
705
706                 empty = 1;
707                 lli = NULL;
708                 if (!list_empty(&sbi->ll_deathrow)) {
709                         lli = list_entry(sbi->ll_deathrow.next,
710                                          struct ll_inode_info,
711                                          lli_dead_list);
712                         list_del_init(&lli->lli_dead_list);
713                         if (!list_empty(&sbi->ll_deathrow))
714                                 empty = 0;
715                 }
716                 spin_unlock(&sbi->ll_deathrow_lock);
717
718                 if (lli)
719                         prune_deathrow_one(lli);
720
721         } while (empty == 0);
722 }
723 #else /* !HAVE_EXPORT___IGET */
724 #define prune_deathrow(sbi, try) do {} while (0)
725 #endif /* HAVE_EXPORT___IGET */
726
727 void client_common_put_super(struct super_block *sb)
728 {
729         struct ll_sb_info *sbi = ll_s2sbi(sb);
730         ENTRY;
731
732         ll_close_thread_shutdown(sbi->ll_lcq);
733
734         lprocfs_unregister_mountpoint(sbi);
735
736         /* destroy inodes in deathrow */
737         prune_deathrow(sbi, 0);
738
739         list_del(&sbi->ll_conn_chain);
740
741         /* callbacks is cleared after disconnect each target */
742         obd_disconnect(sbi->ll_osc_exp);
743         sbi->ll_osc_exp = NULL;
744
745         obd_fid_fini(sbi->ll_mdc_exp);
746         obd_disconnect(sbi->ll_mdc_exp);
747         sbi->ll_mdc_exp = NULL;
748
749         EXIT;
750 }
751
752 void ll_kill_super(struct super_block *sb)
753 {
754         struct ll_sb_info *sbi;
755
756         ENTRY;
757
758         /* not init sb ?*/
759         if (!(sb->s_flags & MS_ACTIVE))
760                 return;
761
762         sbi = ll_s2sbi(sb);
763         /* we need restore s_dev from changed for clustred NFS before put_super
764          * because new kernels have cached s_dev and change sb->s_dev in
765          * put_super not affected real removing devices */
766         if (sbi)
767                 sb->s_dev = sbi->ll_sdev_orig;
768         EXIT;
769 }
770
771 char *ll_read_opt(const char *opt, char *data)
772 {
773         char *value;
774         char *retval;
775         ENTRY;
776
777         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
778         if (strncmp(opt, data, strlen(opt)))
779                 RETURN(NULL);
780         if ((value = strchr(data, '=')) == NULL)
781                 RETURN(NULL);
782
783         value++;
784         OBD_ALLOC(retval, strlen(value) + 1);
785         if (!retval) {
786                 CERROR("out of memory!\n");
787                 RETURN(NULL);
788         }
789
790         memcpy(retval, value, strlen(value)+1);
791         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
792         RETURN(retval);
793 }
794
795 static inline int ll_set_opt(const char *opt, char *data, int fl)
796 {
797         if (strncmp(opt, data, strlen(opt)) != 0)
798                 return(0);
799         else
800                 return(fl);
801 }
802
803 /* non-client-specific mount options are parsed in lmd_parse */
804 static int ll_options(char *options, int *flags)
805 {
806         int tmp;
807         char *s1 = options, *s2;
808         ENTRY;
809
810         if (!options)
811                 RETURN(0);
812
813         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
814
815         while (*s1) {
816                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
817                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
818                 if (tmp) {
819                         *flags |= tmp;
820                         goto next;
821                 }
822                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
823                 if (tmp) {
824                         *flags |= tmp;
825                         goto next;
826                 }
827                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
828                 if (tmp) {
829                         *flags |= tmp;
830                         goto next;
831                 }
832                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
833                 if (tmp) {
834                         *flags &= ~tmp;
835                         goto next;
836                 }
837                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
838                 if (tmp) {
839                         *flags |= tmp;
840                         goto next;
841                 }
842                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
843                 if (tmp) {
844                         *flags &= ~tmp;
845                         goto next;
846                 }
847                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
848                 if (tmp) {
849                         /* Ignore deprecated mount option.  The client will
850                          * always try to mount with ACL support, whether this
851                          * is used depends on whether server supports it. */
852                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
853                                                   "mount option 'acl'.\n");
854                         goto next;
855                 }
856                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
857                 if (tmp) {
858                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
859                                                   "mount option 'noacl'.\n");
860                         goto next;
861                 }
862
863                 tmp = ll_set_opt("checksum", s1, LL_SBI_DATA_CHECKSUM);
864                 if (tmp) {
865                         *flags |= tmp;
866                         goto next;
867                 }
868                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_DATA_CHECKSUM);
869                 if (tmp) {
870                         *flags &= ~tmp;
871                         goto next;
872                 }
873
874                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
875                 if (tmp) {
876                         *flags |= tmp;
877                         goto next;
878                 }
879                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
880                 if (tmp) {
881                         *flags &= ~tmp;
882                         goto next;
883                 }
884                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
885                                    s1);
886                 RETURN(-EINVAL);
887
888 next:
889                 /* Find next opt */
890                 s2 = strchr(s1, ',');
891                 if (s2 == NULL)
892                         break;
893                 s1 = s2 + 1;
894         }
895         RETURN(0);
896 }
897
898 void ll_lli_init(struct ll_inode_info *lli)
899 {
900         lli->lli_inode_magic = LLI_INODE_MAGIC;
901         sema_init(&lli->lli_size_sem, 1);
902         sema_init(&lli->lli_write_sem, 1);
903         lli->lli_flags = 0;
904         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
905         spin_lock_init(&lli->lli_lock);
906         sema_init(&lli->lli_och_sem, 1);
907         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
908         lli->lli_mds_exec_och = NULL;
909         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
910         lli->lli_open_fd_exec_count = 0;
911         INIT_LIST_HEAD(&lli->lli_dead_list);
912 #ifdef HAVE_CLOSE_THREAD
913         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
914 #endif
915 }
916
917 /* COMPAT_146 */
918 #define MDCDEV "mdc_dev"
919 static int old_lustre_process_log(struct super_block *sb, char *newprofile,
920                                   struct config_llog_instance *cfg)
921 {
922         struct lustre_sb_info *lsi = s2lsi(sb);
923         struct obd_device *obd;
924         struct lustre_handle mdc_conn = {0, };
925         struct obd_export *exp;
926         char *ptr, *mdt, *profile;
927         char niduuid[10] = "mdtnid0";
928         class_uuid_t uuid;
929         struct obd_uuid mdc_uuid;
930         struct llog_ctxt *ctxt;
931         struct obd_connect_data ocd = { 0 };
932         lnet_nid_t nid;
933         int i, rc = 0, recov_bk = 1, failnodes = 0;
934         ENTRY;
935
936         ll_generate_random_uuid(uuid);
937         class_uuid_unparse(uuid, &mdc_uuid);
938         CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid);
939
940         /* Figure out the old mdt and profile name from new-style profile
941            ("lustre" from "mds/lustre-client") */
942         mdt = newprofile;
943         profile = strchr(mdt, '/');
944         if (profile == NULL) {
945                 CDEBUG(D_CONFIG, "Can't find MDT name in %s\n", newprofile);
946                 GOTO(out, rc = -EINVAL);
947         }
948         *profile = '\0';
949         profile++;
950         ptr = strrchr(profile, '-');
951         if (ptr == NULL) {
952                 CDEBUG(D_CONFIG, "Can't find client name in %s\n", newprofile);
953                 GOTO(out, rc = -EINVAL);
954         }
955         *ptr = '\0';
956
957         LCONSOLE_WARN("This looks like an old mount command; I will try to "
958                       "contact MDT '%s' for profile '%s'\n", mdt, profile);
959
960         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
961         i = 0;
962         ptr = lsi->lsi_lmd->lmd_dev;
963         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
964                 rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid, 0,0,0);
965                 i++;
966                 /* Stop at the first failover nid */
967                 if (*ptr == ':')
968                         break;
969         }
970         if (i == 0) {
971                 CERROR("No valid MDT nids found.\n");
972                 GOTO(out, rc = -EINVAL);
973         }
974         failnodes++;
975
976         rc = do_lcfg(MDCDEV, 0, LCFG_ATTACH, LUSTRE_MDC_NAME,mdc_uuid.uuid,0,0);
977         if (rc < 0)
978                 GOTO(out_del_uuid, rc);
979
980         rc = do_lcfg(MDCDEV, 0, LCFG_SETUP, mdt, niduuid, 0, 0);
981         if (rc < 0) {
982                 LCONSOLE_ERROR_MSG(0x153, "I couldn't establish a connection "
983                                    "with the MDT. Check that the MDT host NID "
984                                    "is correct and the networks are up.\n");
985                 GOTO(out_detach, rc);
986         }
987
988         obd = class_name2obd(MDCDEV);
989         if (obd == NULL)
990                 GOTO(out_cleanup, rc = -EINVAL);
991
992         /* Add any failover nids */
993         while (*ptr == ':') {
994                 /* New failover node */
995                 sprintf(niduuid, "mdtnid%d", failnodes);
996                 i = 0;
997                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
998                         i++;
999                         rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid,0,0,0);
1000                         if (rc)
1001                                 CERROR("Add uuid for %s failed %d\n",
1002                                        libcfs_nid2str(nid), rc);
1003                         if (*ptr == ':')
1004                                 break;
1005                 }
1006                 if (i > 0) {
1007                         rc = do_lcfg(MDCDEV, 0, LCFG_ADD_CONN, niduuid, 0, 0,0);
1008                         if (rc)
1009                                 CERROR("Add conn for %s failed %d\n",
1010                                        libcfs_nid2str(nid), rc);
1011                         failnodes++;
1012                 } else {
1013                         /* at ":/fsname" */
1014                         break;
1015                 }
1016         }
1017
1018         /* Try all connections, but only once. */
1019         rc = obd_set_info_async(obd->obd_self_export,
1020                                 sizeof(KEY_INIT_RECOV_BACKUP), KEY_INIT_RECOV_BACKUP,
1021                                 sizeof(recov_bk), &recov_bk, NULL);
1022         if (rc)
1023                 GOTO(out_cleanup, rc);
1024
1025         /* If we don't have this then an ACL MDS will refuse the connection */
1026         ocd.ocd_connect_flags = OBD_CONNECT_ACL;
1027
1028         rc = obd_connect(&mdc_conn, obd, &mdc_uuid, &ocd, NULL);
1029         if (rc) {
1030                 CERROR("cannot connect to %s: rc = %d\n", mdt, rc);
1031                 GOTO(out_cleanup, rc);
1032         }
1033
1034         exp = class_conn2export(&mdc_conn);
1035
1036         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1037
1038         cfg->cfg_flags |= CFG_F_COMPAT146;
1039
1040 #if 1
1041         rc = class_config_parse_llog(ctxt, profile, cfg);
1042 #else
1043         /*
1044          * For debugging, it's useful to just dump the log
1045          */
1046         rc = class_config_dump_llog(ctxt, profile, cfg);
1047 #endif
1048         llog_ctxt_put(ctxt);
1049         switch (rc) {
1050         case 0: {
1051                 /* Set the caller's profile name to the old-style */
1052                 memcpy(newprofile, profile, strlen(profile) + 1);
1053                 break;
1054         }
1055         case -EINVAL:
1056                 LCONSOLE_ERROR_MSG(0x154, "%s: The configuration '%s' could not"
1057                                    " be read from the MDT '%s'.  Make sure this"
1058                                    " client and the MDT are running compatible "
1059                                    "versions of Lustre.\n",
1060                                    obd->obd_name, profile, mdt);
1061                 /* fall through */
1062         default:
1063                 LCONSOLE_ERROR_MSG(0x155, "%s: The configuration '%s' could not"
1064                                    " be read from the MDT '%s'.  This may be "
1065                                    "the result of communication errors between "
1066                                    "the client and the MDT, or if the MDT is "
1067                                    "not running.\n", obd->obd_name, profile,
1068                                    mdt);
1069                 break;
1070         }
1071
1072         /* We don't so much care about errors in cleaning up the config llog
1073          * connection, as we have already read the config by this point. */
1074         obd_disconnect(exp);
1075
1076 out_cleanup:
1077         do_lcfg(MDCDEV, 0, LCFG_CLEANUP, 0, 0, 0, 0);
1078
1079 out_detach:
1080         do_lcfg(MDCDEV, 0, LCFG_DETACH, 0, 0, 0, 0);
1081
1082 out_del_uuid:
1083         /* class_add_uuid adds a nid even if the same uuid exists; we might
1084            delete any copy here.  So they all better match. */
1085         for (i = 0; i < failnodes; i++) {
1086                 sprintf(niduuid, "mdtnid%d", i);
1087                 do_lcfg(MDCDEV, 0, LCFG_DEL_UUID, niduuid, 0, 0, 0);
1088         }
1089         /* class_import_put will get rid of the additional connections */
1090 out:
1091         RETURN(rc);
1092 }
1093 /* end COMPAT_146 */
1094
1095 int ll_fill_super(struct super_block *sb)
1096 {
1097         struct lustre_profile *lprof = NULL;
1098         struct lustre_sb_info *lsi = s2lsi(sb);
1099         struct ll_sb_info *sbi;
1100         char  *osc = NULL, *mdc = NULL;
1101         char  *profilenm = get_profile_name(sb);
1102         struct config_llog_instance cfg = {0, };
1103         char   ll_instance[sizeof(sb) * 2 + 1];
1104         int    err;
1105         char  *save = NULL;
1106         char  pseudo[32] = { 0 };
1107         ENTRY;
1108
1109         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
1110
1111         cfs_module_get();
1112
1113         /* client additional sb info */
1114         lsi->lsi_llsbi = sbi = ll_init_sbi();
1115         if (!sbi) {
1116                 cfs_module_put();
1117                 RETURN(-ENOMEM);
1118         }
1119
1120         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
1121         if (err)
1122                 GOTO(out_free, err);
1123
1124         /* Generate a string unique to this super, in case some joker tries
1125            to mount the same fs at two mount points.
1126            Use the address of the super itself.*/
1127         sprintf(ll_instance, "%p", sb);
1128         cfg.cfg_instance = ll_instance;
1129         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1130         cfg.cfg_sb = sb;
1131
1132         /* set up client obds */
1133         if (strchr(profilenm, '/') != NULL) /* COMPAT_146 */
1134                 err = -EINVAL; /* skip error messages, use old config code */
1135         else
1136                 err = lustre_process_log(sb, profilenm, &cfg);
1137         /* COMPAT_146 */
1138         if (err < 0) {
1139                 char *oldname;
1140                 int rc, oldnamelen;
1141                 oldnamelen = strlen(profilenm) + 1;
1142                 /* Temp storage for 1.4.6 profile name */
1143                 OBD_ALLOC(oldname, oldnamelen);
1144                 if (oldname) {
1145                         memcpy(oldname, profilenm, oldnamelen);
1146                         rc = old_lustre_process_log(sb, oldname, &cfg);
1147                         if (rc >= 0) {
1148                                 /* That worked - update the profile name
1149                                    permanently */
1150                                 err = rc;
1151                                 OBD_FREE(lsi->lsi_lmd->lmd_profile,
1152                                          strlen(lsi->lsi_lmd->lmd_profile) + 1);
1153                                 OBD_ALLOC(lsi->lsi_lmd->lmd_profile,
1154                                          strlen(oldname) + 1);
1155                                 if (!lsi->lsi_lmd->lmd_profile) {
1156                                         OBD_FREE(oldname, oldnamelen);
1157                                         GOTO(out_free, err = -ENOMEM);
1158                                 }
1159                                 memcpy(lsi->lsi_lmd->lmd_profile, oldname,
1160                                        strlen(oldname) + 1);
1161                                 profilenm = get_profile_name(sb);
1162                                 /* Don't ever try to recover the MGS */
1163                                 rc = ptlrpc_set_import_active(
1164                                         lsi->lsi_mgc->u.cli.cl_import, 0);
1165                         }
1166                         OBD_FREE(oldname, oldnamelen);
1167                 }
1168         }
1169         /* end COMPAT_146 */
1170         if (err < 0) {
1171                 CERROR("Unable to process log: %d\n", err);
1172                 GOTO(out_free, err);
1173         }
1174
1175         lprof = class_get_profile(profilenm);
1176         if (lprof == NULL) {
1177                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1178                                    " read from the MGS.  Does that filesystem "
1179                                    "exist?\n", profilenm);
1180                 GOTO(out_free, err = -EINVAL);
1181         }
1182
1183         /*
1184          * The configuration for 1.8 client and 2.0 client are different.
1185          * 2.0 introduces lmv, but 1.8 directly uses mdc.
1186          * Here, we will hack to use proper name for mdc if needed.
1187          */
1188         {
1189                 char *fsname_end;
1190                 int namelen;
1191
1192                 save = lprof->lp_mdc;
1193                 fsname_end = strrchr(save, '-');
1194                 if (fsname_end) {
1195                         namelen = fsname_end - save;
1196                         if (strcmp(fsname_end, "-clilmv") == 0) {
1197                                 strncpy(pseudo, save, namelen);
1198                                 strcat(pseudo, "-MDT0000-mdc");
1199                                 lprof->lp_mdc = pseudo;
1200                                 CDEBUG(D_INFO, "1.8.x connecting to 2.0: lmv=%s"
1201                                        " new mdc=%s\n", save, pseudo);
1202                         }
1203                 }
1204         }
1205
1206         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1207                lprof->lp_mdc, lprof->lp_osc);
1208
1209         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
1210                   strlen(ll_instance) + 2);
1211         if (!osc)
1212                 GOTO(out_free, err = -ENOMEM);
1213         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
1214
1215         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
1216                   strlen(ll_instance) + 2);
1217         if (!mdc)
1218                 GOTO(out_free, err = -ENOMEM);
1219         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
1220
1221         /* connections, registrations, sb setup */
1222         err = client_common_fill_super(sb, mdc, osc);
1223
1224 out_free:
1225         if (save && lprof)
1226                 lprof->lp_mdc = save;
1227         if (mdc)
1228                 OBD_FREE(mdc, strlen(mdc) + 1);
1229         if (osc)
1230                 OBD_FREE(osc, strlen(osc) + 1);
1231         if (err)
1232                 ll_put_super(sb);
1233         else
1234                 LCONSOLE_WARN("Client %s has started\n", profilenm);
1235
1236         RETURN(err);
1237 } /* ll_fill_super */
1238
1239
1240 void ll_put_super(struct super_block *sb)
1241 {
1242         struct config_llog_instance cfg;
1243         char   ll_instance[sizeof(sb) * 2 + 1];
1244         struct obd_device *obd;
1245         struct lustre_sb_info *lsi = s2lsi(sb);
1246         struct ll_sb_info *sbi = ll_s2sbi(sb);
1247         char *profilenm = get_profile_name(sb);
1248         int force = 1, next;
1249         ENTRY;
1250
1251         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1252
1253         sprintf(ll_instance, "%p", sb);
1254         cfg.cfg_instance = ll_instance;
1255         lustre_end_log(sb, NULL, &cfg);
1256
1257         if (sbi->ll_mdc_exp) {
1258                 obd = class_exp2obd(sbi->ll_mdc_exp);
1259                 if (obd)
1260                         force = obd->obd_force;
1261         }
1262
1263         /* We need to set force before the lov_disconnect in
1264            lustre_common_put_super, since l_d cleans up osc's as well. */
1265         if (force) {
1266                 next = 0;
1267                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1268                                                      &next)) != NULL) {
1269                         obd->obd_force = force;
1270                 }
1271         }
1272
1273         if (sbi->ll_lcq) {
1274                 /* Only if client_common_fill_super succeeded */
1275                 client_common_put_super(sb);
1276         }
1277
1278         next = 0;
1279         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1280                 class_manual_cleanup(obd);
1281         }
1282
1283         if (profilenm)
1284                 class_del_profile(profilenm);
1285
1286         ll_free_sbi(sb);
1287         lsi->lsi_llsbi = NULL;
1288
1289         lustre_common_put_super(sb);
1290
1291         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1292
1293         cfs_module_put();
1294
1295         EXIT;
1296 } /* client_put_super */
1297
1298 #if defined(HAVE_REGISTER_CACHE) || defined(HAVE_SHRINKER_CACHE)
1299
1300 #if defined(HAVE_CACHE_RETURN_INT)
1301 static int
1302 #else
1303 static void
1304 #endif
1305 ll_shrink_cache(int priority, unsigned int gfp_mask)
1306 {
1307         struct ll_sb_info *sbi;
1308         int count = 0;
1309
1310         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1311                 count += llap_shrink_cache(sbi, priority);
1312
1313 #if defined(HAVE_CACHE_RETURN_INT)
1314         return count;
1315 #endif
1316 }
1317
1318 struct cache_definition ll_cache_definition = {
1319         .name = "llap_cache",
1320         .shrink = ll_shrink_cache
1321 };
1322 #endif /* HAVE_REGISTER_CACHE || HAVE_SHRINKER_CACHE */
1323
1324 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1325 {
1326         struct inode *inode = NULL;
1327         /* NOTE: we depend on atomic igrab() -bzzz */
1328         lock_res_and_lock(lock);
1329         if (lock->l_ast_data) {
1330                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1331                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1332                         inode = igrab(lock->l_ast_data);
1333                 } else {
1334                         inode = lock->l_ast_data;
1335                         ldlm_lock_debug(NULL, inode->i_state & I_FREEING ?
1336                                                 D_INFO : D_WARNING,
1337                                         lock, __FILE__, __func__, __LINE__,
1338                                         "l_ast_data %p is bogus: magic %08x",
1339                                         lock->l_ast_data, lli->lli_inode_magic);
1340                         inode = NULL;
1341                 }
1342         }
1343         unlock_res_and_lock(lock);
1344         return inode;
1345 }
1346
1347 static int null_if_equal(struct ldlm_lock *lock, void *data)
1348 {
1349         if (data == lock->l_ast_data) {
1350                 lock->l_ast_data = NULL;
1351
1352                 if (lock->l_req_mode != lock->l_granted_mode)
1353                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1354         }
1355
1356         return LDLM_ITER_CONTINUE;
1357 }
1358
1359 void ll_clear_inode(struct inode *inode)
1360 {
1361         struct ll_fid fid;
1362         struct ll_inode_info *lli = ll_i2info(inode);
1363         struct ll_sb_info *sbi = ll_i2sbi(inode);
1364         ENTRY;
1365
1366         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1367                inode->i_generation, inode);
1368
1369         if (S_ISDIR(inode->i_mode)) {
1370                 /* these should have been cleared in ll_file_release */
1371                 LASSERT(lli->lli_sai == NULL);
1372                 LASSERT(lli->lli_opendir_key == NULL);
1373                 LASSERT(lli->lli_opendir_pid == 0);
1374         }
1375
1376         ll_inode2fid(&fid, inode);
1377         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1378         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
1379
1380         LASSERT(!lli->lli_open_fd_write_count);
1381         LASSERT(!lli->lli_open_fd_read_count);
1382         LASSERT(!lli->lli_open_fd_exec_count);
1383
1384         if (lli->lli_mds_write_och)
1385                 ll_mdc_real_close(inode, FMODE_WRITE);
1386         if (lli->lli_mds_exec_och) {
1387                 if (!FMODE_EXEC)
1388                         CERROR("No FMODE exec, bug exec och is present for "
1389                                "inode %ld\n", inode->i_ino);
1390                 ll_mdc_real_close(inode, FMODE_EXEC);
1391         }
1392         if (lli->lli_mds_read_och)
1393                 ll_mdc_real_close(inode, FMODE_READ);
1394
1395
1396         if (lli->lli_smd) {
1397                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
1398                                   null_if_equal, inode);
1399
1400                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
1401                 lli->lli_smd = NULL;
1402         }
1403
1404         if (lli->lli_symlink_name) {
1405                 OBD_FREE(lli->lli_symlink_name,
1406                          strlen(lli->lli_symlink_name) + 1);
1407                 lli->lli_symlink_name = NULL;
1408         }
1409
1410 #ifdef CONFIG_FS_POSIX_ACL
1411         if (lli->lli_posix_acl) {
1412                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1413                 posix_acl_release(lli->lli_posix_acl);
1414                 lli->lli_posix_acl = NULL;
1415         }
1416 #endif
1417
1418         lli->lli_inode_magic = LLI_INODE_DEAD;
1419
1420 #ifdef HAVE_EXPORT___IGET
1421         spin_lock(&sbi->ll_deathrow_lock);
1422         list_del_init(&lli->lli_dead_list);
1423         spin_unlock(&sbi->ll_deathrow_lock);
1424 #endif
1425
1426         EXIT;
1427 }
1428 static int ll_setattr_do_truncate(struct inode *inode, loff_t new_size)
1429 {
1430         struct ll_sb_info *sbi = ll_i2sbi(inode);
1431         struct ll_inode_info *lli = ll_i2info(inode);
1432         struct lov_stripe_md *lsm = lli->lli_smd;
1433         int rc;
1434         ldlm_policy_data_t policy = { .l_extent = {new_size,
1435                                                    OBD_OBJECT_EOF } };
1436         struct lustre_handle lockh = { 0 };
1437         int local_lock = 0; /* 0 - no local lock;
1438                              * 1 - lock taken by lock_extent;
1439                              * 2 - by obd_match*/
1440         int ast_flags;
1441         int err;
1442         ENTRY;
1443
1444         UNLOCK_INODE_MUTEX(inode);
1445         UP_WRITE_I_ALLOC_SEM(inode);
1446
1447         if (sbi->ll_lockless_truncate_enable &&
1448             (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK)) {
1449                 ast_flags = LDLM_FL_BLOCK_GRANTED;
1450                 rc = obd_match(sbi->ll_osc_exp, lsm, LDLM_EXTENT,
1451                                &policy, LCK_PW, &ast_flags, inode, &lockh);
1452                 if (rc > 0) {
1453                         local_lock = 2;
1454                         rc = 0;
1455                 } else if (rc == 0) {
1456                         rc = ll_file_punch(inode, new_size, 1);
1457                 }
1458         } else {
1459                 /* XXX when we fix the AST intents to pass the discard-range
1460                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1461                  * XXX here. */
1462                 ast_flags = (new_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
1463                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy,
1464                                     &lockh, ast_flags);
1465                 if (likely(rc == 0))
1466                         local_lock = 1;
1467         }
1468
1469         LOCK_INODE_MUTEX(inode);
1470         DOWN_WRITE_I_ALLOC_SEM(inode);
1471         if (likely(rc == 0)) {
1472                 /* Only ll_inode_size_lock is taken at this level.
1473                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1474                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1475                  * ll_truncate dropped ll_inode_size_lock() */
1476                 ll_inode_size_lock(inode, 0);
1477                 if (!local_lock)
1478                         set_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1479                 rc = vmtruncate(inode, new_size);
1480                 clear_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1481                 if (rc != 0) {
1482                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1483                         ll_inode_size_unlock(inode, 0);
1484                 }
1485         }
1486         if (local_lock) {
1487                 if (local_lock == 2)
1488                         err = obd_cancel(sbi->ll_osc_exp, lsm, LCK_PW, &lockh);
1489                 else
1490                         err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1491                 if (unlikely(err != 0)){
1492                         CERROR("extent unlock failed: err=%d,"
1493                                " unlock method =%d\n", err, local_lock);
1494                         if (rc == 0)
1495                                 rc = err;
1496                 }
1497         }
1498         RETURN(rc);
1499 }
1500
1501 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1502  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1503  * keep these values until such a time that objects are allocated for it.
1504  * We do the MDS operations first, as it is checking permissions for us.
1505  * We don't to the MDS RPC if there is nothing that we want to store there,
1506  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1507  * going to do an RPC anyways.
1508  *
1509  * If we are doing a truncate, we will send the mtime and ctime updates
1510  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1511  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1512  * at the same time.
1513  */
1514 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1515 {
1516         struct ll_inode_info *lli = ll_i2info(inode);
1517         struct lov_stripe_md *lsm = lli->lli_smd;
1518         struct ll_sb_info *sbi = ll_i2sbi(inode);
1519         struct ptlrpc_request *request = NULL;
1520         struct mdc_op_data op_data = { { 0 } };
1521         struct lustre_md md;
1522         int ia_valid = attr->ia_valid;
1523         int rc = 0;
1524         ENTRY;
1525
1526         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1527                attr->ia_valid);
1528         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETATTR, 1);
1529
1530         if (ia_valid & ATTR_SIZE) {
1531                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1532                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1533                                attr->ia_size, ll_file_maxbytes(inode));
1534                         RETURN(-EFBIG);
1535                 }
1536
1537                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1538         }
1539
1540         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1541         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1542                 if (current->fsuid != inode->i_uid &&
1543                     !cfs_capable(CFS_CAP_FOWNER))
1544                         RETURN(-EPERM);
1545         }
1546
1547         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1548         if (attr->ia_valid & ATTR_CTIME) {
1549                 attr->ia_ctime = CURRENT_TIME;
1550                 attr->ia_valid |= ATTR_CTIME_SET;
1551         }
1552         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1553                 attr->ia_atime = CURRENT_TIME;
1554                 attr->ia_valid |= ATTR_ATIME_SET;
1555         }
1556         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1557                 attr->ia_mtime = CURRENT_TIME;
1558                 attr->ia_valid |= ATTR_MTIME_SET;
1559         }
1560
1561         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1562                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1563                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1564                        CURRENT_SECONDS);
1565
1566         /* NB: ATTR_SIZE will only be set after this point if the size
1567          * resides on the MDS, ie, this file has no objects. */
1568         if (lsm)
1569                 attr->ia_valid &= ~ATTR_SIZE;
1570
1571         /* We always do an MDS RPC, even if we're only changing the size;
1572          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1573         ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0, NULL);
1574
1575         rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1576                          attr, NULL, 0, NULL, 0, &request);
1577
1578         if (rc) {
1579                 ptlrpc_req_finished(request);
1580                 if (rc == -ENOENT) {
1581                         inode->i_nlink = 0;
1582                         /* Unlinked special device node?  Or just a race?
1583                          * Pretend we done everything. */
1584                         if (!S_ISREG(inode->i_mode) &&
1585                             !S_ISDIR(inode->i_mode))
1586                                 rc = inode_setattr(inode, attr);
1587                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY)
1588                         CERROR("mdc_setattr fails: rc = %d\n", rc);
1589                 RETURN(rc);
1590         }
1591
1592         rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
1593         if (rc) {
1594                 ptlrpc_req_finished(request);
1595                 RETURN(rc);
1596         }
1597
1598         /* We call inode_setattr to adjust timestamps.
1599          * If there is at least some data in file, we cleared ATTR_SIZE above to
1600          * avoid invoking vmtruncate, otherwise it is important to call
1601          * vmtruncate in inode_setattr to update inode->i_size (bug 6196) */
1602         rc = inode_setattr(inode, attr);
1603
1604         ll_update_inode(inode, &md);
1605         ptlrpc_req_finished(request);
1606
1607         if (!lsm || !S_ISREG(inode->i_mode)) {
1608                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1609                 RETURN(rc);
1610         }
1611
1612         /* We really need to get our PW lock before we change inode->i_size.
1613          * If we don't we can race with other i_size updaters on our node, like
1614          * ll_file_read.  We can also race with i_size propogation to other
1615          * nodes through dirtying and writeback of final cached pages.  This
1616          * last one is especially bad for racing o_append users on other
1617          * nodes. */
1618         if (ia_valid & ATTR_SIZE) {
1619                 rc = ll_setattr_do_truncate(inode, attr->ia_size);
1620         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1621                 struct obd_info oinfo = { { { 0 } } };
1622                 struct obdo *oa;
1623                 struct lustre_handle lockh = { 0 };
1624                 obd_valid valid;
1625
1626                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1627                        inode->i_ino, LTIME_S(attr->ia_mtime));
1628
1629                 OBDO_ALLOC(oa);
1630                 if (oa) {
1631                         oa->o_id = lsm->lsm_object_id;
1632                         oa->o_gr = lsm->lsm_object_gr;
1633                         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1634
1635                         valid = OBD_MD_FLTYPE | OBD_MD_FLFID | OBD_MD_FLGENER;
1636
1637                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1638                                 struct ost_lvb xtimes;
1639
1640                                 /* setting mtime to past is performed under PW
1641                                  * EOF extent lock */
1642                                 oinfo.oi_policy.l_extent.start = 0;
1643                                 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
1644                                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW,
1645                                                     &oinfo.oi_policy,
1646                                                     &lockh, 0);
1647                                 if (rc)
1648                                         RETURN(rc);
1649
1650                                 /* setattr under locks
1651                                  *
1652                                  * 1. restore inode's timestamps which
1653                                  * are about to be set as long as
1654                                  * concurrent stat (via
1655                                  * ll_glimpse_size) might bring
1656                                  * out-of-date ones
1657                                  *
1658                                  * 2. update lsm so that next stat
1659                                  * (via ll_glimpse_size) could get
1660                                  * correct values in lsm */
1661                                 lov_stripe_lock(lli->lli_smd);
1662                                 if (ia_valid & ATTR_ATIME) {
1663                                         LTIME_S(inode->i_atime) =
1664                                                 xtimes.lvb_atime =
1665                                                 LTIME_S(attr->ia_atime);
1666                                         valid |= OBD_MD_FLATIME;
1667                                 }
1668                                 if (ia_valid & ATTR_MTIME) {
1669                                         LTIME_S(inode->i_mtime) =
1670                                                 xtimes.lvb_mtime =
1671                                                 LTIME_S(attr->ia_mtime);
1672                                         valid |= OBD_MD_FLMTIME;
1673                                 }
1674                                 if (ia_valid & ATTR_CTIME) {
1675                                         LTIME_S(inode->i_ctime) =
1676                                                 xtimes.lvb_ctime =
1677                                                 LTIME_S(attr->ia_ctime);
1678                                         valid |= OBD_MD_FLCTIME;
1679                                 }
1680
1681                                 obd_update_lvb(ll_i2obdexp(inode), lli->lli_smd,
1682                                                &xtimes, valid);
1683                                 lov_stripe_unlock(lli->lli_smd);
1684                         } else {
1685                                 /* lockless setattr
1686                                  *
1687                                  * 1. do not use inode's timestamps
1688                                  * because concurrent stat might fill
1689                                  * the inode with out-of-date times,
1690                                  * send values from attr instead
1691                                  *
1692                                  * 2.do no update lsm, as long as stat
1693                                  * (via ll_glimpse_size) will bring
1694                                  * attributes from osts anyway */
1695                                 if (ia_valid & ATTR_ATIME) {
1696                                         oa->o_atime = LTIME_S(attr->ia_atime);
1697                                         oa->o_valid |= OBD_MD_FLATIME;
1698                                 }
1699                                 if (ia_valid & ATTR_MTIME) {
1700                                         oa->o_mtime = LTIME_S(attr->ia_mtime);
1701                                         oa->o_valid |= OBD_MD_FLMTIME;
1702                                 }
1703                                 if (ia_valid & ATTR_CTIME) {
1704                                         oa->o_ctime = LTIME_S(attr->ia_ctime);
1705                                         oa->o_valid |= OBD_MD_FLCTIME;
1706                                 }
1707                         }
1708
1709                         obdo_from_inode(oa, inode, valid);
1710
1711                         oinfo.oi_oa = oa;
1712                         oinfo.oi_md = lsm;
1713
1714                         rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
1715                         if (rc)
1716                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1717
1718                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1719                                 int err;
1720
1721                                 err = ll_extent_unlock(NULL, inode, lsm,
1722                                                        LCK_PW, &lockh);
1723                                 if (unlikely(err != 0)) {
1724                                         CERROR("extent unlock failed: "
1725                                                "err=%d\n", err);
1726                                         if (rc == 0)
1727                                                 rc = err;
1728                                 }
1729                         }
1730                         OBDO_FREE(oa);
1731                 } else {
1732                         rc = -ENOMEM;
1733                 }
1734         }
1735         RETURN(rc);
1736 }
1737
1738 int ll_setattr(struct dentry *de, struct iattr *attr)
1739 {
1740         int mode;
1741
1742         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1743             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1744                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1745         if ((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1746             (ATTR_SIZE|ATTR_MODE)) {
1747                 mode = de->d_inode->i_mode;
1748                 if (((mode & S_ISUID) && (!(attr->ia_mode & S_ISUID))) ||
1749                     ((mode & S_ISGID) && (mode & S_IXGRP) &&
1750                     (!(attr->ia_mode & S_ISGID))))
1751                         attr->ia_valid |= ATTR_FORCE;
1752         }
1753
1754         return ll_setattr_raw(de->d_inode, attr);
1755 }
1756
1757 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1758                        __u64 max_age, __u32 flags)
1759 {
1760         struct ll_sb_info *sbi = ll_s2sbi(sb);
1761         struct obd_statfs obd_osfs;
1762         int rc;
1763         ENTRY;
1764
1765         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age, flags);
1766         if (rc) {
1767                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1768                 RETURN(rc);
1769         }
1770
1771         osfs->os_type = sb->s_magic;
1772
1773         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1774                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1775
1776         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1777                               &obd_osfs, max_age, flags);
1778         if (rc) {
1779                 CERROR("obd_statfs fails: rc = %d\n", rc);
1780                 RETURN(rc);
1781         }
1782
1783         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1784                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1785                obd_osfs.os_files);
1786
1787         osfs->os_bsize = obd_osfs.os_bsize;
1788         osfs->os_blocks = obd_osfs.os_blocks;
1789         osfs->os_bfree = obd_osfs.os_bfree;
1790         osfs->os_bavail = obd_osfs.os_bavail;
1791
1792         /* If we don't have as many objects free on the OST as inodes
1793          * on the MDS, we reduce the total number of inodes to
1794          * compensate, so that the "inodes in use" number is correct.
1795          */
1796         if (obd_osfs.os_ffree < osfs->os_ffree) {
1797                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1798                         obd_osfs.os_ffree;
1799                 osfs->os_ffree = obd_osfs.os_ffree;
1800         }
1801
1802         RETURN(rc);
1803 }
1804 #ifndef HAVE_STATFS_DENTRY_PARAM
1805 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1806 {
1807 #else
1808 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1809 {
1810         struct super_block *sb = de->d_sb;
1811 #endif
1812         struct obd_statfs osfs;
1813         int rc;
1814
1815         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1816         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1817
1818         /* For now we will always get up-to-date statfs values, but in the
1819          * future we may allow some amount of caching on the client (e.g.
1820          * from QOS or lprocfs updates). */
1821         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - 1, 0);
1822         if (rc)
1823                 return rc;
1824
1825         statfs_unpack(sfs, &osfs);
1826
1827         /* We need to downshift for all 32-bit kernels, because we can't
1828          * tell if the kernel is being called via sys_statfs64() or not.
1829          * Stop before overflowing f_bsize - in which case it is better
1830          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1831         if (sizeof(long) < 8) {
1832                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1833                         sfs->f_bsize <<= 1;
1834
1835                         osfs.os_blocks >>= 1;
1836                         osfs.os_bfree >>= 1;
1837                         osfs.os_bavail >>= 1;
1838                 }
1839         }
1840
1841         sfs->f_blocks = osfs.os_blocks;
1842         sfs->f_bfree = osfs.os_bfree;
1843         sfs->f_bavail = osfs.os_bavail;
1844
1845         return 0;
1846 }
1847
1848 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1849 {
1850         struct ll_inode_info *lli;
1851         struct lov_stripe_md *lsm;
1852
1853         lli = ll_i2info(inode);
1854         LASSERT(lli->lli_size_sem_owner != current);
1855         down(&lli->lli_size_sem);
1856         LASSERT(lli->lli_size_sem_owner == NULL);
1857         lli->lli_size_sem_owner = current;
1858         lsm = lli->lli_smd;
1859         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1860                  lsm, lock_lsm);
1861         if (lock_lsm)
1862                 lov_stripe_lock(lsm);
1863 }
1864
1865 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1866 {
1867         struct ll_inode_info *lli;
1868         struct lov_stripe_md *lsm;
1869
1870         lli = ll_i2info(inode);
1871         lsm = lli->lli_smd;
1872         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1873                  lsm, unlock_lsm);
1874         if (unlock_lsm)
1875                 lov_stripe_unlock(lsm);
1876         LASSERT(lli->lli_size_sem_owner == current);
1877         lli->lli_size_sem_owner = NULL;
1878         up(&lli->lli_size_sem);
1879 }
1880
1881 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1882 {
1883         struct ll_inode_info *lli = ll_i2info(inode);
1884
1885         dump_lsm(D_INODE, lsm);
1886         dump_lsm(D_INODE, lli->lli_smd);
1887         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1888                  "lsm must be joined lsm %p\n", lsm);
1889         obd_free_memmd(ll_i2obdexp(inode), &lli->lli_smd);
1890         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1891                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1892         lli->lli_smd = lsm;
1893         lli->lli_maxbytes = lsm->lsm_maxbytes;
1894         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1895                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1896 }
1897
1898 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1899 {
1900         struct ll_inode_info *lli = ll_i2info(inode);
1901         struct mds_body *body = md->body;
1902         struct lov_stripe_md *lsm = md->lsm;
1903         struct ll_sb_info *sbi = ll_i2sbi(inode);
1904         ENTRY;
1905
1906         CDEBUG(D_INODE, "body->valid = "LPX64"\n", body->valid);
1907
1908         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1909         if (lsm != NULL) {
1910                 if (lli->lli_smd == NULL) {
1911                         if (lsm->lsm_magic != LOV_MAGIC_V1 &&
1912                             lsm->lsm_magic != LOV_MAGIC_V3 &&
1913                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1914                                 dump_lsm(D_ERROR, lsm);
1915                                 LBUG();
1916                         }
1917                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1918                                lsm, inode->i_ino, inode->i_generation, inode);
1919                         /* ll_inode_size_lock() requires it is only called
1920                          * with lli_smd != NULL or lock_lsm == 0 or we can
1921                          * race between lock/unlock.  bug 9547 */
1922                         lli->lli_smd = lsm;
1923                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1924                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1925                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1926                 } else {
1927                         if (lli->lli_smd->lsm_magic == lsm->lsm_magic &&
1928                              lli->lli_smd->lsm_stripe_count ==
1929                                         lsm->lsm_stripe_count) {
1930                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1931                                         CERROR("lsm mismatch for inode %ld\n",
1932                                                 inode->i_ino);
1933                                         CERROR("lli_smd:\n");
1934                                         dump_lsm(D_ERROR, lli->lli_smd);
1935                                         CERROR("lsm:\n");
1936                                         dump_lsm(D_ERROR, lsm);
1937                                         LBUG();
1938                                 }
1939                         } else
1940                                 ll_replace_lsm(inode, lsm);
1941                 }
1942                 if (lli->lli_smd != lsm)
1943                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1944         }
1945
1946 #ifdef CONFIG_FS_POSIX_ACL
1947         LASSERT(!md->posix_acl || (body->valid & OBD_MD_FLACL));
1948         if (body->valid & OBD_MD_FLACL) {
1949                 spin_lock(&lli->lli_lock);
1950                 if (lli->lli_posix_acl)
1951                         posix_acl_release(lli->lli_posix_acl);
1952                 lli->lli_posix_acl = md->posix_acl;
1953                 spin_unlock(&lli->lli_lock);
1954         }
1955 #endif
1956
1957         inode->i_ino = ll_fid_build_ino(sbi, &body->fid1);
1958         if (body->valid & OBD_MD_FLGENER)
1959                 inode->i_generation = body->generation;
1960
1961         if (body->valid & OBD_MD_FLATIME) {
1962                 if (body->atime > LTIME_S(inode->i_atime))
1963                         LTIME_S(inode->i_atime) = body->atime;
1964                 lli->lli_lvb.lvb_atime = body->atime;
1965         }
1966         if (body->valid & OBD_MD_FLMTIME) {
1967                 if (body->mtime > LTIME_S(inode->i_mtime)) {
1968                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
1969                                "to "LPU64"\n", inode->i_ino,
1970                                LTIME_S(inode->i_mtime), body->mtime);
1971                         LTIME_S(inode->i_mtime) = body->mtime;
1972                 }
1973                 lli->lli_lvb.lvb_mtime = body->mtime;
1974         }
1975         if (body->valid & OBD_MD_FLCTIME) {
1976                 if (body->ctime > LTIME_S(inode->i_ctime))
1977                         LTIME_S(inode->i_ctime) = body->ctime;
1978                 lli->lli_lvb.lvb_ctime = body->ctime;
1979         }
1980         if (body->valid & OBD_MD_FLMODE)
1981                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1982         if (body->valid & OBD_MD_FLTYPE)
1983                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1984         if (S_ISREG(inode->i_mode)) {
1985                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
1986         } else {
1987                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1988         }
1989 #ifdef HAVE_INODE_BLKSIZE
1990         inode->i_blksize = 1<<inode->i_blkbits;
1991 #endif
1992         if (body->valid & OBD_MD_FLUID)
1993                 inode->i_uid = body->uid;
1994         if (body->valid & OBD_MD_FLGID)
1995                 inode->i_gid = body->gid;
1996         if (body->valid & OBD_MD_FLFLAGS)
1997                 inode->i_flags = ll_ext_to_inode_flags(body->flags |
1998                                                        MDS_BFLAG_EXT_FLAGS);
1999         if (body->valid & OBD_MD_FLNLINK)
2000                 inode->i_nlink = body->nlink;
2001
2002         if (body->valid & OBD_MD_FLRDEV)
2003                 inode->i_rdev = old_decode_dev(body->rdev);
2004         if (body->valid & OBD_MD_FLSIZE) {
2005 #if 0           /* Can't block ll_test_inode->ll_update_inode, b=14326*/
2006                 ll_inode_size_lock(inode, 0);
2007                 i_size_write(inode, body->size);
2008                 ll_inode_size_unlock(inode, 0);
2009 #else
2010                 inode->i_size = body->size;
2011 #endif
2012         }
2013         if (body->valid & OBD_MD_FLBLOCKS)
2014                 inode->i_blocks = body->blocks;
2015
2016         if (body->valid & OBD_MD_FLSIZE)
2017                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
2018         EXIT;
2019 }
2020
2021 static struct backing_dev_info ll_backing_dev_info = {
2022         .ra_pages       = 0,    /* No readahead */
2023 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
2024         .capabilities   = 0,    /* Does contribute to dirty memory */
2025 #else
2026         .memory_backed  = 0,    /* Does contribute to dirty memory */
2027 #endif
2028 };
2029
2030 void ll_read_inode2(struct inode *inode, void *opaque)
2031 {
2032         struct lustre_md *md = opaque;
2033         struct ll_inode_info *lli = ll_i2info(inode);
2034         ENTRY;
2035
2036         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2037                inode->i_generation, inode);
2038
2039         ll_lli_init(lli);
2040
2041         LASSERT(!lli->lli_smd);
2042
2043         /* Core attributes from the MDS first.  This is a new inode, and
2044          * the VFS doesn't zero times in the core inode so we have to do
2045          * it ourselves.  They will be overwritten by either MDS or OST
2046          * attributes - we just need to make sure they aren't newer. */
2047         LTIME_S(inode->i_mtime) = 0;
2048         LTIME_S(inode->i_atime) = 0;
2049         LTIME_S(inode->i_ctime) = 0;
2050         inode->i_rdev = 0;
2051         ll_update_inode(inode, md);
2052
2053         /* OIDEBUG(inode); */
2054
2055         if (S_ISREG(inode->i_mode)) {
2056                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2057                 inode->i_op = &ll_file_inode_operations;
2058                 inode->i_fop = sbi->ll_fop;
2059                 inode->i_mapping->a_ops = &ll_aops;
2060                 EXIT;
2061         } else if (S_ISDIR(inode->i_mode)) {
2062                 inode->i_op = &ll_dir_inode_operations;
2063                 inode->i_fop = &ll_dir_operations;
2064                 inode->i_mapping->a_ops = &ll_dir_aops;
2065                 EXIT;
2066         } else if (S_ISLNK(inode->i_mode)) {
2067                 inode->i_op = &ll_fast_symlink_inode_operations;
2068                 EXIT;
2069         } else {
2070                 inode->i_op = &ll_special_inode_operations;
2071                 init_special_inode(inode, inode->i_mode,
2072                                    kdev_t_to_nr(inode->i_rdev));
2073                 /* initializing backing dev info. */
2074                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
2075                 EXIT;
2076         }
2077 }
2078
2079 int ll_iocontrol(struct inode *inode, struct file *file,
2080                  unsigned int cmd, unsigned long arg)
2081 {
2082         struct ll_sb_info *sbi = ll_i2sbi(inode);
2083         struct ptlrpc_request *req = NULL;
2084         int rc, flags = 0;
2085         ENTRY;
2086
2087         switch(cmd) {
2088         case EXT3_IOC_GETFLAGS: {
2089                 struct ll_fid fid;
2090                 struct mds_body *body;
2091
2092                 ll_inode2fid(&fid, inode);
2093                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
2094                 if (rc) {
2095                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2096                         RETURN(-abs(rc));
2097                 }
2098
2099                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2100                                       sizeof(*body));
2101
2102                 /* We want to return EXT3_*_FL flags to the caller via this
2103                  * ioctl.  An older MDS may be sending S_* flags, fix it up. */
2104                 flags = ll_inode_to_ext_flags(body->flags,
2105                                               MDS_BFLAG_EXT_FLAGS);
2106                 ptlrpc_req_finished (req);
2107
2108                 RETURN(put_user(flags, (int *)arg));
2109         }
2110         case EXT3_IOC_SETFLAGS: {
2111                 struct mdc_op_data op_data = { { 0 } };
2112                 struct ll_iattr_struct attr;
2113                 struct obd_info oinfo = { { { 0 } } };
2114                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
2115
2116                 if (get_user(flags, (int *)arg))
2117                         RETURN(-EFAULT);
2118
2119                 oinfo.oi_md = lsm;
2120                 OBDO_ALLOC(oinfo.oi_oa);
2121                 if (!oinfo.oi_oa)
2122                         RETURN(-ENOMEM);
2123
2124                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0, NULL);
2125
2126                 memset(&attr, 0, sizeof(attr));
2127                 attr.ia_attr_flags = flags;
2128                 ((struct iattr *)&attr)->ia_valid |= ATTR_ATTR_FLAG;
2129
2130                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
2131                                  (struct iattr *)&attr, NULL, 0, NULL, 0, &req);
2132                 ptlrpc_req_finished(req);
2133                 if (rc || lsm == NULL) {
2134                         OBDO_FREE(oinfo.oi_oa);
2135                         RETURN(rc);
2136                 }
2137
2138                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
2139                 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
2140                 oinfo.oi_oa->o_flags = flags;
2141                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP |
2142                                        OBD_MD_FLFLAGS;
2143
2144                 obdo_from_inode(oinfo.oi_oa, inode,
2145                                 OBD_MD_FLFID | OBD_MD_FLGENER);
2146                 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
2147                 OBDO_FREE(oinfo.oi_oa);
2148                 if (rc) {
2149                         if (rc != -EPERM && rc != -EACCES)
2150                                 CERROR("mdc_setattr_async fails: rc = %d\n", rc);
2151                         RETURN(rc);
2152                 }
2153
2154                 inode->i_flags = ll_ext_to_inode_flags(flags |
2155                                                        MDS_BFLAG_EXT_FLAGS);
2156                 RETURN(0);
2157         }
2158         default:
2159                 RETURN(-ENOSYS);
2160         }
2161
2162         RETURN(0);
2163 }
2164
2165 /* umount -f client means force down, don't save state */
2166 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2167 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
2168 {
2169         struct super_block *sb = vfsmnt->mnt_sb;
2170 #else
2171 void ll_umount_begin(struct super_block *sb)
2172 {
2173 #endif
2174         struct lustre_sb_info *lsi = s2lsi(sb);
2175         struct ll_sb_info *sbi = ll_s2sbi(sb);
2176         struct obd_device *obd;
2177         struct obd_ioctl_data ioc_data = { 0 };
2178         ENTRY;
2179
2180 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2181         if (!(flags & MNT_FORCE)) {
2182                 EXIT;
2183                 return;
2184         }
2185 #endif
2186
2187         /* Tell the MGC we got umount -f */
2188         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
2189
2190         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2191                sb->s_count, atomic_read(&sb->s_active));
2192
2193         obd = class_exp2obd(sbi->ll_mdc_exp);
2194         if (obd == NULL) {
2195                 CERROR("Invalid MDC connection handle "LPX64"\n",
2196                        sbi->ll_mdc_exp->exp_handle.h_cookie);
2197                 EXIT;
2198                 return;
2199         }
2200         obd->obd_force = 1;
2201         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
2202                       &ioc_data, NULL);
2203
2204         obd = class_exp2obd(sbi->ll_osc_exp);
2205         if (obd == NULL) {
2206                 CERROR("Invalid LOV connection handle "LPX64"\n",
2207                        sbi->ll_osc_exp->exp_handle.h_cookie);
2208                 EXIT;
2209                 return;
2210         }
2211
2212         obd->obd_force = 1;
2213         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
2214                       &ioc_data, NULL);
2215
2216         /* Really, we'd like to wait until there are no requests outstanding,
2217          * and then continue.  For now, we just invalidate the requests,
2218          * schedule() and sleep one second if needed, and hope.
2219          */
2220         schedule();
2221 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2222         if (atomic_read(&vfsmnt->mnt_count) > 2) {
2223                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
2224                                      cfs_time_seconds(1));
2225                 if (atomic_read(&vfsmnt->mnt_count) > 2)
2226                         LCONSOLE_WARN("Mount still busy with %d refs! You "
2227                                       "may try to umount it a bit later\n",
2228                                       atomic_read(&vfsmnt->mnt_count));
2229         }
2230 #endif
2231
2232         EXIT;
2233 }
2234
2235 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2236 {
2237         struct ll_sb_info *sbi = ll_s2sbi(sb);
2238         int err;
2239         __u32 read_only;
2240
2241         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2242                 read_only = *flags & MS_RDONLY;
2243                 err = obd_set_info_async(sbi->ll_mdc_exp, sizeof(KEY_READONLY),
2244                                          KEY_READONLY, sizeof(read_only),
2245                                          &read_only, NULL);
2246
2247                 /* MDS might have expected a different ro key value, b=17493 */
2248                 if (err == -EINVAL) {
2249                         CDEBUG(D_CONFIG, "Retrying remount with 1.6.6 ro key\n");
2250                         err = obd_set_info_async(sbi->ll_mdc_exp,
2251                                                  sizeof(KEY_READONLY_166COMPAT),
2252                                                  KEY_READONLY_166COMPAT,
2253                                                  sizeof(read_only),
2254                                                  &read_only, NULL);
2255                 }
2256
2257                 if (err) {
2258                         CERROR("Failed to change the read-only flag during "
2259                                "remount: %d\n", err);
2260                         return err;
2261                 }
2262
2263                 if (read_only)
2264                         sb->s_flags |= MS_RDONLY;
2265                 else
2266                         sb->s_flags &= ~MS_RDONLY;
2267         }
2268         return 0;
2269 }
2270
2271 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
2272                   struct ptlrpc_request *req, int offset,struct super_block *sb)
2273 {
2274         struct lustre_md md;
2275         struct ll_sb_info *sbi = NULL;
2276         int rc = 0;
2277         ENTRY;
2278
2279         LASSERT(*inode || sb);
2280         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2281         prune_deathrow(sbi, 1);
2282
2283         rc = mdc_req2lustre_md(req, offset, exp, &md);
2284         if (rc)
2285                 RETURN(rc);
2286
2287         if (*inode) {
2288                 ll_update_inode(*inode, &md);
2289         } else {
2290                 LASSERT(sb);
2291                 /** hashing VFS inode by FIDs.
2292                  * IGIF will be used for for compatibility if needed.
2293                  */
2294                 *inode =ll_iget(sb, ll_fid_build_ino(sbi, &md.body->fid1), &md);
2295                 if (*inode == NULL || is_bad_inode(*inode)) {
2296                         mdc_free_lustre_md(exp, &md);
2297                         rc = -ENOMEM;
2298                         CERROR("new_inode -fatal: rc %d\n", rc);
2299                         GOTO(out, rc);
2300                 }
2301         }
2302
2303         rc = obd_checkmd(exp, ll_i2mdcexp(*inode),
2304                          ll_i2info(*inode)->lli_smd);
2305 out:
2306         RETURN(rc);
2307 }
2308
2309 char *llap_origins[] = {
2310         [LLAP_ORIGIN_UNKNOWN] = "--",
2311         [LLAP_ORIGIN_READPAGE] = "rp",
2312         [LLAP_ORIGIN_READAHEAD] = "ra",
2313         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
2314         [LLAP_ORIGIN_WRITEPAGE] = "wp",
2315         [LLAP_ORIGIN_LOCKLESS_IO] = "ls"
2316 };
2317
2318 struct ll_async_page *llite_pglist_next_llap(struct list_head *head,
2319                                              struct list_head *list)
2320 {
2321         struct ll_async_page *llap;
2322         struct list_head *pos;
2323
2324         list_for_each(pos, list) {
2325                 if (pos == head)
2326                         return NULL;
2327                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
2328                 if (llap->llap_page == NULL)
2329                         continue;
2330                 return llap;
2331         }
2332         LBUG();
2333         return NULL;
2334 }
2335
2336 int ll_obd_statfs(struct inode *inode, void *arg)
2337 {
2338         struct ll_sb_info *sbi = NULL;
2339         struct obd_device *client_obd = NULL, *lov_obd = NULL;
2340         struct lov_obd *lov = NULL;
2341         struct obd_statfs stat_buf = {0};
2342         char *buf = NULL;
2343         struct obd_ioctl_data *data = NULL;
2344         __u32 type, index;
2345         int len = 0, rc;
2346
2347         if (!inode || !(sbi = ll_i2sbi(inode)))
2348                 GOTO(out_statfs, rc = -EINVAL);
2349
2350         rc = obd_ioctl_getdata(&buf, &len, arg);
2351         if (rc)
2352                 GOTO(out_statfs, rc);
2353
2354         data = (void*)buf;
2355         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2356             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2357                 GOTO(out_statfs, rc = -EINVAL);
2358
2359         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2360         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2361
2362         if (type == LL_STATFS_MDC) {
2363                 if (index > 0)
2364                         GOTO(out_statfs, rc = -ENODEV);
2365                 client_obd = class_exp2obd(sbi->ll_mdc_exp);
2366         } else if (type == LL_STATFS_LOV) {
2367                 lov_obd = class_exp2obd(sbi->ll_osc_exp);
2368                 lov = &lov_obd->u.lov;
2369
2370                 if (index >= lov->desc.ld_tgt_count)
2371                         GOTO(out_statfs, rc = -ENODEV);
2372
2373                 if (!lov->lov_tgts[index])
2374                         /* Try again with the next index */
2375                         GOTO(out_statfs, rc = -EAGAIN);
2376
2377                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2378                 if (!lov->lov_tgts[index]->ltd_active)
2379                         GOTO(out_uuid, rc = -ENODATA);
2380         }
2381
2382         if (!client_obd)
2383                 GOTO(out_statfs, rc = -EINVAL);
2384
2385         rc = obd_statfs(client_obd, &stat_buf, cfs_time_current_64() - HZ, 1);
2386         if (rc)
2387                 GOTO(out_statfs, rc);
2388
2389         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
2390                 GOTO(out_statfs, rc = -EFAULT);
2391
2392 out_uuid:
2393         if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
2394                          data->ioc_plen2))
2395                 rc = -EFAULT;
2396
2397 out_statfs:
2398         if (buf)
2399                 obd_ioctl_freedata(buf, len);
2400         return rc;
2401 }
2402
2403 int ll_process_config(struct lustre_cfg *lcfg)
2404 {
2405         char *ptr;
2406         void *sb;
2407         struct lprocfs_static_vars lvars;
2408         unsigned long x;
2409         int rc = 0;
2410
2411         lprocfs_llite_init_vars(&lvars);
2412
2413         /* The instance name contains the sb: lustre-client-aacfe000 */
2414         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2415         if (!ptr || !*(++ptr))
2416                 return -EINVAL;
2417         if (sscanf(ptr, "%lx", &x) != 1)
2418                 return -EINVAL;
2419         sb = (void *)x;
2420         /* This better be a real Lustre superblock! */
2421         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2422
2423         /* Note we have not called client_common_fill_super yet, so
2424            proc fns must be able to handle that! */
2425         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2426                                       lcfg, sb);
2427         return(rc);
2428 }
2429
2430 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2431 {
2432         struct ll_sb_info *sbi;
2433
2434         LASSERT((seq != NULL) && (vfs != NULL));
2435         sbi = ll_s2sbi(vfs->mnt_sb);
2436
2437         if (sbi->ll_flags & LL_SBI_NOLCK)
2438                 seq_puts(seq, ",nolock");
2439
2440         if (sbi->ll_flags & LL_SBI_FLOCK)
2441                 seq_puts(seq, ",flock");
2442
2443         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2444                 seq_puts(seq, ",localflock");
2445
2446         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2447                 seq_puts(seq, ",user_xattr");
2448
2449         if (sbi->ll_flags & LL_SBI_ACL)
2450                 seq_puts(seq, ",acl");
2451
2452         RETURN(0);
2453 }