inode.c 40 KB

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