inode.c 40 KB

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