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