inode.c 40 KB

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