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