inode.c 40 KB

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