inode.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * inode.c
  4. *
  5. * vfs' aops, fops, dops and iops
  6. *
  7. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/types.h>
  11. #include <linux/highmem.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/iversion.h>
  15. #include <asm/byteorder.h>
  16. #include <cluster/masklog.h>
  17. #include "ocfs2.h"
  18. #include "alloc.h"
  19. #include "dir.h"
  20. #include "blockcheck.h"
  21. #include "dlmglue.h"
  22. #include "extent_map.h"
  23. #include "file.h"
  24. #include "heartbeat.h"
  25. #include "inode.h"
  26. #include "journal.h"
  27. #include "namei.h"
  28. #include "suballoc.h"
  29. #include "super.h"
  30. #include "symlink.h"
  31. #include "sysfile.h"
  32. #include "uptodate.h"
  33. #include "xattr.h"
  34. #include "refcounttree.h"
  35. #include "ocfs2_trace.h"
  36. #include "filecheck.h"
  37. #include "buffer_head_io.h"
  38. struct ocfs2_find_inode_args
  39. {
  40. u64 fi_blkno;
  41. unsigned long fi_ino;
  42. unsigned int fi_flags;
  43. unsigned int fi_sysfile_type;
  44. };
  45. static int ocfs2_read_locked_inode(struct inode *inode,
  46. struct ocfs2_find_inode_args *args);
  47. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  48. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  49. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  50. struct inode *inode,
  51. struct buffer_head *fe_bh);
  52. static int ocfs2_filecheck_read_inode_block_full(struct inode *inode,
  53. struct buffer_head **bh,
  54. int flags, int type);
  55. static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
  56. struct buffer_head *bh);
  57. static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
  58. struct buffer_head *bh);
  59. void ocfs2_set_inode_flags(struct inode *inode)
  60. {
  61. unsigned int flags = OCFS2_I(inode)->ip_attr;
  62. inode->i_flags &= ~(S_IMMUTABLE |
  63. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  64. if (flags & OCFS2_IMMUTABLE_FL)
  65. inode->i_flags |= S_IMMUTABLE;
  66. if (flags & OCFS2_SYNC_FL)
  67. inode->i_flags |= S_SYNC;
  68. if (flags & OCFS2_APPEND_FL)
  69. inode->i_flags |= S_APPEND;
  70. if (flags & OCFS2_NOATIME_FL)
  71. inode->i_flags |= S_NOATIME;
  72. if (flags & OCFS2_DIRSYNC_FL)
  73. inode->i_flags |= S_DIRSYNC;
  74. }
  75. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  76. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  77. {
  78. unsigned int flags = oi->vfs_inode.i_flags;
  79. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  80. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  81. if (flags & S_SYNC)
  82. oi->ip_attr |= OCFS2_SYNC_FL;
  83. if (flags & S_APPEND)
  84. oi->ip_attr |= OCFS2_APPEND_FL;
  85. if (flags & S_IMMUTABLE)
  86. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  87. if (flags & S_NOATIME)
  88. oi->ip_attr |= OCFS2_NOATIME_FL;
  89. if (flags & S_DIRSYNC)
  90. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  91. }
  92. struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
  93. {
  94. struct ocfs2_find_inode_args args;
  95. args.fi_blkno = blkno;
  96. args.fi_flags = 0;
  97. args.fi_ino = ino_from_blkno(sb, blkno);
  98. args.fi_sysfile_type = 0;
  99. return ilookup5(sb, blkno, ocfs2_find_actor, &args);
  100. }
  101. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  102. int sysfile_type)
  103. {
  104. int rc = -ESTALE;
  105. struct inode *inode = NULL;
  106. struct super_block *sb = osb->sb;
  107. struct ocfs2_find_inode_args args;
  108. journal_t *journal = osb->journal->j_journal;
  109. trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
  110. sysfile_type);
  111. /* Ok. By now we've either got the offsets passed to us by the
  112. * caller, or we just pulled them off the bh. Lets do some
  113. * sanity checks to make sure they're OK. */
  114. if (blkno == 0) {
  115. inode = ERR_PTR(-EINVAL);
  116. mlog_errno(PTR_ERR(inode));
  117. goto bail;
  118. }
  119. args.fi_blkno = blkno;
  120. args.fi_flags = flags;
  121. args.fi_ino = ino_from_blkno(sb, blkno);
  122. args.fi_sysfile_type = sysfile_type;
  123. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  124. ocfs2_init_locked_inode, &args);
  125. /* inode was *not* in the inode cache. 2.6.x requires
  126. * us to do our own read_inode call and unlock it
  127. * afterwards. */
  128. if (inode == NULL) {
  129. inode = ERR_PTR(-ENOMEM);
  130. mlog_errno(PTR_ERR(inode));
  131. goto bail;
  132. }
  133. trace_ocfs2_iget5_locked(inode_state_read_once(inode));
  134. if (inode_state_read_once(inode) & I_NEW) {
  135. rc = ocfs2_read_locked_inode(inode, &args);
  136. unlock_new_inode(inode);
  137. }
  138. if (is_bad_inode(inode)) {
  139. iput(inode);
  140. inode = ERR_PTR(rc);
  141. goto bail;
  142. }
  143. /*
  144. * Set transaction id's of transactions that have to be committed
  145. * to finish f[data]sync. We set them to currently running transaction
  146. * as we cannot be sure that the inode or some of its metadata isn't
  147. * part of the transaction - the inode could have been reclaimed and
  148. * now it is reread from disk.
  149. */
  150. if (journal) {
  151. transaction_t *transaction;
  152. tid_t tid;
  153. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  154. read_lock(&journal->j_state_lock);
  155. if (journal->j_running_transaction)
  156. transaction = journal->j_running_transaction;
  157. else
  158. transaction = journal->j_committing_transaction;
  159. if (transaction)
  160. tid = transaction->t_tid;
  161. else
  162. tid = journal->j_commit_sequence;
  163. read_unlock(&journal->j_state_lock);
  164. oi->i_sync_tid = tid;
  165. oi->i_datasync_tid = tid;
  166. }
  167. bail:
  168. if (!IS_ERR(inode)) {
  169. trace_ocfs2_iget_end(inode,
  170. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  171. }
  172. return inode;
  173. }
  174. static int ocfs2_dinode_has_extents(struct ocfs2_dinode *di)
  175. {
  176. /* inodes flagged with other stuff in id2 */
  177. if (le32_to_cpu(di->i_flags) &
  178. (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL | OCFS2_CHAIN_FL |
  179. OCFS2_DEALLOC_FL))
  180. return 0;
  181. /* i_flags doesn't indicate when id2 is a fast symlink */
  182. if (S_ISLNK(le16_to_cpu(di->i_mode)) && le64_to_cpu(di->i_size) &&
  183. !le32_to_cpu(di->i_clusters))
  184. return 0;
  185. if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)
  186. return 0;
  187. return 1;
  188. }
  189. /*
  190. * here's how inodes get read from disk:
  191. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  192. * found? : return the in-memory inode
  193. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  194. */
  195. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  196. {
  197. struct ocfs2_find_inode_args *args = NULL;
  198. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  199. int ret = 0;
  200. args = opaque;
  201. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  202. trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
  203. if (oi->ip_blkno != args->fi_blkno)
  204. goto bail;
  205. ret = 1;
  206. bail:
  207. return ret;
  208. }
  209. /*
  210. * initialize the new inode, but don't do anything that would cause
  211. * us to sleep.
  212. * return 0 on success, 1 on failure
  213. */
  214. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  215. {
  216. struct ocfs2_find_inode_args *args = opaque;
  217. #ifdef CONFIG_LOCKDEP
  218. static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
  219. static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
  220. ocfs2_file_ip_alloc_sem_key;
  221. #endif
  222. inode->i_ino = args->fi_ino;
  223. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  224. #ifdef CONFIG_LOCKDEP
  225. switch (args->fi_sysfile_type) {
  226. case BAD_BLOCK_SYSTEM_INODE:
  227. break;
  228. case GLOBAL_INODE_ALLOC_SYSTEM_INODE:
  229. lockdep_set_class(&inode->i_rwsem,
  230. &ocfs2_sysfile_lock_key[GLOBAL_INODE_ALLOC_SYSTEM_INODE]);
  231. break;
  232. case SLOT_MAP_SYSTEM_INODE:
  233. lockdep_set_class(&inode->i_rwsem,
  234. &ocfs2_sysfile_lock_key[SLOT_MAP_SYSTEM_INODE]);
  235. break;
  236. case HEARTBEAT_SYSTEM_INODE:
  237. lockdep_set_class(&inode->i_rwsem,
  238. &ocfs2_sysfile_lock_key[HEARTBEAT_SYSTEM_INODE]);
  239. break;
  240. case GLOBAL_BITMAP_SYSTEM_INODE:
  241. lockdep_set_class(&inode->i_rwsem,
  242. &ocfs2_sysfile_lock_key[GLOBAL_BITMAP_SYSTEM_INODE]);
  243. break;
  244. case USER_QUOTA_SYSTEM_INODE:
  245. lockdep_set_class(&inode->i_rwsem,
  246. &ocfs2_sysfile_lock_key[USER_QUOTA_SYSTEM_INODE]);
  247. break;
  248. case GROUP_QUOTA_SYSTEM_INODE:
  249. lockdep_set_class(&inode->i_rwsem,
  250. &ocfs2_sysfile_lock_key[GROUP_QUOTA_SYSTEM_INODE]);
  251. break;
  252. case ORPHAN_DIR_SYSTEM_INODE:
  253. lockdep_set_class(&inode->i_rwsem,
  254. &ocfs2_sysfile_lock_key[ORPHAN_DIR_SYSTEM_INODE]);
  255. break;
  256. case EXTENT_ALLOC_SYSTEM_INODE:
  257. lockdep_set_class(&inode->i_rwsem,
  258. &ocfs2_sysfile_lock_key[EXTENT_ALLOC_SYSTEM_INODE]);
  259. break;
  260. case INODE_ALLOC_SYSTEM_INODE:
  261. lockdep_set_class(&inode->i_rwsem,
  262. &ocfs2_sysfile_lock_key[INODE_ALLOC_SYSTEM_INODE]);
  263. break;
  264. case JOURNAL_SYSTEM_INODE:
  265. lockdep_set_class(&inode->i_rwsem,
  266. &ocfs2_sysfile_lock_key[JOURNAL_SYSTEM_INODE]);
  267. break;
  268. case LOCAL_ALLOC_SYSTEM_INODE:
  269. lockdep_set_class(&inode->i_rwsem,
  270. &ocfs2_sysfile_lock_key[LOCAL_ALLOC_SYSTEM_INODE]);
  271. break;
  272. case TRUNCATE_LOG_SYSTEM_INODE:
  273. lockdep_set_class(&inode->i_rwsem,
  274. &ocfs2_sysfile_lock_key[TRUNCATE_LOG_SYSTEM_INODE]);
  275. break;
  276. case LOCAL_USER_QUOTA_SYSTEM_INODE:
  277. lockdep_set_class(&inode->i_rwsem,
  278. &ocfs2_sysfile_lock_key[LOCAL_USER_QUOTA_SYSTEM_INODE]);
  279. break;
  280. case LOCAL_GROUP_QUOTA_SYSTEM_INODE:
  281. lockdep_set_class(&inode->i_rwsem,
  282. &ocfs2_sysfile_lock_key[LOCAL_GROUP_QUOTA_SYSTEM_INODE]);
  283. break;
  284. default:
  285. WARN_ONCE(1, "Unknown sysfile type %d\n", args->fi_sysfile_type);
  286. }
  287. if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
  288. args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
  289. args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
  290. args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
  291. lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  292. &ocfs2_quota_ip_alloc_sem_key);
  293. else
  294. lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  295. &ocfs2_file_ip_alloc_sem_key);
  296. #endif
  297. return 0;
  298. }
  299. void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  300. int create_ino)
  301. {
  302. struct super_block *sb;
  303. struct ocfs2_super *osb;
  304. int use_plocks = 1;
  305. sb = inode->i_sb;
  306. osb = OCFS2_SB(sb);
  307. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  308. ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  309. use_plocks = 0;
  310. /*
  311. * These have all been checked by ocfs2_read_inode_block() or set
  312. * by ocfs2_mknod_locked(), so a failure is a code bug.
  313. */
  314. BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
  315. cannot create a superblock
  316. inode today. change if
  317. that is needed. */
  318. BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
  319. BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
  320. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  321. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  322. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  323. inode_set_iversion(inode, 1);
  324. inode->i_generation = le32_to_cpu(fe->i_generation);
  325. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  326. inode->i_mode = le16_to_cpu(fe->i_mode);
  327. i_uid_write(inode, le32_to_cpu(fe->i_uid));
  328. i_gid_write(inode, le32_to_cpu(fe->i_gid));
  329. /* Fast symlinks will have i_size but no allocated clusters. */
  330. if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
  331. inode->i_blocks = 0;
  332. inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
  333. } else {
  334. inode->i_blocks = ocfs2_inode_sector_count(inode);
  335. inode->i_mapping->a_ops = &ocfs2_aops;
  336. }
  337. inode_set_atime(inode, le64_to_cpu(fe->i_atime),
  338. le32_to_cpu(fe->i_atime_nsec));
  339. inode_set_mtime(inode, le64_to_cpu(fe->i_mtime),
  340. le32_to_cpu(fe->i_mtime_nsec));
  341. inode_set_ctime(inode, le64_to_cpu(fe->i_ctime),
  342. le32_to_cpu(fe->i_ctime_nsec));
  343. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  344. mlog(ML_ERROR,
  345. "ip_blkno %llu != i_blkno %llu!\n",
  346. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  347. (unsigned long long)le64_to_cpu(fe->i_blkno));
  348. set_nlink(inode, ocfs2_read_links_count(fe));
  349. trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
  350. le32_to_cpu(fe->i_flags));
  351. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
  352. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  353. inode->i_flags |= S_NOQUOTA;
  354. }
  355. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  356. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  357. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  358. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  359. } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
  360. inode->i_flags |= S_NOQUOTA;
  361. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  362. /* we can't actually hit this as read_inode can't
  363. * handle superblocks today ;-) */
  364. BUG();
  365. }
  366. switch (inode->i_mode & S_IFMT) {
  367. case S_IFREG:
  368. if (use_plocks)
  369. inode->i_fop = &ocfs2_fops;
  370. else
  371. inode->i_fop = &ocfs2_fops_no_plocks;
  372. inode->i_op = &ocfs2_file_iops;
  373. i_size_write(inode, le64_to_cpu(fe->i_size));
  374. break;
  375. case S_IFDIR:
  376. inode->i_op = &ocfs2_dir_iops;
  377. if (use_plocks)
  378. inode->i_fop = &ocfs2_dops;
  379. else
  380. inode->i_fop = &ocfs2_dops_no_plocks;
  381. i_size_write(inode, le64_to_cpu(fe->i_size));
  382. OCFS2_I(inode)->ip_dir_lock_gen = 1;
  383. break;
  384. case S_IFLNK:
  385. inode->i_op = &ocfs2_symlink_inode_operations;
  386. inode_nohighmem(inode);
  387. i_size_write(inode, le64_to_cpu(fe->i_size));
  388. break;
  389. default:
  390. inode->i_op = &ocfs2_special_file_iops;
  391. init_special_inode(inode, inode->i_mode,
  392. inode->i_rdev);
  393. break;
  394. }
  395. if (create_ino) {
  396. inode->i_ino = ino_from_blkno(inode->i_sb,
  397. le64_to_cpu(fe->i_blkno));
  398. /*
  399. * If we ever want to create system files from kernel,
  400. * the generation argument to
  401. * ocfs2_inode_lock_res_init() will have to change.
  402. */
  403. BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
  404. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  405. OCFS2_LOCK_TYPE_META, 0, inode);
  406. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  407. OCFS2_LOCK_TYPE_OPEN, 0, inode);
  408. }
  409. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  410. OCFS2_LOCK_TYPE_RW, inode->i_generation,
  411. inode);
  412. ocfs2_set_inode_flags(inode);
  413. OCFS2_I(inode)->ip_last_used_slot = 0;
  414. OCFS2_I(inode)->ip_last_used_group = 0;
  415. if (S_ISDIR(inode->i_mode))
  416. ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
  417. OCFS2_RESV_FLAG_DIR);
  418. }
  419. static int ocfs2_read_locked_inode(struct inode *inode,
  420. struct ocfs2_find_inode_args *args)
  421. {
  422. struct super_block *sb;
  423. struct ocfs2_super *osb;
  424. struct ocfs2_dinode *fe;
  425. struct buffer_head *bh = NULL;
  426. int status, can_lock, lock_level = 0;
  427. u32 generation = 0;
  428. status = -EINVAL;
  429. sb = inode->i_sb;
  430. osb = OCFS2_SB(sb);
  431. /*
  432. * To improve performance of cold-cache inode stats, we take
  433. * the cluster lock here if possible.
  434. *
  435. * Generally, OCFS2 never trusts the contents of an inode
  436. * unless it's holding a cluster lock, so taking it here isn't
  437. * a correctness issue as much as it is a performance
  438. * improvement.
  439. *
  440. * There are three times when taking the lock is not a good idea:
  441. *
  442. * 1) During startup, before we have initialized the DLM.
  443. *
  444. * 2) If we are reading certain system files which never get
  445. * cluster locks (local alloc, truncate log).
  446. *
  447. * 3) If the process doing the iget() is responsible for
  448. * orphan dir recovery. We're holding the orphan dir lock and
  449. * can get into a deadlock with another process on another
  450. * node in ->delete_inode().
  451. *
  452. * #1 and #2 can be simply solved by never taking the lock
  453. * here for system files (which are the only type we read
  454. * during mount). It's a heavier approach, but our main
  455. * concern is user-accessible files anyway.
  456. *
  457. * #3 works itself out because we'll eventually take the
  458. * cluster lock before trusting anything anyway.
  459. */
  460. can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  461. && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
  462. && !ocfs2_mount_local(osb);
  463. trace_ocfs2_read_locked_inode(
  464. (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
  465. /*
  466. * To maintain backwards compatibility with older versions of
  467. * ocfs2-tools, we still store the generation value for system
  468. * files. The only ones that actually matter to userspace are
  469. * the journals, but it's easier and inexpensive to just flag
  470. * all system files similarly.
  471. */
  472. if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  473. generation = osb->fs_generation;
  474. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  475. OCFS2_LOCK_TYPE_META,
  476. generation, inode);
  477. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  478. OCFS2_LOCK_TYPE_OPEN,
  479. 0, inode);
  480. if (can_lock) {
  481. status = ocfs2_open_lock(inode);
  482. if (status) {
  483. make_bad_inode(inode);
  484. mlog_errno(status);
  485. return status;
  486. }
  487. status = ocfs2_inode_lock(inode, NULL, lock_level);
  488. if (status) {
  489. make_bad_inode(inode);
  490. mlog_errno(status);
  491. return status;
  492. }
  493. }
  494. if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  495. status = ocfs2_try_open_lock(inode, 0);
  496. if (status) {
  497. make_bad_inode(inode);
  498. return status;
  499. }
  500. }
  501. if (can_lock) {
  502. if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
  503. status = ocfs2_filecheck_read_inode_block_full(inode,
  504. &bh, OCFS2_BH_IGNORE_CACHE, 0);
  505. else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
  506. status = ocfs2_filecheck_read_inode_block_full(inode,
  507. &bh, OCFS2_BH_IGNORE_CACHE, 1);
  508. else
  509. status = ocfs2_read_inode_block_full(inode,
  510. &bh, OCFS2_BH_IGNORE_CACHE);
  511. } else {
  512. status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
  513. /*
  514. * If buffer is in jbd, then its checksum may not have been
  515. * computed as yet.
  516. */
  517. if (!status && !buffer_jbd(bh)) {
  518. if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
  519. status = ocfs2_filecheck_validate_inode_block(
  520. osb->sb, bh);
  521. else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
  522. status = ocfs2_filecheck_repair_inode_block(
  523. osb->sb, bh);
  524. else
  525. status = ocfs2_validate_inode_block(
  526. osb->sb, bh);
  527. }
  528. }
  529. if (status < 0) {
  530. mlog_errno(status);
  531. goto bail;
  532. }
  533. status = -EINVAL;
  534. fe = (struct ocfs2_dinode *) bh->b_data;
  535. /*
  536. * This is a code bug. Right now the caller needs to
  537. * understand whether it is asking for a system file inode or
  538. * not so the proper lock names can be built.
  539. */
  540. mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  541. !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  542. "Inode %llu: system file state is ambiguous\n",
  543. (unsigned long long)args->fi_blkno);
  544. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  545. S_ISBLK(le16_to_cpu(fe->i_mode)))
  546. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  547. ocfs2_populate_inode(inode, fe, 0);
  548. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  549. if (buffer_dirty(bh) && !buffer_jbd(bh)) {
  550. if (can_lock) {
  551. ocfs2_inode_unlock(inode, lock_level);
  552. lock_level = 1;
  553. ocfs2_inode_lock(inode, NULL, lock_level);
  554. }
  555. status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
  556. if (status < 0) {
  557. mlog_errno(status);
  558. goto bail;
  559. }
  560. }
  561. status = 0;
  562. bail:
  563. if (can_lock)
  564. ocfs2_inode_unlock(inode, lock_level);
  565. if (status < 0)
  566. make_bad_inode(inode);
  567. brelse(bh);
  568. return status;
  569. }
  570. void ocfs2_sync_blockdev(struct super_block *sb)
  571. {
  572. sync_blockdev(sb->s_bdev);
  573. }
  574. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  575. struct inode *inode,
  576. struct buffer_head *fe_bh)
  577. {
  578. int status = 0;
  579. struct ocfs2_dinode *fe;
  580. handle_t *handle = NULL;
  581. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  582. /*
  583. * This check will also skip truncate of inodes with inline
  584. * data and fast symlinks.
  585. */
  586. if (fe->i_clusters) {
  587. if (ocfs2_should_order_data(inode))
  588. ocfs2_begin_ordered_truncate(inode, 0);
  589. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  590. if (IS_ERR(handle)) {
  591. status = PTR_ERR(handle);
  592. handle = NULL;
  593. mlog_errno(status);
  594. goto out;
  595. }
  596. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  597. fe_bh,
  598. OCFS2_JOURNAL_ACCESS_WRITE);
  599. if (status < 0) {
  600. mlog_errno(status);
  601. goto out;
  602. }
  603. i_size_write(inode, 0);
  604. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  605. if (status < 0) {
  606. mlog_errno(status);
  607. goto out;
  608. }
  609. ocfs2_commit_trans(osb, handle);
  610. handle = NULL;
  611. status = ocfs2_commit_truncate(osb, inode, fe_bh);
  612. if (status < 0)
  613. mlog_errno(status);
  614. }
  615. out:
  616. if (handle)
  617. ocfs2_commit_trans(osb, handle);
  618. return status;
  619. }
  620. static int ocfs2_remove_inode(struct inode *inode,
  621. struct buffer_head *di_bh,
  622. struct inode *orphan_dir_inode,
  623. struct buffer_head *orphan_dir_bh)
  624. {
  625. int status;
  626. struct inode *inode_alloc_inode = NULL;
  627. struct buffer_head *inode_alloc_bh = NULL;
  628. handle_t *handle;
  629. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  630. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  631. inode_alloc_inode =
  632. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  633. le16_to_cpu(di->i_suballoc_slot));
  634. if (!inode_alloc_inode) {
  635. status = -ENOENT;
  636. mlog_errno(status);
  637. goto bail;
  638. }
  639. inode_lock(inode_alloc_inode);
  640. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  641. if (status < 0) {
  642. inode_unlock(inode_alloc_inode);
  643. mlog_errno(status);
  644. goto bail;
  645. }
  646. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
  647. ocfs2_quota_trans_credits(inode->i_sb));
  648. if (IS_ERR(handle)) {
  649. status = PTR_ERR(handle);
  650. mlog_errno(status);
  651. goto bail_unlock;
  652. }
  653. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  654. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  655. orphan_dir_bh, false);
  656. if (status < 0) {
  657. mlog_errno(status);
  658. goto bail_commit;
  659. }
  660. }
  661. /* set the inodes dtime */
  662. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  663. OCFS2_JOURNAL_ACCESS_WRITE);
  664. if (status < 0) {
  665. mlog_errno(status);
  666. goto bail_commit;
  667. }
  668. di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
  669. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  670. ocfs2_journal_dirty(handle, di_bh);
  671. ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
  672. dquot_free_inode(inode);
  673. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  674. inode_alloc_bh, di);
  675. if (status < 0)
  676. mlog_errno(status);
  677. bail_commit:
  678. ocfs2_commit_trans(osb, handle);
  679. bail_unlock:
  680. ocfs2_inode_unlock(inode_alloc_inode, 1);
  681. inode_unlock(inode_alloc_inode);
  682. brelse(inode_alloc_bh);
  683. bail:
  684. iput(inode_alloc_inode);
  685. return status;
  686. }
  687. /*
  688. * Serialize with orphan dir recovery. If the process doing
  689. * recovery on this orphan dir does an iget() with the dir
  690. * i_rwsem held, we'll deadlock here. Instead we detect this
  691. * and exit early - recovery will wipe this inode for us.
  692. */
  693. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  694. int slot)
  695. {
  696. int ret = 0;
  697. spin_lock(&osb->osb_lock);
  698. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  699. ret = -EDEADLK;
  700. goto out;
  701. }
  702. /* This signals to the orphan recovery process that it should
  703. * wait for us to handle the wipe. */
  704. osb->osb_orphan_wipes[slot]++;
  705. out:
  706. spin_unlock(&osb->osb_lock);
  707. trace_ocfs2_check_orphan_recovery_state(slot, ret);
  708. return ret;
  709. }
  710. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  711. int slot)
  712. {
  713. spin_lock(&osb->osb_lock);
  714. osb->osb_orphan_wipes[slot]--;
  715. spin_unlock(&osb->osb_lock);
  716. wake_up(&osb->osb_wipe_event);
  717. }
  718. static int ocfs2_wipe_inode(struct inode *inode,
  719. struct buffer_head *di_bh)
  720. {
  721. int status, orphaned_slot = -1;
  722. struct inode *orphan_dir_inode = NULL;
  723. struct buffer_head *orphan_dir_bh = NULL;
  724. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  725. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  726. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  727. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  728. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  729. if (status)
  730. return status;
  731. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  732. ORPHAN_DIR_SYSTEM_INODE,
  733. orphaned_slot);
  734. if (!orphan_dir_inode) {
  735. status = -ENOENT;
  736. mlog_errno(status);
  737. goto bail;
  738. }
  739. /* Lock the orphan dir. The lock will be held for the entire
  740. * delete_inode operation. We do this now to avoid races with
  741. * recovery completion on other nodes. */
  742. inode_lock(orphan_dir_inode);
  743. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  744. if (status < 0) {
  745. inode_unlock(orphan_dir_inode);
  746. mlog_errno(status);
  747. goto bail;
  748. }
  749. }
  750. /* we do this while holding the orphan dir lock because we
  751. * don't want recovery being run from another node to try an
  752. * inode delete underneath us -- this will result in two nodes
  753. * truncating the same file! */
  754. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  755. if (status < 0) {
  756. mlog_errno(status);
  757. goto bail_unlock_dir;
  758. }
  759. /* Remove any dir index tree */
  760. if (S_ISDIR(inode->i_mode)) {
  761. status = ocfs2_dx_dir_truncate(inode, di_bh);
  762. if (status) {
  763. mlog_errno(status);
  764. goto bail_unlock_dir;
  765. }
  766. }
  767. /*Free extended attribute resources associated with this inode.*/
  768. status = ocfs2_xattr_remove(inode, di_bh);
  769. if (status < 0) {
  770. mlog_errno(status);
  771. goto bail_unlock_dir;
  772. }
  773. status = ocfs2_remove_refcount_tree(inode, di_bh);
  774. if (status < 0) {
  775. mlog_errno(status);
  776. goto bail_unlock_dir;
  777. }
  778. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  779. orphan_dir_bh);
  780. if (status < 0)
  781. mlog_errno(status);
  782. bail_unlock_dir:
  783. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
  784. return status;
  785. ocfs2_inode_unlock(orphan_dir_inode, 1);
  786. inode_unlock(orphan_dir_inode);
  787. brelse(orphan_dir_bh);
  788. bail:
  789. iput(orphan_dir_inode);
  790. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  791. return status;
  792. }
  793. /* There is a series of simple checks that should be done before a
  794. * trylock is even considered. Encapsulate those in this function. */
  795. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  796. {
  797. int ret = 0;
  798. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  799. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  800. trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
  801. (unsigned long long)oi->ip_blkno,
  802. oi->ip_flags);
  803. /* We shouldn't be getting here for the root directory
  804. * inode.. */
  805. if (inode == osb->root_inode) {
  806. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  807. goto bail;
  808. }
  809. /*
  810. * If we're coming from downconvert_thread we can't go into our own
  811. * voting [hello, deadlock city!] so we cannot delete the inode. But
  812. * since we dropped last inode ref when downconverting dentry lock,
  813. * we cannot have the file open and thus the node doing unlink will
  814. * take care of deleting the inode.
  815. */
  816. if (current == osb->dc_task)
  817. goto bail;
  818. spin_lock(&oi->ip_lock);
  819. /* OCFS2 *never* deletes system files. This should technically
  820. * never get here as system file inodes should always have a
  821. * positive link count. */
  822. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  823. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  824. (unsigned long long)oi->ip_blkno);
  825. goto bail_unlock;
  826. }
  827. ret = 1;
  828. bail_unlock:
  829. spin_unlock(&oi->ip_lock);
  830. bail:
  831. return ret;
  832. }
  833. /* Query the cluster to determine whether we should wipe an inode from
  834. * disk or not.
  835. *
  836. * Requires the inode to have the cluster lock. */
  837. static int ocfs2_query_inode_wipe(struct inode *inode,
  838. struct buffer_head *di_bh,
  839. int *wipe)
  840. {
  841. int status = 0, reason = 0;
  842. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  843. struct ocfs2_dinode *di;
  844. *wipe = 0;
  845. trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
  846. inode->i_nlink);
  847. /* While we were waiting for the cluster lock in
  848. * ocfs2_delete_inode, another node might have asked to delete
  849. * the inode. Recheck our flags to catch this. */
  850. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  851. reason = 1;
  852. goto bail;
  853. }
  854. /* Now that we have an up to date inode, we can double check
  855. * the link count. */
  856. if (inode->i_nlink)
  857. goto bail;
  858. /* Do some basic inode verification... */
  859. di = (struct ocfs2_dinode *) di_bh->b_data;
  860. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
  861. !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  862. /*
  863. * Inodes in the orphan dir must have ORPHANED_FL. The only
  864. * inodes that come back out of the orphan dir are reflink
  865. * targets. A reflink target may be moved out of the orphan
  866. * dir between the time we scan the directory and the time we
  867. * process it. This would lead to HAS_REFCOUNT_FL being set but
  868. * ORPHANED_FL not.
  869. */
  870. if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
  871. reason = 2;
  872. goto bail;
  873. }
  874. /* for lack of a better error? */
  875. status = -EEXIST;
  876. mlog(ML_ERROR,
  877. "Inode %llu (on-disk %llu) not orphaned! "
  878. "Disk flags 0x%x, inode flags 0x%x\n",
  879. (unsigned long long)oi->ip_blkno,
  880. (unsigned long long)le64_to_cpu(di->i_blkno),
  881. le32_to_cpu(di->i_flags), oi->ip_flags);
  882. goto bail;
  883. }
  884. /* has someone already deleted us?! baaad... */
  885. if (di->i_dtime) {
  886. status = -EEXIST;
  887. mlog_errno(status);
  888. goto bail;
  889. }
  890. /*
  891. * This is how ocfs2 determines whether an inode is still live
  892. * within the cluster. Every node takes a shared read lock on
  893. * the inode open lock in ocfs2_read_locked_inode(). When we
  894. * get to ->delete_inode(), each node tries to convert it's
  895. * lock to an exclusive. Trylocks are serialized by the inode
  896. * meta data lock. If the upconvert succeeds, we know the inode
  897. * is no longer live and can be deleted.
  898. *
  899. * Though we call this with the meta data lock held, the
  900. * trylock keeps us from ABBA deadlock.
  901. */
  902. status = ocfs2_try_open_lock(inode, 1);
  903. if (status == -EAGAIN) {
  904. status = 0;
  905. reason = 3;
  906. goto bail;
  907. }
  908. if (status < 0) {
  909. mlog_errno(status);
  910. goto bail;
  911. }
  912. *wipe = 1;
  913. trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
  914. bail:
  915. trace_ocfs2_query_inode_wipe_end(status, reason);
  916. return status;
  917. }
  918. /* Support function for ocfs2_delete_inode. Will help us keep the
  919. * inode data in a consistent state for clear_inode. Always truncates
  920. * pages, optionally sync's them first. */
  921. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  922. int sync_data)
  923. {
  924. trace_ocfs2_cleanup_delete_inode(
  925. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  926. if (sync_data)
  927. filemap_write_and_wait(inode->i_mapping);
  928. truncate_inode_pages_final(&inode->i_data);
  929. }
  930. static void ocfs2_delete_inode(struct inode *inode)
  931. {
  932. int wipe, status;
  933. sigset_t oldset;
  934. struct buffer_head *di_bh = NULL;
  935. struct ocfs2_dinode *di = NULL;
  936. trace_ocfs2_delete_inode(inode->i_ino,
  937. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  938. is_bad_inode(inode));
  939. /* When we fail in read_inode() we mark inode as bad. The second test
  940. * catches the case when inode allocation fails before allocating
  941. * a block for inode. */
  942. if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
  943. goto bail;
  944. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  945. /* It's probably not necessary to truncate_inode_pages
  946. * here but we do it for safety anyway (it will most
  947. * likely be a no-op anyway) */
  948. ocfs2_cleanup_delete_inode(inode, 0);
  949. goto bail;
  950. }
  951. dquot_initialize(inode);
  952. /* We want to block signals in delete_inode as the lock and
  953. * messaging paths may return us -ERESTARTSYS. Which would
  954. * cause us to exit early, resulting in inodes being orphaned
  955. * forever. */
  956. ocfs2_block_signals(&oldset);
  957. /*
  958. * Synchronize us against ocfs2_get_dentry. We take this in
  959. * shared mode so that all nodes can still concurrently
  960. * process deletes.
  961. */
  962. status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
  963. if (status < 0) {
  964. mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
  965. ocfs2_cleanup_delete_inode(inode, 0);
  966. goto bail_unblock;
  967. }
  968. /* Lock down the inode. This gives us an up to date view of
  969. * it's metadata (for verification), and allows us to
  970. * serialize delete_inode on multiple nodes.
  971. *
  972. * Even though we might be doing a truncate, we don't take the
  973. * allocation lock here as it won't be needed - nobody will
  974. * have the file open.
  975. */
  976. status = ocfs2_inode_lock(inode, &di_bh, 1);
  977. if (status < 0) {
  978. if (status != -ENOENT)
  979. mlog_errno(status);
  980. ocfs2_cleanup_delete_inode(inode, 0);
  981. goto bail_unlock_nfs_sync;
  982. }
  983. di = (struct ocfs2_dinode *)di_bh->b_data;
  984. /* Skip inode deletion and wait for dio orphan entry recovered
  985. * first */
  986. if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
  987. ocfs2_cleanup_delete_inode(inode, 0);
  988. goto bail_unlock_inode;
  989. }
  990. /* Query the cluster. This will be the final decision made
  991. * before we go ahead and wipe the inode. */
  992. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  993. if (!wipe || status < 0) {
  994. /* Error and remote inode busy both mean we won't be
  995. * removing the inode, so they take almost the same
  996. * path. */
  997. if (status < 0)
  998. mlog_errno(status);
  999. /* Someone in the cluster has disallowed a wipe of
  1000. * this inode, or it was never completely
  1001. * orphaned. Write out the pages and exit now. */
  1002. ocfs2_cleanup_delete_inode(inode, 1);
  1003. goto bail_unlock_inode;
  1004. }
  1005. ocfs2_cleanup_delete_inode(inode, 0);
  1006. status = ocfs2_wipe_inode(inode, di_bh);
  1007. if (status < 0) {
  1008. if (status != -EDEADLK)
  1009. mlog_errno(status);
  1010. goto bail_unlock_inode;
  1011. }
  1012. /*
  1013. * Mark the inode as successfully deleted.
  1014. *
  1015. * This is important for ocfs2_clear_inode() as it will check
  1016. * this flag and skip any checkpointing work
  1017. *
  1018. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  1019. * the LVB for other nodes.
  1020. */
  1021. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  1022. bail_unlock_inode:
  1023. ocfs2_inode_unlock(inode, 1);
  1024. brelse(di_bh);
  1025. bail_unlock_nfs_sync:
  1026. ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
  1027. bail_unblock:
  1028. ocfs2_unblock_signals(&oldset);
  1029. bail:
  1030. return;
  1031. }
  1032. static void ocfs2_clear_inode(struct inode *inode)
  1033. {
  1034. int status;
  1035. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1036. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1037. clear_inode(inode);
  1038. trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
  1039. inode->i_nlink);
  1040. mlog_bug_on_msg(osb == NULL,
  1041. "Inode=%lu\n", inode->i_ino);
  1042. dquot_drop(inode);
  1043. /* To prevent remote deletes we hold open lock before, now it
  1044. * is time to unlock PR and EX open locks. */
  1045. ocfs2_open_unlock(inode);
  1046. /* Do these before all the other work so that we don't bounce
  1047. * the downconvert thread while waiting to destroy the locks. */
  1048. ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres);
  1049. ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres);
  1050. ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres);
  1051. ocfs2_resv_discard(&osb->osb_la_resmap,
  1052. &oi->ip_la_data_resv);
  1053. ocfs2_resv_init_once(&oi->ip_la_data_resv);
  1054. /* We very well may get a clear_inode before all an inodes
  1055. * metadata has hit disk. Of course, we can't drop any cluster
  1056. * locks until the journal has finished with it. The only
  1057. * exception here are successfully wiped inodes - their
  1058. * metadata can now be considered to be part of the system
  1059. * inodes from which it came. */
  1060. if (!(oi->ip_flags & OCFS2_INODE_DELETED))
  1061. ocfs2_checkpoint_inode(inode);
  1062. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  1063. "Clear inode of %llu, inode has io markers\n",
  1064. (unsigned long long)oi->ip_blkno);
  1065. mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list),
  1066. "Clear inode of %llu, inode has unwritten extents\n",
  1067. (unsigned long long)oi->ip_blkno);
  1068. ocfs2_extent_map_trunc(inode, 0);
  1069. status = ocfs2_drop_inode_locks(inode);
  1070. if (status < 0)
  1071. mlog_errno(status);
  1072. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  1073. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  1074. ocfs2_lock_res_free(&oi->ip_open_lockres);
  1075. ocfs2_metadata_cache_exit(INODE_CACHE(inode));
  1076. mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
  1077. "Clear inode of %llu, inode has %u cache items\n",
  1078. (unsigned long long)oi->ip_blkno,
  1079. INODE_CACHE(inode)->ci_num_cached);
  1080. mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
  1081. "Clear inode of %llu, inode has a bad flag\n",
  1082. (unsigned long long)oi->ip_blkno);
  1083. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  1084. "Clear inode of %llu, inode is locked\n",
  1085. (unsigned long long)oi->ip_blkno);
  1086. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  1087. "Clear inode of %llu, io_mutex is locked\n",
  1088. (unsigned long long)oi->ip_blkno);
  1089. mutex_unlock(&oi->ip_io_mutex);
  1090. /*
  1091. * down_trylock() returns 0, down_write_trylock() returns 1
  1092. * kernel 1, world 0
  1093. */
  1094. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  1095. "Clear inode of %llu, alloc_sem is locked\n",
  1096. (unsigned long long)oi->ip_blkno);
  1097. up_write(&oi->ip_alloc_sem);
  1098. mlog_bug_on_msg(oi->ip_open_count,
  1099. "Clear inode of %llu has open count %d\n",
  1100. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  1101. /* Clear all other flags. */
  1102. oi->ip_flags = 0;
  1103. oi->ip_dir_start_lookup = 0;
  1104. oi->ip_blkno = 0ULL;
  1105. /*
  1106. * ip_jinode is used to track txns against this inode. We ensure that
  1107. * the journal is flushed before journal shutdown. Thus it is safe to
  1108. * have inodes get cleaned up after journal shutdown.
  1109. */
  1110. if (!osb->journal)
  1111. return;
  1112. jbd2_journal_release_jbd_inode(osb->journal->j_journal,
  1113. &oi->ip_jinode);
  1114. }
  1115. void ocfs2_evict_inode(struct inode *inode)
  1116. {
  1117. write_inode_now(inode, 1);
  1118. if (!inode->i_nlink ||
  1119. (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
  1120. ocfs2_delete_inode(inode);
  1121. } else {
  1122. truncate_inode_pages_final(&inode->i_data);
  1123. }
  1124. ocfs2_clear_inode(inode);
  1125. }
  1126. /*
  1127. * This is called from our getattr.
  1128. */
  1129. int ocfs2_inode_revalidate(struct dentry *dentry)
  1130. {
  1131. struct inode *inode = d_inode(dentry);
  1132. int status = 0;
  1133. trace_ocfs2_inode_revalidate(inode,
  1134. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
  1135. inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
  1136. if (!inode) {
  1137. status = -ENOENT;
  1138. goto bail;
  1139. }
  1140. spin_lock(&OCFS2_I(inode)->ip_lock);
  1141. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  1142. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1143. status = -ENOENT;
  1144. goto bail;
  1145. }
  1146. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1147. /* Let ocfs2_inode_lock do the work of updating our struct
  1148. * inode for us. */
  1149. status = ocfs2_inode_lock(inode, NULL, 0);
  1150. if (status < 0) {
  1151. if (status != -ENOENT)
  1152. mlog_errno(status);
  1153. goto bail;
  1154. }
  1155. ocfs2_inode_unlock(inode, 0);
  1156. bail:
  1157. return status;
  1158. }
  1159. /*
  1160. * Updates a disk inode from a
  1161. * struct inode.
  1162. * Only takes ip_lock.
  1163. */
  1164. int ocfs2_mark_inode_dirty(handle_t *handle,
  1165. struct inode *inode,
  1166. struct buffer_head *bh)
  1167. {
  1168. int status;
  1169. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1170. trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
  1171. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1172. OCFS2_JOURNAL_ACCESS_WRITE);
  1173. if (status < 0) {
  1174. mlog_errno(status);
  1175. goto leave;
  1176. }
  1177. spin_lock(&OCFS2_I(inode)->ip_lock);
  1178. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1179. ocfs2_get_inode_flags(OCFS2_I(inode));
  1180. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1181. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1182. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1183. fe->i_size = cpu_to_le64(i_size_read(inode));
  1184. ocfs2_set_links_count(fe, inode->i_nlink);
  1185. fe->i_uid = cpu_to_le32(i_uid_read(inode));
  1186. fe->i_gid = cpu_to_le32(i_gid_read(inode));
  1187. fe->i_mode = cpu_to_le16(inode->i_mode);
  1188. fe->i_atime = cpu_to_le64(inode_get_atime_sec(inode));
  1189. fe->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
  1190. fe->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
  1191. fe->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
  1192. fe->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode));
  1193. fe->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
  1194. ocfs2_journal_dirty(handle, bh);
  1195. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1196. leave:
  1197. return status;
  1198. }
  1199. /*
  1200. *
  1201. * Updates a struct inode from a disk inode.
  1202. * does no i/o, only takes ip_lock.
  1203. */
  1204. void ocfs2_refresh_inode(struct inode *inode,
  1205. struct ocfs2_dinode *fe)
  1206. {
  1207. spin_lock(&OCFS2_I(inode)->ip_lock);
  1208. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1209. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1210. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1211. ocfs2_set_inode_flags(inode);
  1212. i_size_write(inode, le64_to_cpu(fe->i_size));
  1213. set_nlink(inode, ocfs2_read_links_count(fe));
  1214. i_uid_write(inode, le32_to_cpu(fe->i_uid));
  1215. i_gid_write(inode, le32_to_cpu(fe->i_gid));
  1216. inode->i_mode = le16_to_cpu(fe->i_mode);
  1217. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1218. inode->i_blocks = 0;
  1219. else
  1220. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1221. inode_set_atime(inode, le64_to_cpu(fe->i_atime),
  1222. le32_to_cpu(fe->i_atime_nsec));
  1223. inode_set_mtime(inode, le64_to_cpu(fe->i_mtime),
  1224. le32_to_cpu(fe->i_mtime_nsec));
  1225. inode_set_ctime(inode, le64_to_cpu(fe->i_ctime),
  1226. le32_to_cpu(fe->i_ctime_nsec));
  1227. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1228. }
  1229. int ocfs2_validate_inode_block(struct super_block *sb,
  1230. struct buffer_head *bh)
  1231. {
  1232. int rc;
  1233. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1234. trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
  1235. BUG_ON(!buffer_uptodate(bh));
  1236. /*
  1237. * If the ecc fails, we return the error but otherwise
  1238. * leave the filesystem running. We know any error is
  1239. * local to this block.
  1240. */
  1241. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1242. if (rc) {
  1243. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  1244. (unsigned long long)bh->b_blocknr);
  1245. goto bail;
  1246. }
  1247. if ((!di->i_links_count && !di->i_links_count_hi) || !di->i_mode) {
  1248. mlog(ML_ERROR, "Invalid dinode #%llu: "
  1249. "Corrupt state (nlink = %u or mode = %u) detected!\n",
  1250. (unsigned long long)bh->b_blocknr,
  1251. ocfs2_read_links_count(di), le16_to_cpu(di->i_mode));
  1252. rc = -EFSCORRUPTED;
  1253. goto bail;
  1254. }
  1255. /*
  1256. * Errors after here are fatal.
  1257. */
  1258. rc = -EINVAL;
  1259. if (!OCFS2_IS_VALID_DINODE(di)) {
  1260. rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
  1261. (unsigned long long)bh->b_blocknr, 7,
  1262. di->i_signature);
  1263. goto bail;
  1264. }
  1265. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1266. rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
  1267. (unsigned long long)bh->b_blocknr,
  1268. (unsigned long long)le64_to_cpu(di->i_blkno));
  1269. goto bail;
  1270. }
  1271. if (!(le32_to_cpu(di->i_flags) & OCFS2_VALID_FL)) {
  1272. rc = ocfs2_error(sb,
  1273. "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
  1274. (unsigned long long)bh->b_blocknr);
  1275. goto bail;
  1276. }
  1277. if (le32_to_cpu(di->i_fs_generation) !=
  1278. OCFS2_SB(sb)->fs_generation) {
  1279. rc = ocfs2_error(sb,
  1280. "Invalid dinode #%llu: fs_generation is %u\n",
  1281. (unsigned long long)bh->b_blocknr,
  1282. le32_to_cpu(di->i_fs_generation));
  1283. goto bail;
  1284. }
  1285. if (le16_to_cpu(di->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
  1286. (u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slots - 1) {
  1287. rc = ocfs2_error(sb, "Invalid dinode %llu: suballoc slot %u\n",
  1288. (unsigned long long)bh->b_blocknr,
  1289. le16_to_cpu(di->i_suballoc_slot));
  1290. goto bail;
  1291. }
  1292. if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) {
  1293. struct ocfs2_inline_data *data = &di->id2.i_data;
  1294. if (le32_to_cpu(di->i_clusters)) {
  1295. rc = ocfs2_error(sb,
  1296. "Invalid dinode %llu: %u clusters\n",
  1297. (unsigned long long)bh->b_blocknr,
  1298. le32_to_cpu(di->i_clusters));
  1299. goto bail;
  1300. }
  1301. if (le16_to_cpu(data->id_count) >
  1302. ocfs2_max_inline_data_with_xattr(sb, di)) {
  1303. rc = ocfs2_error(sb,
  1304. "Invalid dinode #%llu: inline data id_count %u exceeds max %d\n",
  1305. (unsigned long long)bh->b_blocknr,
  1306. le16_to_cpu(data->id_count),
  1307. ocfs2_max_inline_data_with_xattr(sb, di));
  1308. goto bail;
  1309. }
  1310. if (le64_to_cpu(di->i_size) > le16_to_cpu(data->id_count)) {
  1311. rc = ocfs2_error(sb,
  1312. "Invalid dinode #%llu: inline data i_size %llu exceeds id_count %u\n",
  1313. (unsigned long long)bh->b_blocknr,
  1314. (unsigned long long)le64_to_cpu(di->i_size),
  1315. le16_to_cpu(data->id_count));
  1316. goto bail;
  1317. }
  1318. }
  1319. if (le32_to_cpu(di->i_flags) & OCFS2_CHAIN_FL) {
  1320. struct ocfs2_chain_list *cl = &di->id2.i_chain;
  1321. u16 bpc = 1 << (OCFS2_SB(sb)->s_clustersize_bits -
  1322. sb->s_blocksize_bits);
  1323. if (le16_to_cpu(cl->cl_count) != ocfs2_chain_recs_per_inode(sb)) {
  1324. rc = ocfs2_error(sb, "Invalid dinode %llu: chain list count %u\n",
  1325. (unsigned long long)bh->b_blocknr,
  1326. le16_to_cpu(cl->cl_count));
  1327. goto bail;
  1328. }
  1329. if (le16_to_cpu(cl->cl_next_free_rec) > le16_to_cpu(cl->cl_count)) {
  1330. rc = ocfs2_error(sb, "Invalid dinode %llu: chain list index %u\n",
  1331. (unsigned long long)bh->b_blocknr,
  1332. le16_to_cpu(cl->cl_next_free_rec));
  1333. goto bail;
  1334. }
  1335. if (OCFS2_SB(sb)->bitmap_blkno &&
  1336. OCFS2_SB(sb)->bitmap_blkno != le64_to_cpu(di->i_blkno) &&
  1337. le16_to_cpu(cl->cl_bpc) != bpc) {
  1338. rc = ocfs2_error(sb, "Invalid dinode %llu: bits per cluster %u\n",
  1339. (unsigned long long)bh->b_blocknr,
  1340. le16_to_cpu(cl->cl_bpc));
  1341. goto bail;
  1342. }
  1343. }
  1344. if ((le16_to_cpu(di->i_dyn_features) & OCFS2_HAS_REFCOUNT_FL) &&
  1345. !di->i_refcount_loc) {
  1346. rc = ocfs2_error(sb, "Inode #%llu has refcount flag but no i_refcount_loc\n",
  1347. (unsigned long long)bh->b_blocknr);
  1348. goto bail;
  1349. }
  1350. rc = 0;
  1351. bail:
  1352. return rc;
  1353. }
  1354. static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
  1355. struct buffer_head *bh)
  1356. {
  1357. int rc = 0;
  1358. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1359. trace_ocfs2_filecheck_validate_inode_block(
  1360. (unsigned long long)bh->b_blocknr);
  1361. BUG_ON(!buffer_uptodate(bh));
  1362. /*
  1363. * Call ocfs2_validate_meta_ecc() first since it has ecc repair
  1364. * function, but we should not return error immediately when ecc
  1365. * validation fails, because the reason is quite likely the invalid
  1366. * inode number inputted.
  1367. */
  1368. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1369. if (rc) {
  1370. mlog(ML_ERROR,
  1371. "Filecheck: checksum failed for dinode %llu\n",
  1372. (unsigned long long)bh->b_blocknr);
  1373. rc = -OCFS2_FILECHECK_ERR_BLOCKECC;
  1374. }
  1375. if (!OCFS2_IS_VALID_DINODE(di)) {
  1376. mlog(ML_ERROR,
  1377. "Filecheck: invalid dinode #%llu: signature = %.*s\n",
  1378. (unsigned long long)bh->b_blocknr, 7, di->i_signature);
  1379. rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
  1380. goto bail;
  1381. } else if (rc)
  1382. goto bail;
  1383. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1384. mlog(ML_ERROR,
  1385. "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
  1386. (unsigned long long)bh->b_blocknr,
  1387. (unsigned long long)le64_to_cpu(di->i_blkno));
  1388. rc = -OCFS2_FILECHECK_ERR_BLOCKNO;
  1389. goto bail;
  1390. }
  1391. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1392. mlog(ML_ERROR,
  1393. "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
  1394. "not set\n",
  1395. (unsigned long long)bh->b_blocknr);
  1396. rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1397. goto bail;
  1398. }
  1399. if (le32_to_cpu(di->i_fs_generation) !=
  1400. OCFS2_SB(sb)->fs_generation) {
  1401. mlog(ML_ERROR,
  1402. "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
  1403. (unsigned long long)bh->b_blocknr,
  1404. le32_to_cpu(di->i_fs_generation));
  1405. rc = -OCFS2_FILECHECK_ERR_GENERATION;
  1406. }
  1407. bail:
  1408. return rc;
  1409. }
  1410. static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
  1411. struct buffer_head *bh)
  1412. {
  1413. int changed = 0;
  1414. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1415. if (!ocfs2_filecheck_validate_inode_block(sb, bh))
  1416. return 0;
  1417. trace_ocfs2_filecheck_repair_inode_block(
  1418. (unsigned long long)bh->b_blocknr);
  1419. if (unlikely(ocfs2_emergency_state(OCFS2_SB(sb)))) {
  1420. mlog(ML_ERROR,
  1421. "Filecheck: cannot repair dinode #%llu "
  1422. "on readonly filesystem\n",
  1423. (unsigned long long)bh->b_blocknr);
  1424. return -OCFS2_FILECHECK_ERR_READONLY;
  1425. }
  1426. if (buffer_jbd(bh)) {
  1427. mlog(ML_ERROR,
  1428. "Filecheck: cannot repair dinode #%llu, "
  1429. "its buffer is in jbd\n",
  1430. (unsigned long long)bh->b_blocknr);
  1431. return -OCFS2_FILECHECK_ERR_INJBD;
  1432. }
  1433. if (!OCFS2_IS_VALID_DINODE(di)) {
  1434. /* Cannot fix invalid inode block */
  1435. return -OCFS2_FILECHECK_ERR_INVALIDINO;
  1436. }
  1437. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1438. /* Cannot just add VALID_FL flag back as a fix,
  1439. * need more things to check here.
  1440. */
  1441. return -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1442. }
  1443. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1444. di->i_blkno = cpu_to_le64(bh->b_blocknr);
  1445. changed = 1;
  1446. mlog(ML_ERROR,
  1447. "Filecheck: reset dinode #%llu: i_blkno to %llu\n",
  1448. (unsigned long long)bh->b_blocknr,
  1449. (unsigned long long)le64_to_cpu(di->i_blkno));
  1450. }
  1451. if (le32_to_cpu(di->i_fs_generation) !=
  1452. OCFS2_SB(sb)->fs_generation) {
  1453. di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  1454. changed = 1;
  1455. mlog(ML_ERROR,
  1456. "Filecheck: reset dinode #%llu: fs_generation to %u\n",
  1457. (unsigned long long)bh->b_blocknr,
  1458. le32_to_cpu(di->i_fs_generation));
  1459. }
  1460. if (ocfs2_dinode_has_extents(di) &&
  1461. le16_to_cpu(di->id2.i_list.l_next_free_rec) > le16_to_cpu(di->id2.i_list.l_count)) {
  1462. di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count;
  1463. changed = 1;
  1464. mlog(ML_ERROR,
  1465. "Filecheck: reset dinode #%llu: l_next_free_rec to %u\n",
  1466. (unsigned long long)bh->b_blocknr,
  1467. le16_to_cpu(di->id2.i_list.l_next_free_rec));
  1468. }
  1469. if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
  1470. ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
  1471. mark_buffer_dirty(bh);
  1472. mlog(ML_ERROR,
  1473. "Filecheck: reset dinode #%llu: compute meta ecc\n",
  1474. (unsigned long long)bh->b_blocknr);
  1475. }
  1476. return 0;
  1477. }
  1478. static int
  1479. ocfs2_filecheck_read_inode_block_full(struct inode *inode,
  1480. struct buffer_head **bh,
  1481. int flags, int type)
  1482. {
  1483. int rc;
  1484. struct buffer_head *tmp = *bh;
  1485. if (!type) /* Check inode block */
  1486. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1487. OCFS2_I(inode)->ip_blkno,
  1488. 1, &tmp, flags,
  1489. ocfs2_filecheck_validate_inode_block);
  1490. else /* Repair inode block */
  1491. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1492. OCFS2_I(inode)->ip_blkno,
  1493. 1, &tmp, flags,
  1494. ocfs2_filecheck_repair_inode_block);
  1495. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1496. if (!rc && !*bh)
  1497. *bh = tmp;
  1498. return rc;
  1499. }
  1500. int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  1501. int flags)
  1502. {
  1503. int rc;
  1504. struct buffer_head *tmp = *bh;
  1505. rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
  1506. 1, &tmp, flags, ocfs2_validate_inode_block);
  1507. if (rc < 0)
  1508. make_bad_inode(inode);
  1509. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1510. if (!rc && !*bh)
  1511. *bh = tmp;
  1512. return rc;
  1513. }
  1514. int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  1515. {
  1516. return ocfs2_read_inode_block_full(inode, bh, 0);
  1517. }
  1518. static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
  1519. {
  1520. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1521. return oi->ip_blkno;
  1522. }
  1523. static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
  1524. {
  1525. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1526. return oi->vfs_inode.i_sb;
  1527. }
  1528. static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
  1529. __acquires(&oi->ip_lock)
  1530. {
  1531. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1532. spin_lock(&oi->ip_lock);
  1533. }
  1534. static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
  1535. __releases(&oi->ip_lock)
  1536. {
  1537. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1538. spin_unlock(&oi->ip_lock);
  1539. }
  1540. static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
  1541. {
  1542. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1543. mutex_lock(&oi->ip_io_mutex);
  1544. }
  1545. static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
  1546. {
  1547. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1548. mutex_unlock(&oi->ip_io_mutex);
  1549. }
  1550. const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
  1551. .co_owner = ocfs2_inode_cache_owner,
  1552. .co_get_super = ocfs2_inode_cache_get_super,
  1553. .co_cache_lock = ocfs2_inode_cache_lock,
  1554. .co_cache_unlock = ocfs2_inode_cache_unlock,
  1555. .co_io_lock = ocfs2_inode_cache_io_lock,
  1556. .co_io_unlock = ocfs2_inode_cache_io_unlock,
  1557. };