inode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * inode.c
  5. *
  6. * vfs' aops, fops, dops and iops
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/quotaops.h>
  31. #include <asm/byteorder.h>
  32. #define MLOG_MASK_PREFIX ML_INODE
  33. #include <cluster/masklog.h>
  34. #include "ocfs2.h"
  35. #include "alloc.h"
  36. #include "blockcheck.h"
  37. #include "dlmglue.h"
  38. #include "extent_map.h"
  39. #include "file.h"
  40. #include "heartbeat.h"
  41. #include "inode.h"
  42. #include "journal.h"
  43. #include "namei.h"
  44. #include "suballoc.h"
  45. #include "super.h"
  46. #include "symlink.h"
  47. #include "sysfile.h"
  48. #include "uptodate.h"
  49. #include "xattr.h"
  50. #include "buffer_head_io.h"
  51. struct ocfs2_find_inode_args
  52. {
  53. u64 fi_blkno;
  54. unsigned long fi_ino;
  55. unsigned int fi_flags;
  56. unsigned int fi_sysfile_type;
  57. };
  58. static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
  59. static int ocfs2_read_locked_inode(struct inode *inode,
  60. struct ocfs2_find_inode_args *args);
  61. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  62. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  63. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  64. struct inode *inode,
  65. struct buffer_head *fe_bh);
  66. void ocfs2_set_inode_flags(struct inode *inode)
  67. {
  68. unsigned int flags = OCFS2_I(inode)->ip_attr;
  69. inode->i_flags &= ~(S_IMMUTABLE |
  70. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  71. if (flags & OCFS2_IMMUTABLE_FL)
  72. inode->i_flags |= S_IMMUTABLE;
  73. if (flags & OCFS2_SYNC_FL)
  74. inode->i_flags |= S_SYNC;
  75. if (flags & OCFS2_APPEND_FL)
  76. inode->i_flags |= S_APPEND;
  77. if (flags & OCFS2_NOATIME_FL)
  78. inode->i_flags |= S_NOATIME;
  79. if (flags & OCFS2_DIRSYNC_FL)
  80. inode->i_flags |= S_DIRSYNC;
  81. }
  82. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  83. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  84. {
  85. unsigned int flags = oi->vfs_inode.i_flags;
  86. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  87. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  88. if (flags & S_SYNC)
  89. oi->ip_attr |= OCFS2_SYNC_FL;
  90. if (flags & S_APPEND)
  91. oi->ip_attr |= OCFS2_APPEND_FL;
  92. if (flags & S_IMMUTABLE)
  93. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  94. if (flags & S_NOATIME)
  95. oi->ip_attr |= OCFS2_NOATIME_FL;
  96. if (flags & S_DIRSYNC)
  97. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  98. }
  99. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  100. int sysfile_type)
  101. {
  102. struct inode *inode = NULL;
  103. struct super_block *sb = osb->sb;
  104. struct ocfs2_find_inode_args args;
  105. mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
  106. /* Ok. By now we've either got the offsets passed to us by the
  107. * caller, or we just pulled them off the bh. Lets do some
  108. * sanity checks to make sure they're OK. */
  109. if (blkno == 0) {
  110. inode = ERR_PTR(-EINVAL);
  111. mlog_errno(PTR_ERR(inode));
  112. goto bail;
  113. }
  114. args.fi_blkno = blkno;
  115. args.fi_flags = flags;
  116. args.fi_ino = ino_from_blkno(sb, blkno);
  117. args.fi_sysfile_type = sysfile_type;
  118. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  119. ocfs2_init_locked_inode, &args);
  120. /* inode was *not* in the inode cache. 2.6.x requires
  121. * us to do our own read_inode call and unlock it
  122. * afterwards. */
  123. if (inode && inode->i_state & I_NEW) {
  124. mlog(0, "Inode was not in inode cache, reading it.\n");
  125. ocfs2_read_locked_inode(inode, &args);
  126. unlock_new_inode(inode);
  127. }
  128. if (inode == NULL) {
  129. inode = ERR_PTR(-ENOMEM);
  130. mlog_errno(PTR_ERR(inode));
  131. goto bail;
  132. }
  133. if (is_bad_inode(inode)) {
  134. iput(inode);
  135. inode = ERR_PTR(-ESTALE);
  136. goto bail;
  137. }
  138. bail:
  139. if (!IS_ERR(inode)) {
  140. mlog(0, "returning inode with number %llu\n",
  141. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  142. mlog_exit_ptr(inode);
  143. }
  144. return inode;
  145. }
  146. /*
  147. * here's how inodes get read from disk:
  148. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  149. * found? : return the in-memory inode
  150. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  151. */
  152. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  153. {
  154. struct ocfs2_find_inode_args *args = NULL;
  155. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  156. int ret = 0;
  157. mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
  158. args = opaque;
  159. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  160. if (oi->ip_blkno != args->fi_blkno)
  161. goto bail;
  162. ret = 1;
  163. bail:
  164. mlog_exit(ret);
  165. return ret;
  166. }
  167. /*
  168. * initialize the new inode, but don't do anything that would cause
  169. * us to sleep.
  170. * return 0 on success, 1 on failure
  171. */
  172. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  173. {
  174. struct ocfs2_find_inode_args *args = opaque;
  175. mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
  176. inode->i_ino = args->fi_ino;
  177. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  178. if (args->fi_sysfile_type != 0)
  179. lockdep_set_class(&inode->i_mutex,
  180. &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
  181. mlog_exit(0);
  182. return 0;
  183. }
  184. void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  185. int create_ino)
  186. {
  187. struct super_block *sb;
  188. struct ocfs2_super *osb;
  189. int use_plocks = 1;
  190. mlog_entry("(0x%p, size:%llu)\n", inode,
  191. (unsigned long long)le64_to_cpu(fe->i_size));
  192. sb = inode->i_sb;
  193. osb = OCFS2_SB(sb);
  194. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  195. ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  196. use_plocks = 0;
  197. /*
  198. * These have all been checked by ocfs2_read_inode_block() or set
  199. * by ocfs2_mknod_locked(), so a failure is a code bug.
  200. */
  201. BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
  202. cannot create a superblock
  203. inode today. change if
  204. that is needed. */
  205. BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
  206. BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
  207. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  208. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  209. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  210. inode->i_version = 1;
  211. inode->i_generation = le32_to_cpu(fe->i_generation);
  212. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  213. inode->i_mode = le16_to_cpu(fe->i_mode);
  214. inode->i_uid = le32_to_cpu(fe->i_uid);
  215. inode->i_gid = le32_to_cpu(fe->i_gid);
  216. /* Fast symlinks will have i_size but no allocated clusters. */
  217. if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
  218. inode->i_blocks = 0;
  219. else
  220. inode->i_blocks = ocfs2_inode_sector_count(inode);
  221. inode->i_mapping->a_ops = &ocfs2_aops;
  222. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  223. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  224. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  225. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  226. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  227. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  228. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  229. mlog(ML_ERROR,
  230. "ip_blkno %llu != i_blkno %llu!\n",
  231. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  232. (unsigned long long)le64_to_cpu(fe->i_blkno));
  233. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  234. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
  235. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  236. inode->i_flags |= S_NOQUOTA;
  237. }
  238. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  239. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  240. mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
  241. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  242. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  243. } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
  244. inode->i_flags |= S_NOQUOTA;
  245. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  246. mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
  247. /* we can't actually hit this as read_inode can't
  248. * handle superblocks today ;-) */
  249. BUG();
  250. }
  251. switch (inode->i_mode & S_IFMT) {
  252. case S_IFREG:
  253. if (use_plocks)
  254. inode->i_fop = &ocfs2_fops;
  255. else
  256. inode->i_fop = &ocfs2_fops_no_plocks;
  257. inode->i_op = &ocfs2_file_iops;
  258. i_size_write(inode, le64_to_cpu(fe->i_size));
  259. break;
  260. case S_IFDIR:
  261. inode->i_op = &ocfs2_dir_iops;
  262. if (use_plocks)
  263. inode->i_fop = &ocfs2_dops;
  264. else
  265. inode->i_fop = &ocfs2_dops_no_plocks;
  266. i_size_write(inode, le64_to_cpu(fe->i_size));
  267. break;
  268. case S_IFLNK:
  269. if (ocfs2_inode_is_fast_symlink(inode))
  270. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  271. else
  272. inode->i_op = &ocfs2_symlink_inode_operations;
  273. i_size_write(inode, le64_to_cpu(fe->i_size));
  274. break;
  275. default:
  276. inode->i_op = &ocfs2_special_file_iops;
  277. init_special_inode(inode, inode->i_mode,
  278. inode->i_rdev);
  279. break;
  280. }
  281. if (create_ino) {
  282. inode->i_ino = ino_from_blkno(inode->i_sb,
  283. le64_to_cpu(fe->i_blkno));
  284. /*
  285. * If we ever want to create system files from kernel,
  286. * the generation argument to
  287. * ocfs2_inode_lock_res_init() will have to change.
  288. */
  289. BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
  290. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  291. OCFS2_LOCK_TYPE_META, 0, inode);
  292. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  293. OCFS2_LOCK_TYPE_OPEN, 0, inode);
  294. }
  295. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  296. OCFS2_LOCK_TYPE_RW, inode->i_generation,
  297. inode);
  298. ocfs2_set_inode_flags(inode);
  299. mlog_exit_void();
  300. }
  301. static int ocfs2_read_locked_inode(struct inode *inode,
  302. struct ocfs2_find_inode_args *args)
  303. {
  304. struct super_block *sb;
  305. struct ocfs2_super *osb;
  306. struct ocfs2_dinode *fe;
  307. struct buffer_head *bh = NULL;
  308. int status, can_lock;
  309. u32 generation = 0;
  310. mlog_entry("(0x%p, 0x%p)\n", inode, args);
  311. status = -EINVAL;
  312. if (inode == NULL || inode->i_sb == NULL) {
  313. mlog(ML_ERROR, "bad inode\n");
  314. return status;
  315. }
  316. sb = inode->i_sb;
  317. osb = OCFS2_SB(sb);
  318. if (!args) {
  319. mlog(ML_ERROR, "bad inode args\n");
  320. make_bad_inode(inode);
  321. return status;
  322. }
  323. /*
  324. * To improve performance of cold-cache inode stats, we take
  325. * the cluster lock here if possible.
  326. *
  327. * Generally, OCFS2 never trusts the contents of an inode
  328. * unless it's holding a cluster lock, so taking it here isn't
  329. * a correctness issue as much as it is a performance
  330. * improvement.
  331. *
  332. * There are three times when taking the lock is not a good idea:
  333. *
  334. * 1) During startup, before we have initialized the DLM.
  335. *
  336. * 2) If we are reading certain system files which never get
  337. * cluster locks (local alloc, truncate log).
  338. *
  339. * 3) If the process doing the iget() is responsible for
  340. * orphan dir recovery. We're holding the orphan dir lock and
  341. * can get into a deadlock with another process on another
  342. * node in ->delete_inode().
  343. *
  344. * #1 and #2 can be simply solved by never taking the lock
  345. * here for system files (which are the only type we read
  346. * during mount). It's a heavier approach, but our main
  347. * concern is user-accesible files anyway.
  348. *
  349. * #3 works itself out because we'll eventually take the
  350. * cluster lock before trusting anything anyway.
  351. */
  352. can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  353. && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
  354. && !ocfs2_mount_local(osb);
  355. /*
  356. * To maintain backwards compatibility with older versions of
  357. * ocfs2-tools, we still store the generation value for system
  358. * files. The only ones that actually matter to userspace are
  359. * the journals, but it's easier and inexpensive to just flag
  360. * all system files similarly.
  361. */
  362. if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  363. generation = osb->fs_generation;
  364. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  365. OCFS2_LOCK_TYPE_META,
  366. generation, inode);
  367. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  368. OCFS2_LOCK_TYPE_OPEN,
  369. 0, inode);
  370. if (can_lock) {
  371. status = ocfs2_open_lock(inode);
  372. if (status) {
  373. make_bad_inode(inode);
  374. mlog_errno(status);
  375. return status;
  376. }
  377. status = ocfs2_inode_lock(inode, NULL, 0);
  378. if (status) {
  379. make_bad_inode(inode);
  380. mlog_errno(status);
  381. return status;
  382. }
  383. }
  384. if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  385. status = ocfs2_try_open_lock(inode, 0);
  386. if (status) {
  387. make_bad_inode(inode);
  388. return status;
  389. }
  390. }
  391. if (can_lock) {
  392. status = ocfs2_read_inode_block_full(inode, &bh,
  393. OCFS2_BH_IGNORE_CACHE);
  394. } else {
  395. status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
  396. if (!status)
  397. status = ocfs2_validate_inode_block(osb->sb, bh);
  398. }
  399. if (status < 0) {
  400. mlog_errno(status);
  401. goto bail;
  402. }
  403. status = -EINVAL;
  404. fe = (struct ocfs2_dinode *) bh->b_data;
  405. /*
  406. * This is a code bug. Right now the caller needs to
  407. * understand whether it is asking for a system file inode or
  408. * not so the proper lock names can be built.
  409. */
  410. mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  411. !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  412. "Inode %llu: system file state is ambigous\n",
  413. (unsigned long long)args->fi_blkno);
  414. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  415. S_ISBLK(le16_to_cpu(fe->i_mode)))
  416. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  417. ocfs2_populate_inode(inode, fe, 0);
  418. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  419. status = 0;
  420. bail:
  421. if (can_lock)
  422. ocfs2_inode_unlock(inode, 0);
  423. if (status < 0)
  424. make_bad_inode(inode);
  425. if (args && bh)
  426. brelse(bh);
  427. mlog_exit(status);
  428. return status;
  429. }
  430. void ocfs2_sync_blockdev(struct super_block *sb)
  431. {
  432. sync_blockdev(sb->s_bdev);
  433. }
  434. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  435. struct inode *inode,
  436. struct buffer_head *fe_bh)
  437. {
  438. int status = 0;
  439. struct ocfs2_truncate_context *tc = NULL;
  440. struct ocfs2_dinode *fe;
  441. handle_t *handle = NULL;
  442. mlog_entry_void();
  443. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  444. /*
  445. * This check will also skip truncate of inodes with inline
  446. * data and fast symlinks.
  447. */
  448. if (fe->i_clusters) {
  449. if (ocfs2_should_order_data(inode))
  450. ocfs2_begin_ordered_truncate(inode, 0);
  451. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  452. if (IS_ERR(handle)) {
  453. status = PTR_ERR(handle);
  454. mlog_errno(status);
  455. goto out;
  456. }
  457. status = ocfs2_journal_access_di(handle, inode, fe_bh,
  458. OCFS2_JOURNAL_ACCESS_WRITE);
  459. if (status < 0) {
  460. mlog_errno(status);
  461. goto out;
  462. }
  463. i_size_write(inode, 0);
  464. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  465. if (status < 0) {
  466. mlog_errno(status);
  467. goto out;
  468. }
  469. ocfs2_commit_trans(osb, handle);
  470. handle = NULL;
  471. status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
  472. if (status < 0) {
  473. mlog_errno(status);
  474. goto out;
  475. }
  476. status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
  477. if (status < 0) {
  478. mlog_errno(status);
  479. goto out;
  480. }
  481. }
  482. out:
  483. if (handle)
  484. ocfs2_commit_trans(osb, handle);
  485. mlog_exit(status);
  486. return status;
  487. }
  488. static int ocfs2_remove_inode(struct inode *inode,
  489. struct buffer_head *di_bh,
  490. struct inode *orphan_dir_inode,
  491. struct buffer_head *orphan_dir_bh)
  492. {
  493. int status;
  494. struct inode *inode_alloc_inode = NULL;
  495. struct buffer_head *inode_alloc_bh = NULL;
  496. handle_t *handle;
  497. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  498. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  499. inode_alloc_inode =
  500. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  501. le16_to_cpu(di->i_suballoc_slot));
  502. if (!inode_alloc_inode) {
  503. status = -EEXIST;
  504. mlog_errno(status);
  505. goto bail;
  506. }
  507. mutex_lock(&inode_alloc_inode->i_mutex);
  508. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  509. if (status < 0) {
  510. mutex_unlock(&inode_alloc_inode->i_mutex);
  511. mlog_errno(status);
  512. goto bail;
  513. }
  514. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
  515. ocfs2_quota_trans_credits(inode->i_sb));
  516. if (IS_ERR(handle)) {
  517. status = PTR_ERR(handle);
  518. mlog_errno(status);
  519. goto bail_unlock;
  520. }
  521. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  522. orphan_dir_bh);
  523. if (status < 0) {
  524. mlog_errno(status);
  525. goto bail_commit;
  526. }
  527. /* set the inodes dtime */
  528. status = ocfs2_journal_access_di(handle, inode, di_bh,
  529. OCFS2_JOURNAL_ACCESS_WRITE);
  530. if (status < 0) {
  531. mlog_errno(status);
  532. goto bail_commit;
  533. }
  534. di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
  535. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  536. status = ocfs2_journal_dirty(handle, di_bh);
  537. if (status < 0) {
  538. mlog_errno(status);
  539. goto bail_commit;
  540. }
  541. ocfs2_remove_from_cache(inode, di_bh);
  542. vfs_dq_free_inode(inode);
  543. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  544. inode_alloc_bh, di);
  545. if (status < 0)
  546. mlog_errno(status);
  547. bail_commit:
  548. ocfs2_commit_trans(osb, handle);
  549. bail_unlock:
  550. ocfs2_inode_unlock(inode_alloc_inode, 1);
  551. mutex_unlock(&inode_alloc_inode->i_mutex);
  552. brelse(inode_alloc_bh);
  553. bail:
  554. iput(inode_alloc_inode);
  555. return status;
  556. }
  557. /*
  558. * Serialize with orphan dir recovery. If the process doing
  559. * recovery on this orphan dir does an iget() with the dir
  560. * i_mutex held, we'll deadlock here. Instead we detect this
  561. * and exit early - recovery will wipe this inode for us.
  562. */
  563. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  564. int slot)
  565. {
  566. int ret = 0;
  567. spin_lock(&osb->osb_lock);
  568. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  569. mlog(0, "Recovery is happening on orphan dir %d, will skip "
  570. "this inode\n", slot);
  571. ret = -EDEADLK;
  572. goto out;
  573. }
  574. /* This signals to the orphan recovery process that it should
  575. * wait for us to handle the wipe. */
  576. osb->osb_orphan_wipes[slot]++;
  577. out:
  578. spin_unlock(&osb->osb_lock);
  579. return ret;
  580. }
  581. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  582. int slot)
  583. {
  584. spin_lock(&osb->osb_lock);
  585. osb->osb_orphan_wipes[slot]--;
  586. spin_unlock(&osb->osb_lock);
  587. wake_up(&osb->osb_wipe_event);
  588. }
  589. static int ocfs2_wipe_inode(struct inode *inode,
  590. struct buffer_head *di_bh)
  591. {
  592. int status, orphaned_slot;
  593. struct inode *orphan_dir_inode = NULL;
  594. struct buffer_head *orphan_dir_bh = NULL;
  595. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  596. struct ocfs2_dinode *di;
  597. di = (struct ocfs2_dinode *) di_bh->b_data;
  598. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  599. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  600. if (status)
  601. return status;
  602. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  603. ORPHAN_DIR_SYSTEM_INODE,
  604. orphaned_slot);
  605. if (!orphan_dir_inode) {
  606. status = -EEXIST;
  607. mlog_errno(status);
  608. goto bail;
  609. }
  610. /* Lock the orphan dir. The lock will be held for the entire
  611. * delete_inode operation. We do this now to avoid races with
  612. * recovery completion on other nodes. */
  613. mutex_lock(&orphan_dir_inode->i_mutex);
  614. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  615. if (status < 0) {
  616. mutex_unlock(&orphan_dir_inode->i_mutex);
  617. mlog_errno(status);
  618. goto bail;
  619. }
  620. /* we do this while holding the orphan dir lock because we
  621. * don't want recovery being run from another node to try an
  622. * inode delete underneath us -- this will result in two nodes
  623. * truncating the same file! */
  624. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  625. if (status < 0) {
  626. mlog_errno(status);
  627. goto bail_unlock_dir;
  628. }
  629. /*Free extended attribute resources associated with this inode.*/
  630. status = ocfs2_xattr_remove(inode, di_bh);
  631. if (status < 0) {
  632. mlog_errno(status);
  633. goto bail_unlock_dir;
  634. }
  635. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  636. orphan_dir_bh);
  637. if (status < 0)
  638. mlog_errno(status);
  639. bail_unlock_dir:
  640. ocfs2_inode_unlock(orphan_dir_inode, 1);
  641. mutex_unlock(&orphan_dir_inode->i_mutex);
  642. brelse(orphan_dir_bh);
  643. bail:
  644. iput(orphan_dir_inode);
  645. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  646. return status;
  647. }
  648. /* There is a series of simple checks that should be done before a
  649. * trylock is even considered. Encapsulate those in this function. */
  650. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  651. {
  652. int ret = 0;
  653. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  654. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  655. /* We shouldn't be getting here for the root directory
  656. * inode.. */
  657. if (inode == osb->root_inode) {
  658. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  659. goto bail;
  660. }
  661. /* If we're coming from downconvert_thread we can't go into our own
  662. * voting [hello, deadlock city!], so unforuntately we just
  663. * have to skip deleting this guy. That's OK though because
  664. * the node who's doing the actual deleting should handle it
  665. * anyway. */
  666. if (current == osb->dc_task) {
  667. mlog(0, "Skipping delete of %lu because we're currently "
  668. "in downconvert\n", inode->i_ino);
  669. goto bail;
  670. }
  671. spin_lock(&oi->ip_lock);
  672. /* OCFS2 *never* deletes system files. This should technically
  673. * never get here as system file inodes should always have a
  674. * positive link count. */
  675. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  676. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  677. (unsigned long long)oi->ip_blkno);
  678. goto bail_unlock;
  679. }
  680. /* If we have allowd wipe of this inode for another node, it
  681. * will be marked here so we can safely skip it. Recovery will
  682. * cleanup any inodes we might inadvertantly skip here. */
  683. if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
  684. mlog(0, "Skipping delete of %lu because another node "
  685. "has done this for us.\n", inode->i_ino);
  686. goto bail_unlock;
  687. }
  688. ret = 1;
  689. bail_unlock:
  690. spin_unlock(&oi->ip_lock);
  691. bail:
  692. return ret;
  693. }
  694. /* Query the cluster to determine whether we should wipe an inode from
  695. * disk or not.
  696. *
  697. * Requires the inode to have the cluster lock. */
  698. static int ocfs2_query_inode_wipe(struct inode *inode,
  699. struct buffer_head *di_bh,
  700. int *wipe)
  701. {
  702. int status = 0;
  703. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  704. struct ocfs2_dinode *di;
  705. *wipe = 0;
  706. /* While we were waiting for the cluster lock in
  707. * ocfs2_delete_inode, another node might have asked to delete
  708. * the inode. Recheck our flags to catch this. */
  709. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  710. mlog(0, "Skipping delete of %llu because flags changed\n",
  711. (unsigned long long)oi->ip_blkno);
  712. goto bail;
  713. }
  714. /* Now that we have an up to date inode, we can double check
  715. * the link count. */
  716. if (inode->i_nlink) {
  717. mlog(0, "Skipping delete of %llu because nlink = %u\n",
  718. (unsigned long long)oi->ip_blkno, inode->i_nlink);
  719. goto bail;
  720. }
  721. /* Do some basic inode verification... */
  722. di = (struct ocfs2_dinode *) di_bh->b_data;
  723. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
  724. /* for lack of a better error? */
  725. status = -EEXIST;
  726. mlog(ML_ERROR,
  727. "Inode %llu (on-disk %llu) not orphaned! "
  728. "Disk flags 0x%x, inode flags 0x%x\n",
  729. (unsigned long long)oi->ip_blkno,
  730. (unsigned long long)le64_to_cpu(di->i_blkno),
  731. le32_to_cpu(di->i_flags), oi->ip_flags);
  732. goto bail;
  733. }
  734. /* has someone already deleted us?! baaad... */
  735. if (di->i_dtime) {
  736. status = -EEXIST;
  737. mlog_errno(status);
  738. goto bail;
  739. }
  740. /*
  741. * This is how ocfs2 determines whether an inode is still live
  742. * within the cluster. Every node takes a shared read lock on
  743. * the inode open lock in ocfs2_read_locked_inode(). When we
  744. * get to ->delete_inode(), each node tries to convert it's
  745. * lock to an exclusive. Trylocks are serialized by the inode
  746. * meta data lock. If the upconvert suceeds, we know the inode
  747. * is no longer live and can be deleted.
  748. *
  749. * Though we call this with the meta data lock held, the
  750. * trylock keeps us from ABBA deadlock.
  751. */
  752. status = ocfs2_try_open_lock(inode, 1);
  753. if (status == -EAGAIN) {
  754. status = 0;
  755. mlog(0, "Skipping delete of %llu because it is in use on "
  756. "other nodes\n", (unsigned long long)oi->ip_blkno);
  757. goto bail;
  758. }
  759. if (status < 0) {
  760. mlog_errno(status);
  761. goto bail;
  762. }
  763. *wipe = 1;
  764. mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
  765. (unsigned long long)oi->ip_blkno,
  766. le16_to_cpu(di->i_orphaned_slot));
  767. bail:
  768. return status;
  769. }
  770. /* Support function for ocfs2_delete_inode. Will help us keep the
  771. * inode data in a consistent state for clear_inode. Always truncates
  772. * pages, optionally sync's them first. */
  773. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  774. int sync_data)
  775. {
  776. mlog(0, "Cleanup inode %llu, sync = %d\n",
  777. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  778. if (sync_data)
  779. write_inode_now(inode, 1);
  780. truncate_inode_pages(&inode->i_data, 0);
  781. }
  782. void ocfs2_delete_inode(struct inode *inode)
  783. {
  784. int wipe, status;
  785. sigset_t blocked, oldset;
  786. struct buffer_head *di_bh = NULL;
  787. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  788. /* When we fail in read_inode() we mark inode as bad. The second test
  789. * catches the case when inode allocation fails before allocating
  790. * a block for inode. */
  791. if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) {
  792. mlog(0, "Skipping delete of bad inode\n");
  793. goto bail;
  794. }
  795. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  796. /* It's probably not necessary to truncate_inode_pages
  797. * here but we do it for safety anyway (it will most
  798. * likely be a no-op anyway) */
  799. ocfs2_cleanup_delete_inode(inode, 0);
  800. goto bail;
  801. }
  802. /* We want to block signals in delete_inode as the lock and
  803. * messaging paths may return us -ERESTARTSYS. Which would
  804. * cause us to exit early, resulting in inodes being orphaned
  805. * forever. */
  806. sigfillset(&blocked);
  807. status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
  808. if (status < 0) {
  809. mlog_errno(status);
  810. ocfs2_cleanup_delete_inode(inode, 1);
  811. goto bail;
  812. }
  813. /* Lock down the inode. This gives us an up to date view of
  814. * it's metadata (for verification), and allows us to
  815. * serialize delete_inode on multiple nodes.
  816. *
  817. * Even though we might be doing a truncate, we don't take the
  818. * allocation lock here as it won't be needed - nobody will
  819. * have the file open.
  820. */
  821. status = ocfs2_inode_lock(inode, &di_bh, 1);
  822. if (status < 0) {
  823. if (status != -ENOENT)
  824. mlog_errno(status);
  825. ocfs2_cleanup_delete_inode(inode, 0);
  826. goto bail_unblock;
  827. }
  828. /* Query the cluster. This will be the final decision made
  829. * before we go ahead and wipe the inode. */
  830. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  831. if (!wipe || status < 0) {
  832. /* Error and remote inode busy both mean we won't be
  833. * removing the inode, so they take almost the same
  834. * path. */
  835. if (status < 0)
  836. mlog_errno(status);
  837. /* Someone in the cluster has disallowed a wipe of
  838. * this inode, or it was never completely
  839. * orphaned. Write out the pages and exit now. */
  840. ocfs2_cleanup_delete_inode(inode, 1);
  841. goto bail_unlock_inode;
  842. }
  843. ocfs2_cleanup_delete_inode(inode, 0);
  844. status = ocfs2_wipe_inode(inode, di_bh);
  845. if (status < 0) {
  846. if (status != -EDEADLK)
  847. mlog_errno(status);
  848. goto bail_unlock_inode;
  849. }
  850. /*
  851. * Mark the inode as successfully deleted.
  852. *
  853. * This is important for ocfs2_clear_inode() as it will check
  854. * this flag and skip any checkpointing work
  855. *
  856. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  857. * the LVB for other nodes.
  858. */
  859. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  860. bail_unlock_inode:
  861. ocfs2_inode_unlock(inode, 1);
  862. brelse(di_bh);
  863. bail_unblock:
  864. status = sigprocmask(SIG_SETMASK, &oldset, NULL);
  865. if (status < 0)
  866. mlog_errno(status);
  867. bail:
  868. clear_inode(inode);
  869. mlog_exit_void();
  870. }
  871. void ocfs2_clear_inode(struct inode *inode)
  872. {
  873. int status;
  874. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  875. mlog_entry_void();
  876. if (!inode)
  877. goto bail;
  878. mlog(0, "Clearing inode: %llu, nlink = %u\n",
  879. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
  880. mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  881. "Inode=%lu\n", inode->i_ino);
  882. /* To preven remote deletes we hold open lock before, now it
  883. * is time to unlock PR and EX open locks. */
  884. ocfs2_open_unlock(inode);
  885. /* Do these before all the other work so that we don't bounce
  886. * the downconvert thread while waiting to destroy the locks. */
  887. ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
  888. ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
  889. ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
  890. /* We very well may get a clear_inode before all an inodes
  891. * metadata has hit disk. Of course, we can't drop any cluster
  892. * locks until the journal has finished with it. The only
  893. * exception here are successfully wiped inodes - their
  894. * metadata can now be considered to be part of the system
  895. * inodes from which it came. */
  896. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  897. ocfs2_checkpoint_inode(inode);
  898. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  899. "Clear inode of %llu, inode has io markers\n",
  900. (unsigned long long)oi->ip_blkno);
  901. ocfs2_extent_map_trunc(inode, 0);
  902. status = ocfs2_drop_inode_locks(inode);
  903. if (status < 0)
  904. mlog_errno(status);
  905. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  906. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  907. ocfs2_lock_res_free(&oi->ip_open_lockres);
  908. ocfs2_metadata_cache_purge(inode);
  909. mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
  910. "Clear inode of %llu, inode has %u cache items\n",
  911. (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
  912. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  913. "Clear inode of %llu, inode has a bad flag\n",
  914. (unsigned long long)oi->ip_blkno);
  915. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  916. "Clear inode of %llu, inode is locked\n",
  917. (unsigned long long)oi->ip_blkno);
  918. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  919. "Clear inode of %llu, io_mutex is locked\n",
  920. (unsigned long long)oi->ip_blkno);
  921. mutex_unlock(&oi->ip_io_mutex);
  922. /*
  923. * down_trylock() returns 0, down_write_trylock() returns 1
  924. * kernel 1, world 0
  925. */
  926. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  927. "Clear inode of %llu, alloc_sem is locked\n",
  928. (unsigned long long)oi->ip_blkno);
  929. up_write(&oi->ip_alloc_sem);
  930. mlog_bug_on_msg(oi->ip_open_count,
  931. "Clear inode of %llu has open count %d\n",
  932. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  933. /* Clear all other flags. */
  934. oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
  935. oi->ip_created_trans = 0;
  936. oi->ip_last_trans = 0;
  937. oi->ip_dir_start_lookup = 0;
  938. oi->ip_blkno = 0ULL;
  939. /*
  940. * ip_jinode is used to track txns against this inode. We ensure that
  941. * the journal is flushed before journal shutdown. Thus it is safe to
  942. * have inodes get cleaned up after journal shutdown.
  943. */
  944. jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
  945. &oi->ip_jinode);
  946. bail:
  947. mlog_exit_void();
  948. }
  949. /* Called under inode_lock, with no more references on the
  950. * struct inode, so it's safe here to check the flags field
  951. * and to manipulate i_nlink without any other locks. */
  952. void ocfs2_drop_inode(struct inode *inode)
  953. {
  954. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  955. mlog_entry_void();
  956. mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
  957. (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
  958. if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
  959. generic_delete_inode(inode);
  960. else
  961. generic_drop_inode(inode);
  962. mlog_exit_void();
  963. }
  964. /*
  965. * This is called from our getattr.
  966. */
  967. int ocfs2_inode_revalidate(struct dentry *dentry)
  968. {
  969. struct inode *inode = dentry->d_inode;
  970. int status = 0;
  971. mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
  972. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
  973. if (!inode) {
  974. mlog(0, "eep, no inode!\n");
  975. status = -ENOENT;
  976. goto bail;
  977. }
  978. spin_lock(&OCFS2_I(inode)->ip_lock);
  979. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  980. spin_unlock(&OCFS2_I(inode)->ip_lock);
  981. mlog(0, "inode deleted!\n");
  982. status = -ENOENT;
  983. goto bail;
  984. }
  985. spin_unlock(&OCFS2_I(inode)->ip_lock);
  986. /* Let ocfs2_inode_lock do the work of updating our struct
  987. * inode for us. */
  988. status = ocfs2_inode_lock(inode, NULL, 0);
  989. if (status < 0) {
  990. if (status != -ENOENT)
  991. mlog_errno(status);
  992. goto bail;
  993. }
  994. ocfs2_inode_unlock(inode, 0);
  995. bail:
  996. mlog_exit(status);
  997. return status;
  998. }
  999. /*
  1000. * Updates a disk inode from a
  1001. * struct inode.
  1002. * Only takes ip_lock.
  1003. */
  1004. int ocfs2_mark_inode_dirty(handle_t *handle,
  1005. struct inode *inode,
  1006. struct buffer_head *bh)
  1007. {
  1008. int status;
  1009. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1010. mlog_entry("(inode %llu)\n",
  1011. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1012. status = ocfs2_journal_access_di(handle, inode, bh,
  1013. OCFS2_JOURNAL_ACCESS_WRITE);
  1014. if (status < 0) {
  1015. mlog_errno(status);
  1016. goto leave;
  1017. }
  1018. spin_lock(&OCFS2_I(inode)->ip_lock);
  1019. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1020. ocfs2_get_inode_flags(OCFS2_I(inode));
  1021. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1022. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1023. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1024. fe->i_size = cpu_to_le64(i_size_read(inode));
  1025. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  1026. fe->i_uid = cpu_to_le32(inode->i_uid);
  1027. fe->i_gid = cpu_to_le32(inode->i_gid);
  1028. fe->i_mode = cpu_to_le16(inode->i_mode);
  1029. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1030. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1031. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1032. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1033. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1034. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1035. status = ocfs2_journal_dirty(handle, bh);
  1036. if (status < 0)
  1037. mlog_errno(status);
  1038. status = 0;
  1039. leave:
  1040. mlog_exit(status);
  1041. return status;
  1042. }
  1043. /*
  1044. *
  1045. * Updates a struct inode from a disk inode.
  1046. * does no i/o, only takes ip_lock.
  1047. */
  1048. void ocfs2_refresh_inode(struct inode *inode,
  1049. struct ocfs2_dinode *fe)
  1050. {
  1051. spin_lock(&OCFS2_I(inode)->ip_lock);
  1052. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1053. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1054. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1055. ocfs2_set_inode_flags(inode);
  1056. i_size_write(inode, le64_to_cpu(fe->i_size));
  1057. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  1058. inode->i_uid = le32_to_cpu(fe->i_uid);
  1059. inode->i_gid = le32_to_cpu(fe->i_gid);
  1060. inode->i_mode = le16_to_cpu(fe->i_mode);
  1061. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1062. inode->i_blocks = 0;
  1063. else
  1064. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1065. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1066. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1067. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1068. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1069. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1070. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1071. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1072. }
  1073. int ocfs2_validate_inode_block(struct super_block *sb,
  1074. struct buffer_head *bh)
  1075. {
  1076. int rc;
  1077. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1078. mlog(0, "Validating dinode %llu\n",
  1079. (unsigned long long)bh->b_blocknr);
  1080. BUG_ON(!buffer_uptodate(bh));
  1081. /*
  1082. * If the ecc fails, we return the error but otherwise
  1083. * leave the filesystem running. We know any error is
  1084. * local to this block.
  1085. */
  1086. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1087. if (rc) {
  1088. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  1089. (unsigned long long)bh->b_blocknr);
  1090. goto bail;
  1091. }
  1092. /*
  1093. * Errors after here are fatal.
  1094. */
  1095. rc = -EINVAL;
  1096. if (!OCFS2_IS_VALID_DINODE(di)) {
  1097. ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
  1098. (unsigned long long)bh->b_blocknr, 7,
  1099. di->i_signature);
  1100. goto bail;
  1101. }
  1102. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1103. ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
  1104. (unsigned long long)bh->b_blocknr,
  1105. (unsigned long long)le64_to_cpu(di->i_blkno));
  1106. goto bail;
  1107. }
  1108. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1109. ocfs2_error(sb,
  1110. "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
  1111. (unsigned long long)bh->b_blocknr);
  1112. goto bail;
  1113. }
  1114. if (le32_to_cpu(di->i_fs_generation) !=
  1115. OCFS2_SB(sb)->fs_generation) {
  1116. ocfs2_error(sb,
  1117. "Invalid dinode #%llu: fs_generation is %u\n",
  1118. (unsigned long long)bh->b_blocknr,
  1119. le32_to_cpu(di->i_fs_generation));
  1120. goto bail;
  1121. }
  1122. rc = 0;
  1123. bail:
  1124. return rc;
  1125. }
  1126. int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  1127. int flags)
  1128. {
  1129. int rc;
  1130. struct buffer_head *tmp = *bh;
  1131. rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp,
  1132. flags, ocfs2_validate_inode_block);
  1133. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1134. if (!rc && !*bh)
  1135. *bh = tmp;
  1136. return rc;
  1137. }
  1138. int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  1139. {
  1140. return ocfs2_read_inode_block_full(inode, bh, 0);
  1141. }