inode.c 35 KB

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