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