inode.c 35 KB

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