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