Whamcloud - gitweb
697e2f8155ddfc099dff60a68905e3f6369a7b17
[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  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002-2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
36
37 kmem_cache_t *ll_file_data_slab;
38
39 LIST_HEAD(ll_super_blocks);
40 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
41
42 extern struct address_space_operations ll_aops;
43 extern struct address_space_operations ll_dir_aops;
44
45 #ifndef log2
46 #define log2(n) ffz(~(n))
47 #endif
48
49 /* We need to have some extra twiddling here because some systems have
50  * no random state when they start up. */
51 static void
52 lustre_generate_random_uuid(class_uuid_t uuid)
53 {
54         struct timeval t;
55         int *i, j, k;
56
57         ENTRY;
58         LASSERT(sizeof(class_uuid_t) % sizeof(*i) == 0);
59
60         j = jiffies;
61         do_gettimeofday(&t);
62         k = t.tv_usec;
63
64         generate_random_uuid(uuid);
65
66         for (i = (int *)uuid; (char *)i < (char *)uuid + sizeof(class_uuid_t); i++) {
67                 *i ^= j ^ k;
68                 j = ((j << 8) & 0xffffff00) | ((j >> 24) & 0x000000ff);
69                 k = ((k >> 8) & 0x00ffffff) | ((k << 24) & 0xff000000);
70         }
71
72         EXIT;
73 }
74
75 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
76 {
77         struct ll_sb_info *sbi = NULL;
78         class_uuid_t uuid;
79         ENTRY;
80
81         OBD_ALLOC(sbi, sizeof(*sbi));
82         if (!sbi)
83                 RETURN(NULL);
84
85         spin_lock_init(&sbi->ll_lock);
86         INIT_LIST_HEAD(&sbi->ll_pglist);
87         sbi->ll_pglist_gen = 0;
88         if (num_physpages >> (20 - PAGE_SHIFT) < 512)
89                 sbi->ll_async_page_max = num_physpages / 2;
90         else
91                 sbi->ll_async_page_max = (num_physpages / 4) * 3;
92         sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
93                                            SBI_DEFAULT_READAHEAD_MAX);
94
95         INIT_LIST_HEAD(&sbi->ll_conn_chain);
96         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
97         ll_s2sbi_nocast(sb) = sbi;
98
99         lustre_generate_random_uuid(uuid);
100         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
101         CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
102
103         spin_lock(&ll_sb_lock);
104         list_add_tail(&sbi->ll_list, &ll_super_blocks);
105         spin_unlock(&ll_sb_lock);
106         RETURN(sbi);
107 }
108
109 void lustre_free_sbi(struct super_block *sb)
110 {
111         struct ll_sb_info *sbi = ll_s2sbi(sb);
112         ENTRY;
113
114         if (sbi != NULL) {
115                 spin_lock(&ll_sb_lock);
116                 list_del(&sbi->ll_list);
117                 spin_unlock(&ll_sb_lock);
118                 OBD_FREE(sbi, sizeof(*sbi));
119         }
120         ll_s2sbi_nocast(sb) = NULL;
121         EXIT;
122 }
123
124 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
125 {
126         struct inode *root = 0;
127         struct ll_sb_info *sbi = ll_s2sbi(sb);
128         struct obd_device *obd;
129         struct ll_fid rootfid;
130         struct obd_statfs osfs;
131         struct ptlrpc_request *request = NULL;
132         struct lustre_handle osc_conn = {0, };
133         struct lustre_handle mdc_conn = {0, };
134         struct lustre_md md;
135         struct obd_connect_data *data = NULL;
136         kdev_t devno;
137         int err;
138         ENTRY;
139
140         obd = class_name2obd(mdc);
141         if (!obd) {
142                 CERROR("MDC %s: not setup or attached\n", mdc);
143                 RETURN(-EINVAL);
144         }
145
146         OBD_ALLOC(data, sizeof(*data));
147         if (data == NULL)
148                 RETURN(-ENOMEM);
149
150         if (proc_lustre_fs_root) {
151                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
152                                                   osc, mdc);
153                 if (err < 0)
154                         CERROR("could not register mount in /proc/lustre");
155         }
156
157         if (sb->s_flags & MS_RDONLY)
158                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
159
160         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data);
161         if (err == -EBUSY) {
162                 CERROR("An MDS (mdc %s) is performing recovery, of which this"
163                        " client is not a part.  Please wait for recovery to "
164                        "complete, abort, or time out.\n", mdc);
165                 GOTO(out, err);
166         } else if (err) {
167                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
168                 GOTO(out, err);
169         }
170         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
171
172         err = obd_statfs(obd, &osfs, jiffies - HZ);
173         if (err)
174                 GOTO(out_mdc, err);
175
176         LASSERT(osfs.os_bsize);
177         sb->s_blocksize = osfs.os_bsize;
178         sb->s_blocksize_bits = log2(osfs.os_bsize);
179         sb->s_magic = LL_SUPER_MAGIC;
180         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
181         sbi->ll_namelen = osfs.os_namelen;
182
183         devno = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
184                              strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
185         /* s_dev is also used in lt_compare() to compare two fs */
186         sb->s_dev = devno;
187
188         obd = class_name2obd(osc);
189         if (!obd) {
190                 CERROR("OSC %s: not setup or attached\n", osc);
191                 GOTO(out_mdc, err);
192         }
193
194         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data);
195         if (err == -EBUSY) {
196                 CERROR("An OST (osc %s) is performing recovery, of which this"
197                        " client is not a part.  Please wait for recovery to "
198                        "complete, abort, or time out.\n", osc);
199                 GOTO(out, err);
200         } else if (err) {
201                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
202                 GOTO(out_mdc, err);
203         }
204         sbi->ll_osc_exp = class_conn2export(&osc_conn);
205
206         mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
207
208         if (!ll_async_page_slab) {
209                 ll_async_page_slab_size =
210                         size_round(sizeof(struct ll_async_page)) +
211                         obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
212                                             0, NULL, NULL, NULL);
213                 ll_async_page_slab = kmem_cache_create("ll_async_page",
214                                                        ll_async_page_slab_size,
215                                                        0, 0, NULL, NULL);
216                 if (!ll_async_page_slab)
217                         GOTO(out_osc, -ENOMEM);
218         }
219
220         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
221         if (err) {
222                 CERROR("cannot mds_connect: rc = %d\n", err);
223                 GOTO(out_osc, err);
224         }
225         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
226         sbi->ll_rootino = rootfid.id;
227
228         sb->s_op = &lustre_super_operations;
229
230         /* make root inode
231          * XXX: move this to after cbd setup? */
232         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
233                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
234         if (err) {
235                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
236                 GOTO(out_osc, err);
237         }
238
239         err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
240         if (err) {
241                 CERROR("failed to understand root inode md: rc = %d\n",err);
242                 ptlrpc_req_finished (request);
243                 GOTO(out_osc, err);
244         }
245
246         LASSERT(sbi->ll_rootino != 0);
247         root = ll_iget(sb, sbi->ll_rootino, &md);
248
249         ptlrpc_req_finished(request);
250
251         if (root == NULL || is_bad_inode(root)) {
252                 /* XXX might need iput() for bad inode */
253                 CERROR("lustre_lite: bad iget4 for root\n");
254                 GOTO(out_root, err = -EBADF);
255         }
256
257         err = ll_close_thread_start(&sbi->ll_lcq);
258         if (err) {
259                 CERROR("cannot start close thread: rc %d\n", err);
260                 GOTO(out_root, err);
261         }
262
263         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
264            backing dev info assigned to inode mapping is used for
265            determining maximal readahead. */
266 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
267     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
268         /* bug 2805 - set VM readahead to zero */
269         vm_max_readahead = vm_min_readahead = 0;
270 #endif
271
272         sb->s_root = d_alloc_root(root);
273         if (data != NULL)
274                 OBD_FREE(data, sizeof(*data));
275         RETURN(err);
276
277 out_root:
278         if (root)
279                 iput(root);
280 out_osc:
281         obd_disconnect(sbi->ll_osc_exp);
282 out_mdc:
283         obd_disconnect(sbi->ll_mdc_exp);
284 out:
285         if (data != NULL)
286                 OBD_FREE(data, sizeof(*data));
287         lprocfs_unregister_mountpoint(sbi);
288         RETURN(err);
289 }
290
291 void lustre_dump_inode(struct inode *inode)
292 {
293         struct list_head *tmp;
294         int dentry_count = 0;
295         BDEVNAME_DECLARE_STORAGE(buf);
296
297         LASSERT(inode != NULL);
298
299         list_for_each(tmp, &inode->i_dentry)
300                 dentry_count++;
301
302         CERROR("inode %p dump: dev=%s:%lu, mode=%o, count=%u, %d dentries\n",
303                inode, ll_bdevname(inode->i_sb, buf), inode->i_ino,
304                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
305 }
306
307 void lustre_dump_dentry(struct dentry *dentry, int recur)
308 {
309         struct list_head *tmp;
310         int subdirs = 0;
311
312         LASSERT(dentry != NULL);
313
314         list_for_each(tmp, &dentry->d_subdirs)
315                 subdirs++;
316
317         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
318                " flags=0x%x, vfs_flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
319                dentry->d_name.len, dentry->d_name.name,
320                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
321                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
322                dentry->d_flags, dentry->d_flags, dentry->d_fsdata, subdirs);
323         if (dentry->d_inode != NULL)
324                 lustre_dump_inode(dentry->d_inode);
325
326         if (recur == 0)
327                 return;
328
329         list_for_each(tmp, &dentry->d_subdirs) {
330                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
331                 lustre_dump_dentry(d, recur - 1);
332         }
333 }
334
335 void lustre_common_put_super(struct super_block *sb)
336 {
337         struct ll_sb_info *sbi = ll_s2sbi(sb);
338         struct hlist_node *tmp, *next;
339         ENTRY;
340
341         ll_close_thread_shutdown(sbi->ll_lcq);
342
343         list_del(&sbi->ll_conn_chain);
344         obd_disconnect(sbi->ll_osc_exp);
345
346         lprocfs_unregister_mountpoint(sbi);
347         if (sbi->ll_proc_root) {
348                 lprocfs_remove(sbi->ll_proc_root);
349                 sbi->ll_proc_root = NULL;
350         }
351
352         obd_disconnect(sbi->ll_mdc_exp);
353
354         // We do this to get rid of orphaned dentries. That is not really trw.
355         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
356                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
357                 CWARN("found orphan dentry %.*s (%p->%p) at unmount, dumping "
358                       "before and after shrink_dcache_parent\n",
359                       dentry->d_name.len, dentry->d_name.name, dentry, next);
360                 lustre_dump_dentry(dentry, 1);
361                 shrink_dcache_parent(dentry);
362                 lustre_dump_dentry(dentry, 1);
363         }
364         EXIT;
365 }
366
367 char *ll_read_opt(const char *opt, char *data)
368 {
369         char *value;
370         char *retval;
371         ENTRY;
372
373         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
374         if (strncmp(opt, data, strlen(opt)))
375                 RETURN(NULL);
376         if ((value = strchr(data, '=')) == NULL)
377                 RETURN(NULL);
378
379         value++;
380         OBD_ALLOC(retval, strlen(value) + 1);
381         if (!retval) {
382                 CERROR("out of memory!\n");
383                 RETURN(NULL);
384         }
385
386         memcpy(retval, value, strlen(value)+1);
387         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
388         RETURN(retval);
389 }
390
391 int ll_set_opt(const char *opt, char *data, int fl)
392 {
393         ENTRY;
394
395         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
396         if (strncmp(opt, data, strlen(opt)))
397                 RETURN(0);
398         else
399                 RETURN(fl);
400 }
401
402 void ll_options(char *options, char **ost, char **mdc, int *flags)
403 {
404         char *this_char;
405 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
406         char *opt_ptr = options;
407 #endif
408         ENTRY;
409
410         if (!options) {
411                 EXIT;
412                 return;
413         }
414
415 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
416         for (this_char = strtok (options, ",");
417              this_char != NULL;
418              this_char = strtok (NULL, ","))
419 #else
420         while ((this_char = strsep (&opt_ptr, ",")) != NULL)
421 #endif
422         {
423                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
424                 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
425                         continue;
426                 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
427                         continue;
428                 if (!(*flags & LL_SBI_NOLCK) &&
429                     ((*flags) = (*flags) |
430                                 ll_set_opt("nolock", this_char,
431                                            LL_SBI_NOLCK)))
432                         continue;
433         }
434         EXIT;
435 }
436
437 void ll_lli_init(struct ll_inode_info *lli)
438 {
439         sema_init(&lli->lli_open_sem, 1);
440         sema_init(&lli->lli_size_sem, 1);
441         lli->lli_size_pid = 0;
442         lli->lli_flags = 0;
443         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
444         spin_lock_init(&lli->lli_lock);
445         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
446         lli->lli_inode_magic = LLI_INODE_MAGIC;
447 }
448
449 int ll_fill_super(struct super_block *sb, void *data, int silent)
450 {
451         struct ll_sb_info *sbi;
452         char *osc = NULL;
453         char *mdc = NULL;
454         int err;
455         ENTRY;
456
457         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
458
459         sbi = lustre_init_sbi(sb);
460         if (!sbi)
461                 RETURN(-ENOMEM);
462
463         ll_options(data, &osc, &mdc, &sbi->ll_flags);
464
465         if (!osc) {
466                 CERROR("no osc\n");
467                 GOTO(out, err = -EINVAL);
468         }
469
470         if (!mdc) {
471                 CERROR("no mdc\n");
472                 GOTO(out, err = -EINVAL);
473         }
474
475         err = lustre_common_fill_super(sb, mdc, osc);
476 out:
477         if (err)
478                 lustre_free_sbi(sb);
479
480         if (mdc)
481                 OBD_FREE(mdc, strlen(mdc) + 1);
482         if (osc)
483                 OBD_FREE(osc, strlen(osc) + 1);
484
485         RETURN(err);
486 } /* ll_read_super */
487
488 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
489                        struct config_llog_instance *cfg, int allow_recov)
490 {
491         struct lustre_cfg *lcfg = NULL;
492         struct lustre_cfg_bufs bufs;
493         struct portals_cfg pcfg;
494         char * peer = "MDS_PEER_UUID";
495         struct obd_device *obd;
496         struct lustre_handle mdc_conn = {0, };
497         struct obd_export *exp;
498         char * name = "mdc_dev";
499         class_uuid_t uuid;
500         struct obd_uuid mdc_uuid;
501         struct llog_ctxt *ctxt;
502         int rc = 0;
503         int err;
504         ENTRY;
505
506         if (lmd_bad_magic(lmd))
507                 RETURN(-EINVAL);
508
509         lustre_generate_random_uuid(uuid);
510         class_uuid_unparse(uuid, &mdc_uuid);
511         CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid);
512
513         if (lmd->lmd_local_nid) {
514                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
515                 pcfg.pcfg_nal = lmd->lmd_nal;
516                 pcfg.pcfg_nid = lmd->lmd_local_nid;
517                 err = libcfs_nal_cmd(&pcfg);
518                 if (err <0)
519                         GOTO(out, err);
520         }
521
522         if (lmd->lmd_nal == SOCKNAL ||
523             lmd->lmd_nal == OPENIBNAL ||
524             lmd->lmd_nal == IIBNAL ||
525             lmd->lmd_nal == VIBNAL ||
526             lmd->lmd_nal == RANAL) {
527                 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
528                 pcfg.pcfg_nal     = lmd->lmd_nal;
529                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
530                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
531                 pcfg.pcfg_misc    = lmd->lmd_port;
532                 err = libcfs_nal_cmd(&pcfg);
533                 if (err <0)
534                         GOTO(out, err);
535         }
536
537         lustre_cfg_bufs_reset(&bufs, name);
538         lustre_cfg_bufs_set_string(&bufs, 1, peer);
539
540         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
541         lcfg->lcfg_nal = lmd->lmd_nal;
542         lcfg->lcfg_nid = lmd->lmd_server_nid;
543         LASSERT(lcfg->lcfg_nal);
544         LASSERT(lcfg->lcfg_nid);
545         err = class_process_config(lcfg);
546         lustre_cfg_free(lcfg);
547         if (err < 0)
548                 GOTO(out_del_conn, err);
549
550         lustre_cfg_bufs_reset(&bufs, name);
551         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MDC_NAME);
552         lustre_cfg_bufs_set_string(&bufs, 2, mdc_uuid.uuid);
553
554         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
555         err = class_process_config(lcfg);
556         lustre_cfg_free(lcfg);
557         if (err < 0)
558                 GOTO(out_del_uuid, err);
559
560         lustre_cfg_bufs_reset(&bufs, name);
561         lustre_cfg_bufs_set_string(&bufs, 1, lmd->lmd_mds);
562         lustre_cfg_bufs_set_string(&bufs, 2, peer);
563
564         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
565         err = class_process_config(lcfg);
566         lustre_cfg_free(lcfg);
567         if (err < 0)
568                 GOTO(out_detach, err);
569
570         obd = class_name2obd(name);
571         if (obd == NULL)
572                 GOTO(out_cleanup, err = -EINVAL);
573
574         /* Disable initial recovery on this import */
575         err = obd_set_info(obd->obd_self_export,
576                            strlen("initial_recov"), "initial_recov",
577                            sizeof(allow_recov), &allow_recov);
578         if (err)
579                 GOTO(out_cleanup, err);
580
581         err = obd_connect(&mdc_conn, obd, &mdc_uuid, NULL /* ocd */);
582         if (err) {
583                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
584                 GOTO(out_cleanup, err);
585         }
586
587         exp = class_conn2export(&mdc_conn);
588
589         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
590 #if 1
591         rc = class_config_parse_llog(ctxt, profile, cfg);
592 #else
593         /*
594          * For debugging, it's useful to just dump the log
595          */
596         rc = class_config_dump_llog(ctxt, profile, cfg);
597 #endif
598         switch (rc) {
599         case 0:
600                 break;
601         case -EINVAL:
602                 LCONSOLE_ERROR("%s: The configuration '%s' could not be read "
603                                "from the MDS.  Make sure this client and the "
604                                "MDS are running compatible versions of "
605                                "Lustre.\n",
606                                obd->obd_name, profile);
607                 /* fall through */
608         default:
609                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
610                 break;
611         }
612
613         err = obd_disconnect(exp);
614
615 out_cleanup:
616         lustre_cfg_bufs_reset(&bufs, name);
617         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
618         err = class_process_config(lcfg);
619         lustre_cfg_free(lcfg);
620         if (err < 0)
621                 GOTO(out, err);
622
623 out_detach:
624         lustre_cfg_bufs_reset(&bufs, name);
625         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
626         err = class_process_config(lcfg);
627         lustre_cfg_free(lcfg);
628         if (err < 0)
629                 GOTO(out, err);
630
631 out_del_uuid:
632         lustre_cfg_bufs_reset(&bufs, name);
633         lustre_cfg_bufs_set_string(&bufs, 1, peer);
634         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
635         err = class_process_config(lcfg);
636         lustre_cfg_free(lcfg);
637
638 out_del_conn:
639         if (lmd->lmd_nal == SOCKNAL ||
640             lmd->lmd_nal == OPENIBNAL ||
641             lmd->lmd_nal == IIBNAL ||
642             lmd->lmd_nal == VIBNAL ||
643             lmd->lmd_nal == RANAL) {
644                 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
645                 pcfg.pcfg_nal     = lmd->lmd_nal;
646                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
647                 pcfg.pcfg_flags   = 1;          /* single_share */
648                 err = libcfs_nal_cmd(&pcfg);
649                 if (err <0)
650                         GOTO(out, err);
651         }
652 out:
653         if (rc == 0)
654                 rc = err;
655
656         RETURN(rc);
657 }
658
659 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
660 {
661         struct lustre_cfg *lcfg;
662         struct lustre_cfg_bufs bufs;
663         struct obd_device *obd;
664         int next = 0;
665
666         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL){
667                 int err;
668
669                 /* the lcfg is almost the same for both ops */
670                 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
671                 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
672
673                 err = class_process_config(lcfg);
674                 if (err) {
675                         CERROR("cleanup failed: %s\n", obd->obd_name);
676                         //continue;
677                 }
678
679                 lcfg->lcfg_command = LCFG_DETACH;
680                 err = class_process_config(lcfg);
681                 lustre_cfg_free(lcfg);
682                 if (err) {
683                         CERROR("detach failed: %s\n", obd->obd_name);
684                         //continue;
685                 }
686         }
687
688         if (sbi->ll_lmd != NULL)
689                 class_del_profile(sbi->ll_lmd->lmd_profile);
690 }
691
692 int lustre_fill_super(struct super_block *sb, void *data, int silent)
693 {
694         struct lustre_mount_data * lmd = data;
695         struct ll_sb_info *sbi;
696         char *osc = NULL;
697         char *mdc = NULL;
698         int err;
699         ENTRY;
700
701         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
702         if (lmd_bad_magic(lmd))
703                 RETURN(-EINVAL);
704
705         sbi = lustre_init_sbi(sb);
706         if (!sbi)
707                 RETURN(-ENOMEM);
708
709         if (lmd->lmd_profile) {
710                 struct lustre_profile *lprof;
711                 struct config_llog_instance cfg;
712                 int len;
713
714                 if (lmd->lmd_mds[0] == '\0') {
715                         CERROR("no mds name\n");
716                         GOTO(out_free, err = -EINVAL);
717                 }
718
719                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
720                 if (sbi->ll_lmd == NULL)
721                         GOTO(out_free, err = -ENOMEM);
722                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
723
724                 /* generate a string unique to this super, let's try
725                  the address of the super itself.*/
726                 len = (sizeof(sb) * 2) + 1;
727                 OBD_ALLOC(sbi->ll_instance, len);
728                 if (sbi->ll_instance == NULL)
729                         GOTO(out_free, err = -ENOMEM);
730                 sprintf(sbi->ll_instance, "%p", sb);
731
732                 cfg.cfg_instance = sbi->ll_instance;
733                 cfg.cfg_uuid = sbi->ll_sb_uuid;
734                 cfg.cfg_local_nid = lmd->lmd_local_nid;
735                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
736                 if (err < 0) {
737                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
738
739                         GOTO(out_free, err);
740                 }
741
742                 lprof = class_get_profile(lmd->lmd_profile);
743                 if (lprof == NULL) {
744                         CERROR("No profile found: %s\n", lmd->lmd_profile);
745                         GOTO(out_free, err = -EINVAL);
746                 }
747                 if (osc)
748                         OBD_FREE(osc, strlen(osc) + 1);
749                 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
750                           strlen(sbi->ll_instance) + 2);
751                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
752
753                 if (mdc)
754                         OBD_FREE(mdc, strlen(mdc) + 1);
755                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
756                           strlen(sbi->ll_instance) + 2);
757                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
758         }
759
760         if (!osc) {
761                 CERROR("no osc\n");
762                 GOTO(out_free, err = -EINVAL);
763         }
764
765         if (!mdc) {
766                 CERROR("no mdc\n");
767                 GOTO(out_free, err = -EINVAL);
768         }
769
770         err = lustre_common_fill_super(sb, mdc, osc);
771
772         if (err)
773                 GOTO(out_free, err);
774
775 out_dev:
776         if (mdc)
777                 OBD_FREE(mdc, strlen(mdc) + 1);
778         if (osc)
779                 OBD_FREE(osc, strlen(osc) + 1);
780
781         RETURN(err);
782
783 out_free:
784         if (sbi->ll_lmd) {
785                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
786                 int err;
787
788                 if (sbi->ll_instance != NULL) {
789                         char * cln_prof;
790                         struct config_llog_instance cfg;
791
792                         cfg.cfg_instance = sbi->ll_instance;
793                         cfg.cfg_uuid = sbi->ll_sb_uuid;
794
795                         OBD_ALLOC(cln_prof, len);
796                         sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
797
798                         err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,0);
799                         if (err < 0) {
800                                 CERROR("Unable to process log: %s\n", cln_prof);
801                                 lustre_manual_cleanup(sbi);
802                         }
803                         OBD_FREE(cln_prof, len);
804                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
805                 }
806                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
807         }
808         lustre_free_sbi(sb);
809
810         goto out_dev;
811 } /* lustre_fill_super */
812
813 void lustre_put_super(struct super_block *sb)
814 {
815         struct obd_device *obd;
816         struct ll_sb_info *sbi = ll_s2sbi(sb);
817         int force_umount = 0;
818         ENTRY;
819
820         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
821         obd = class_exp2obd(sbi->ll_mdc_exp);
822         if (obd)
823                 force_umount = obd->obd_no_recov;
824         obd = NULL;
825
826         lustre_common_put_super(sb);
827
828         if (sbi->ll_lmd != NULL) {
829 #if 0
830                 char * cln_prof;
831                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
832                 int err;
833                 struct config_llog_instance cfg;
834
835                 if (force_umount) {
836                         CERROR("force umount, doing manual cleanup\n");
837                         lustre_manual_cleanup(sbi);
838                         GOTO(free_lmd, 0);
839                 }
840
841                 cfg.cfg_instance = sbi->ll_instance;
842                 cfg.cfg_uuid = sbi->ll_sb_uuid;
843
844                 OBD_ALLOC(cln_prof, len);
845                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
846
847                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
848                 if (err < 0) {
849                         CERROR("Unable to process log: %s, doing manual cleanup"
850                                "\n", cln_prof);
851                         lustre_manual_cleanup(sbi);
852                 }
853
854                 OBD_FREE(cln_prof, len);
855         free_lmd:
856 #else
857                 lustre_manual_cleanup(sbi);
858 #endif
859                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
860                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
861         }
862
863         lustre_free_sbi(sb);
864
865         EXIT;
866 } /* lustre_put_super */
867
868 #ifdef HAVE_REGISTER_CACHE
869 #include <linux/cache_def.h>
870 #ifdef HAVE_CACHE_RETURN_INT
871 static int
872 #else
873 static void
874 #endif
875 ll_shrink_cache(int priority, unsigned int gfp_mask)
876 {
877         struct ll_sb_info *sbi;
878         int count = 0;
879
880         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
881                 count += llap_shrink_cache(sbi, priority);
882
883 #ifdef HAVE_CACHE_RETURN_INT
884         return count;
885 #endif
886 }
887
888 struct cache_definition ll_cache_definition = {
889         .name = "llap_cache",
890         .shrink = ll_shrink_cache
891 };
892 #endif /* HAVE_REGISTER_CACHE */
893
894 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
895 {
896         struct inode *inode = NULL;
897         l_lock(&lock->l_resource->lr_namespace->ns_lock);
898         if (lock->l_ast_data) {
899                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
900                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
901                         inode = igrab(lock->l_ast_data);
902                 } else {
903                         inode = lock->l_ast_data;
904                         __LDLM_DEBUG(inode->i_state & I_FREEING ?
905                                      D_INFO : D_WARNING, lock,
906                                      "l_ast_data %p is bogus: magic %08x",
907                                      lock->l_ast_data, lli->lli_inode_magic);
908                         inode = NULL;
909                 }
910         }
911         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
912         return inode;
913 }
914
915 static int null_if_equal(struct ldlm_lock *lock, void *data)
916 {
917         if (data == lock->l_ast_data) {
918                 lock->l_ast_data = NULL;
919
920                 if (lock->l_req_mode != lock->l_granted_mode)
921                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
922         }
923
924         return LDLM_ITER_CONTINUE;
925 }
926
927 void ll_clear_inode(struct inode *inode)
928 {
929         struct ll_fid fid;
930         struct ll_inode_info *lli = ll_i2info(inode);
931         struct ll_sb_info *sbi = ll_i2sbi(inode);
932         ENTRY;
933
934         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
935                inode->i_generation, inode);
936
937         ll_inode2fid(&fid, inode);
938         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
939         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
940
941         if (lli->lli_smd)
942                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
943                                   null_if_equal, inode);
944
945         if (lli->lli_smd) {
946                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
947                 lli->lli_smd = NULL;
948         }
949
950         if (lli->lli_symlink_name) {
951                 OBD_FREE(lli->lli_symlink_name,
952                          strlen(lli->lli_symlink_name) + 1);
953                 lli->lli_symlink_name = NULL;
954         }
955         lli->lli_inode_magic = LLI_INODE_DEAD;
956
957         EXIT;
958 }
959
960 /* If this inode has objects allocated to it (lsm != NULL), then the OST
961  * object(s) determine the file size and mtime.  Otherwise, the MDS will
962  * keep these values until such a time that objects are allocated for it.
963  * We do the MDS operations first, as it is checking permissions for us.
964  * We don't to the MDS RPC if there is nothing that we want to store there,
965  * otherwise there is no harm in updating mtime/atime on the MDS if we are
966  * going to do an RPC anyways.
967  *
968  * If we are doing a truncate, we will send the mtime and ctime updates
969  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
970  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
971  * at the same time.
972  */
973 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
974 {
975         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
976         struct ll_sb_info *sbi = ll_i2sbi(inode);
977         struct ptlrpc_request *request = NULL;
978         struct mdc_op_data op_data;
979         int ia_valid = attr->ia_valid;
980         int rc = 0;
981         ENTRY;
982
983         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
984         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
985
986         if (ia_valid & ATTR_SIZE) {
987                 if (attr->ia_size > ll_file_maxbytes(inode)) {
988                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
989                                attr->ia_size, ll_file_maxbytes(inode));
990                         RETURN(-EFBIG);
991                 }
992
993                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
994         }
995
996         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
997         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
998                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
999                         RETURN(-EPERM);
1000         }
1001
1002         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1003         if (attr->ia_valid & ATTR_CTIME) {
1004                 attr->ia_ctime = CURRENT_TIME;
1005                 attr->ia_valid |= ATTR_CTIME_SET;
1006         }
1007         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1008                 attr->ia_atime = CURRENT_TIME;
1009                 attr->ia_valid |= ATTR_ATIME_SET;
1010         }
1011         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1012                 attr->ia_mtime = CURRENT_TIME;
1013                 attr->ia_valid |= ATTR_MTIME_SET;
1014         }
1015
1016         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1017                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1018                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1019                        CURRENT_SECONDS);
1020         if (lsm)
1021                 attr->ia_valid &= ~ATTR_SIZE;
1022
1023         /* If only OST attributes being set on objects, don't do MDS RPC.
1024          * In that case, we need to check permissions and update the local
1025          * inode ourselves so we can call obdo_from_inode() always. */
1026         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
1027                 struct lustre_md md;
1028                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1029
1030                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1031                                  attr, NULL, 0, NULL, 0, &request);
1032
1033                 if (rc) {
1034                         ptlrpc_req_finished(request);
1035                         if (rc != -EPERM && rc != -EACCES)
1036                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1037                         RETURN(rc);
1038                 }
1039
1040                 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
1041                 if (rc) {
1042                         ptlrpc_req_finished(request);
1043                         RETURN(rc);
1044                 }
1045
1046                 /* We call inode_setattr to adjust timestamps, but we first
1047                  * clear ATTR_SIZE to avoid invoking vmtruncate.
1048                  *
1049                  * NB: ATTR_SIZE will only be set at this point if the size
1050                  * resides on the MDS, ie, this file has no objects. */
1051                 attr->ia_valid &= ~ATTR_SIZE;
1052                 inode_setattr(inode, attr);
1053
1054                 ll_update_inode(inode, md.body, md.lsm);
1055                 ptlrpc_req_finished(request);
1056
1057                 if (!lsm || !S_ISREG(inode->i_mode)) {
1058                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1059                         RETURN(0);
1060                 }
1061         } else {
1062                 /* The OST doesn't check permissions, but the alternative is
1063                  * a gratuitous RPC to the MDS.  We already rely on the client
1064                  * to do read/write/truncate permission checks, so is mtime OK?
1065                  */
1066                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1067                         /* from sys_utime() */
1068                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1069                                 if (current->fsuid != inode->i_uid &&
1070                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1071                                         RETURN(rc);
1072                         } else {
1073                                 /* from inode_change_ok() */
1074                                 if (current->fsuid != inode->i_uid &&
1075                                     !capable(CAP_FOWNER))
1076                                         RETURN(-EPERM);
1077                         }
1078                 }
1079
1080                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1081                 inode_setattr(inode, attr);
1082         }
1083
1084         /* We really need to get our PW lock before we change inode->i_size.
1085          * If we don't we can race with other i_size updaters on our node, like
1086          * ll_file_read.  We can also race with i_size propogation to other
1087          * nodes through dirtying and writeback of final cached pages.  This
1088          * last one is especially bad for racing o_append users on other
1089          * nodes. */
1090         if (ia_valid & ATTR_SIZE) {
1091                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1092                                                            OBD_OBJECT_EOF } };
1093                 struct lustre_handle lockh = { 0 };
1094                 struct ll_inode_info *lli = ll_i2info(inode);
1095                 int err, ast_flags = 0;
1096                 /* XXX when we fix the AST intents to pass the discard-range
1097                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1098                  * XXX here. */
1099                 if (attr->ia_size == 0)
1100                         ast_flags = LDLM_AST_DISCARD_DATA;
1101
1102                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1103                                     ast_flags);
1104                 if (rc != 0)
1105                         RETURN(rc);
1106
1107                 down(&lli->lli_size_sem);
1108                 lli->lli_size_pid = current->pid;
1109                 rc = vmtruncate(inode, attr->ia_size);
1110                 // if vmtruncate returned 0, then ll_truncate dropped _size_sem
1111                 if (rc != 0) {
1112                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1113                         lli->lli_size_pid = 0;
1114                         up(&lli->lli_size_sem);
1115                 }
1116
1117                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1118                 if (err) {
1119                         CERROR("ll_extent_unlock failed: %d\n", err);
1120                         if (!rc)
1121                                 rc = err;
1122                 }
1123         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1124                 struct obdo oa;
1125
1126                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1127                        inode->i_ino, LTIME_S(attr->ia_mtime));
1128                 oa.o_id = lsm->lsm_object_id;
1129                 oa.o_valid = OBD_MD_FLID;
1130                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1131                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1132                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
1133                 if (rc)
1134                         CERROR("obd_setattr fails: rc=%d\n", rc);
1135         }
1136         RETURN(rc);
1137 }
1138
1139 int ll_setattr(struct dentry *de, struct iattr *attr)
1140 {
1141         LBUG(); /* code is unused, but leave this in case of VFS changes */
1142         RETURN(-ENOSYS);
1143 }
1144
1145 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1146                        unsigned long max_age)
1147 {
1148         struct ll_sb_info *sbi = ll_s2sbi(sb);
1149         struct obd_statfs obd_osfs;
1150         int rc;
1151         ENTRY;
1152
1153         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1154         if (rc) {
1155                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1156                 RETURN(rc);
1157         }
1158
1159         osfs->os_type = sb->s_magic;
1160
1161         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1162                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1163
1164         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1165         if (rc) {
1166                 CERROR("obd_statfs fails: rc = %d\n", rc);
1167                 RETURN(rc);
1168         }
1169
1170         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1171                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1172                obd_osfs.os_files);
1173
1174         osfs->os_blocks = obd_osfs.os_blocks;
1175         osfs->os_bfree = obd_osfs.os_bfree;
1176         osfs->os_bavail = obd_osfs.os_bavail;
1177
1178         /* If we don't have as many objects free on the OST as inodes
1179          * on the MDS, we reduce the total number of inodes to
1180          * compensate, so that the "inodes in use" number is correct.
1181          */
1182         if (obd_osfs.os_ffree < osfs->os_ffree) {
1183                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1184                         obd_osfs.os_ffree;
1185                 osfs->os_ffree = obd_osfs.os_ffree;
1186         }
1187
1188         RETURN(rc);
1189 }
1190
1191 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1192 {
1193         struct obd_statfs osfs;
1194         int rc;
1195
1196         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1197         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1198
1199         /* For now we will always get up-to-date statfs values, but in the
1200          * future we may allow some amount of caching on the client (e.g.
1201          * from QOS or lprocfs updates). */
1202         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1203         if (rc)
1204                 return rc;
1205
1206         statfs_unpack(sfs, &osfs);
1207
1208         if (sizeof(sfs->f_blocks) == 4) {
1209                 while (osfs.os_blocks > ~0UL) {
1210                         sfs->f_bsize <<= 1;
1211
1212                         osfs.os_blocks >>= 1;
1213                         osfs.os_bfree >>= 1;
1214                         osfs.os_bavail >>= 1;
1215                 }
1216         }
1217
1218         sfs->f_blocks = osfs.os_blocks;
1219         sfs->f_bfree = osfs.os_bfree;
1220         sfs->f_bavail = osfs.os_bavail;
1221
1222         return 0;
1223 }
1224
1225 void ll_update_inode(struct inode *inode, struct mds_body *body,
1226                      struct lov_stripe_md *lsm)
1227 {
1228         struct ll_inode_info *lli = ll_i2info(inode);
1229
1230         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1231         if (lsm != NULL) {
1232                 if (lli->lli_smd == NULL) {
1233                         lli->lli_smd = lsm;
1234                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1235                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1236                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1237                 } else {
1238                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1239                                 CERROR("lsm mismatch for inode %ld\n",
1240                                        inode->i_ino);
1241                                 CERROR("lli_smd:\n");
1242                                 dump_lsm(D_ERROR, lli->lli_smd);
1243                                 CERROR("lsm:\n");
1244                                 dump_lsm(D_ERROR, lsm);
1245                                 LBUG();
1246                         }
1247                 }
1248                 /* bug 2844 - limit i_blksize for broken user-space apps */
1249                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1250                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1251                 if (lli->lli_smd != lsm)
1252                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1253         }
1254
1255         if (body->valid & OBD_MD_FLID)
1256                 inode->i_ino = body->ino;
1257         if (body->valid & OBD_MD_FLATIME)
1258                 LTIME_S(inode->i_atime) = body->atime;
1259         if (body->valid & OBD_MD_FLMTIME &&
1260             body->mtime > LTIME_S(inode->i_mtime)) {
1261                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1262                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1263                 LTIME_S(inode->i_mtime) = body->mtime;
1264         }
1265         if (body->valid & OBD_MD_FLCTIME &&
1266             body->ctime > LTIME_S(inode->i_ctime))
1267                 LTIME_S(inode->i_ctime) = body->ctime;
1268         if (body->valid & OBD_MD_FLMODE)
1269                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1270         if (body->valid & OBD_MD_FLTYPE)
1271                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1272         if (body->valid & OBD_MD_FLUID)
1273                 inode->i_uid = body->uid;
1274         if (body->valid & OBD_MD_FLGID)
1275                 inode->i_gid = body->gid;
1276         if (body->valid & OBD_MD_FLFLAGS)
1277                 inode->i_flags = body->flags;
1278         if (body->valid & OBD_MD_FLNLINK)
1279                 inode->i_nlink = body->nlink;
1280         if (body->valid & OBD_MD_FLGENER)
1281                 inode->i_generation = body->generation;
1282         if (body->valid & OBD_MD_FLRDEV)
1283 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1284                 inode->i_rdev = body->rdev;
1285 #else
1286                 inode->i_rdev = old_decode_dev(body->rdev);
1287 #endif
1288         if (body->valid & OBD_MD_FLSIZE)
1289                 inode->i_size = body->size;
1290         if (body->valid & OBD_MD_FLBLOCKS)
1291                 inode->i_blocks = body->blocks;
1292
1293         if (body->valid & OBD_MD_FLSIZE)
1294                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1295 }
1296
1297 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1298 static struct backing_dev_info ll_backing_dev_info = {
1299         .ra_pages       = 0,    /* No readahead */
1300         .memory_backed  = 0,    /* Does contribute to dirty memory */
1301 };
1302 #endif
1303
1304 void ll_read_inode2(struct inode *inode, void *opaque)
1305 {
1306         struct lustre_md *md = opaque;
1307         struct ll_inode_info *lli = ll_i2info(inode);
1308         ENTRY;
1309
1310         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1311                inode->i_generation, inode);
1312
1313         ll_lli_init(lli);
1314
1315         LASSERT(!lli->lli_smd);
1316
1317         /* Core attributes from the MDS first.  This is a new inode, and
1318          * the VFS doesn't zero times in the core inode so we have to do
1319          * it ourselves.  They will be overwritten by either MDS or OST
1320          * attributes - we just need to make sure they aren't newer. */
1321         LTIME_S(inode->i_mtime) = 0;
1322         LTIME_S(inode->i_atime) = 0;
1323         LTIME_S(inode->i_ctime) = 0;
1324         inode->i_rdev = 0;
1325         ll_update_inode(inode, md->body, md->lsm);
1326
1327         /* OIDEBUG(inode); */
1328
1329         if (S_ISREG(inode->i_mode)) {
1330                 inode->i_op = &ll_file_inode_operations;
1331                 inode->i_fop = &ll_file_operations;
1332                 inode->i_mapping->a_ops = &ll_aops;
1333                 EXIT;
1334         } else if (S_ISDIR(inode->i_mode)) {
1335                 inode->i_op = &ll_dir_inode_operations;
1336                 inode->i_fop = &ll_dir_operations;
1337                 inode->i_mapping->a_ops = &ll_dir_aops;
1338                 EXIT;
1339         } else if (S_ISLNK(inode->i_mode)) {
1340                 inode->i_op = &ll_fast_symlink_inode_operations;
1341                 EXIT;
1342         } else {
1343                 inode->i_op = &ll_special_inode_operations;
1344
1345 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1346                 init_special_inode(inode, inode->i_mode,
1347                                    kdev_t_to_nr(inode->i_rdev));
1348
1349                 /* initializing backing dev info. */
1350                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1351 #else
1352                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1353 #endif
1354                 lli->ll_save_ifop = inode->i_fop;
1355
1356                 if (S_ISCHR(inode->i_mode))
1357                         inode->i_fop = &ll_special_chr_inode_fops;
1358                 else if (S_ISBLK(inode->i_mode))
1359                         inode->i_fop = &ll_special_blk_inode_fops;
1360                 else if (S_ISFIFO(inode->i_mode))
1361                         inode->i_fop = &ll_special_fifo_inode_fops;
1362                 else if (S_ISSOCK(inode->i_mode))
1363                         inode->i_fop = &ll_special_sock_inode_fops;
1364                 EXIT;
1365         }
1366 }
1367
1368 int ll_iocontrol(struct inode *inode, struct file *file,
1369                  unsigned int cmd, unsigned long arg)
1370 {
1371         struct ll_sb_info *sbi = ll_i2sbi(inode);
1372         struct ptlrpc_request *req = NULL;
1373         int rc, flags = 0;
1374         ENTRY;
1375
1376         switch(cmd) {
1377         case EXT3_IOC_GETFLAGS: {
1378                 struct ll_fid fid;
1379                 unsigned long valid = OBD_MD_FLFLAGS;
1380                 struct mds_body *body;
1381
1382                 ll_inode2fid(&fid, inode);
1383                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1384                 if (rc) {
1385                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1386                         RETURN(-abs(rc));
1387                 }
1388
1389                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1390
1391                 if (body->flags & S_APPEND)
1392                         flags |= EXT3_APPEND_FL;
1393                 if (body->flags & S_IMMUTABLE)
1394                         flags |= EXT3_IMMUTABLE_FL;
1395                 if (body->flags & S_NOATIME)
1396                         flags |= EXT3_NOATIME_FL;
1397
1398                 ptlrpc_req_finished (req);
1399
1400                 RETURN(put_user(flags, (int *)arg));
1401         }
1402         case EXT3_IOC_SETFLAGS: {
1403                 struct mdc_op_data op_data;
1404                 struct iattr attr;
1405                 struct obdo *oa;
1406                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1407
1408                 if (get_user(flags, (int *)arg))
1409                         RETURN(-EFAULT);
1410
1411                 oa = obdo_alloc();
1412                 if (!oa)
1413                         RETURN(-ENOMEM);
1414
1415                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1416
1417                 memset(&attr, 0x0, sizeof(attr));
1418                 attr.ia_attr_flags = flags;
1419                 attr.ia_valid |= ATTR_ATTR_FLAG;
1420
1421                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1422                                  &attr, NULL, 0, NULL, 0, &req);
1423                 if (rc) {
1424                         ptlrpc_req_finished(req);
1425                         if (rc != -EPERM && rc != -EACCES)
1426                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1427                         obdo_free(oa);
1428                         RETURN(rc);
1429                 }
1430                 ptlrpc_req_finished(req);
1431
1432                 oa->o_id = lsm->lsm_object_id;
1433                 oa->o_flags = flags;
1434                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1435
1436                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1437                 obdo_free(oa);
1438                 if (rc) {
1439                         if (rc != -EPERM && rc != -EACCES)
1440                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1441                         RETURN(rc);
1442                 }
1443
1444                 if (flags & EXT3_APPEND_FL)
1445                         inode->i_flags |= S_APPEND;
1446                 else
1447                         inode->i_flags &= ~S_APPEND;
1448                 if (flags & EXT3_IMMUTABLE_FL)
1449                         inode->i_flags |= S_IMMUTABLE;
1450                 else
1451                         inode->i_flags &= ~S_IMMUTABLE;
1452                 if (flags & EXT3_NOATIME_FL)
1453                         inode->i_flags |= S_NOATIME;
1454                 else
1455                         inode->i_flags &= ~S_NOATIME;
1456
1457                 RETURN(0);
1458         }
1459         default:
1460                 RETURN(-ENOSYS);
1461         }
1462
1463         RETURN(0);
1464 }
1465
1466 void ll_umount_begin(struct super_block *sb)
1467 {
1468         struct ll_sb_info *sbi = ll_s2sbi(sb);
1469         struct obd_device *obd;
1470         struct obd_ioctl_data ioc_data = { 0 };
1471         ENTRY;
1472         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1473
1474         obd = class_exp2obd(sbi->ll_mdc_exp);
1475         if (obd == NULL) {
1476                 CERROR("Invalid MDC connection handle "LPX64"\n",
1477                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1478                 EXIT;
1479                 return;
1480         }
1481         obd->obd_no_recov = 1;
1482         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1483                       &ioc_data, NULL);
1484
1485         obd = class_exp2obd(sbi->ll_osc_exp);
1486         if (obd == NULL) {
1487                 CERROR("Invalid LOV connection handle "LPX64"\n",
1488                        sbi->ll_osc_exp->exp_handle.h_cookie);
1489                 EXIT;
1490                 return;
1491         }
1492
1493         obd->obd_no_recov = 1;
1494         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1495                       &ioc_data, NULL);
1496
1497         /* Really, we'd like to wait until there are no requests outstanding,
1498          * and then continue.  For now, we just invalidate the requests,
1499          * schedule, and hope.
1500          */
1501         schedule();
1502
1503         EXIT;
1504 }
1505
1506 int lustre_remount_fs(struct super_block *sb, int *flags, char *data)
1507 {
1508         struct ll_sb_info *sbi = ll_s2sbi(sb);
1509         int err;
1510         __u32 read_only;
1511
1512         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1513                 read_only = *flags & MS_RDONLY;
1514                 err = obd_set_info(sbi->ll_mdc_exp, strlen("read-only"),
1515                                    "read-only", sizeof(read_only), &read_only);
1516                 if (err) {
1517                         CERROR("Failed to change the read-only flag during "
1518                                "remount: %d\n", err);
1519                         return err;
1520                 }
1521         }
1522         return 0;
1523 }
1524
1525 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1526                   struct ptlrpc_request *req, int offset,struct super_block *sb)
1527 {
1528         struct lustre_md md;
1529         int rc = 0;
1530
1531         rc = mdc_req2lustre_md(req, offset, exp, &md);
1532         if (rc)
1533                 RETURN(rc);
1534
1535         if (*inode) {
1536                 ll_update_inode(*inode, md.body, md.lsm);
1537         } else {
1538                 LASSERT(sb);
1539                 *inode = ll_iget(sb, md.body->ino, &md);
1540                 if (*inode == NULL || is_bad_inode(*inode)) {
1541                         /* free the lsm if we allocated one above */
1542                         if (md.lsm != NULL)
1543                                 obd_free_memmd(exp, &md.lsm);
1544                         rc = -ENOMEM;
1545                         CERROR("new_inode -fatal: rc %d\n", rc);
1546                 }
1547         }
1548
1549         RETURN(rc);
1550 }
1551
1552 char *llap_origins[] = {
1553         [LLAP_ORIGIN_UNKNOWN] = "--",
1554         [LLAP_ORIGIN_READPAGE] = "rp",
1555         [LLAP_ORIGIN_READAHEAD] = "ra",
1556         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
1557         [LLAP_ORIGIN_WRITEPAGE] = "wp",
1558 };
1559
1560 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
1561                                              struct list_head *list)
1562 {
1563         struct ll_async_page *llap;
1564         struct list_head *pos;
1565
1566         list_for_each(pos, list) {
1567                 if (pos == &sbi->ll_pglist)
1568                         return NULL;
1569                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
1570                 if (llap->llap_page == NULL)
1571                         continue;
1572                 return llap;
1573         }
1574         LBUG();
1575         return NULL;
1576 }
1577