inode.c 35 KB

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