inode.c 37 KB

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