file.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  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/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #define MLOG_MASK_PREFIX ML_INODE
  37. #include <cluster/masklog.h>
  38. #include "ocfs2.h"
  39. #include "alloc.h"
  40. #include "aops.h"
  41. #include "dir.h"
  42. #include "dlmglue.h"
  43. #include "extent_map.h"
  44. #include "file.h"
  45. #include "sysfile.h"
  46. #include "inode.h"
  47. #include "ioctl.h"
  48. #include "journal.h"
  49. #include "mmap.h"
  50. #include "suballoc.h"
  51. #include "super.h"
  52. #include "buffer_head_io.h"
  53. static int ocfs2_sync_inode(struct inode *inode)
  54. {
  55. filemap_fdatawrite(inode->i_mapping);
  56. return sync_mapping_buffers(inode->i_mapping);
  57. }
  58. static int ocfs2_file_open(struct inode *inode, struct file *file)
  59. {
  60. int status;
  61. int mode = file->f_flags;
  62. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  63. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  64. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  65. spin_lock(&oi->ip_lock);
  66. /* Check that the inode hasn't been wiped from disk by another
  67. * node. If it hasn't then we're safe as long as we hold the
  68. * spin lock until our increment of open count. */
  69. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  70. spin_unlock(&oi->ip_lock);
  71. status = -ENOENT;
  72. goto leave;
  73. }
  74. if (mode & O_DIRECT)
  75. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  76. oi->ip_open_count++;
  77. spin_unlock(&oi->ip_lock);
  78. status = 0;
  79. leave:
  80. mlog_exit(status);
  81. return status;
  82. }
  83. static int ocfs2_file_release(struct inode *inode, struct file *file)
  84. {
  85. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  86. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  87. file->f_path.dentry->d_name.len,
  88. file->f_path.dentry->d_name.name);
  89. spin_lock(&oi->ip_lock);
  90. if (!--oi->ip_open_count)
  91. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  92. spin_unlock(&oi->ip_lock);
  93. mlog_exit(0);
  94. return 0;
  95. }
  96. static int ocfs2_sync_file(struct file *file,
  97. struct dentry *dentry,
  98. int datasync)
  99. {
  100. int err = 0;
  101. journal_t *journal;
  102. struct inode *inode = dentry->d_inode;
  103. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  104. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  105. dentry->d_name.len, dentry->d_name.name);
  106. err = ocfs2_sync_inode(dentry->d_inode);
  107. if (err)
  108. goto bail;
  109. journal = osb->journal->j_journal;
  110. err = journal_force_commit(journal);
  111. bail:
  112. mlog_exit(err);
  113. return (err < 0) ? -EIO : 0;
  114. }
  115. int ocfs2_should_update_atime(struct inode *inode,
  116. struct vfsmount *vfsmnt)
  117. {
  118. struct timespec now;
  119. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  120. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  121. return 0;
  122. if ((inode->i_flags & S_NOATIME) ||
  123. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  124. return 0;
  125. /*
  126. * We can be called with no vfsmnt structure - NFSD will
  127. * sometimes do this.
  128. *
  129. * Note that our action here is different than touch_atime() -
  130. * if we can't tell whether this is a noatime mount, then we
  131. * don't know whether to trust the value of s_atime_quantum.
  132. */
  133. if (vfsmnt == NULL)
  134. return 0;
  135. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  136. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  137. return 0;
  138. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  139. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  140. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  141. return 1;
  142. return 0;
  143. }
  144. now = CURRENT_TIME;
  145. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  146. return 0;
  147. else
  148. return 1;
  149. }
  150. int ocfs2_update_inode_atime(struct inode *inode,
  151. struct buffer_head *bh)
  152. {
  153. int ret;
  154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  155. handle_t *handle;
  156. mlog_entry_void();
  157. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  158. if (handle == NULL) {
  159. ret = -ENOMEM;
  160. mlog_errno(ret);
  161. goto out;
  162. }
  163. inode->i_atime = CURRENT_TIME;
  164. ret = ocfs2_mark_inode_dirty(handle, inode, bh);
  165. if (ret < 0)
  166. mlog_errno(ret);
  167. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  168. out:
  169. mlog_exit(ret);
  170. return ret;
  171. }
  172. static int ocfs2_set_inode_size(handle_t *handle,
  173. struct inode *inode,
  174. struct buffer_head *fe_bh,
  175. u64 new_i_size)
  176. {
  177. int status;
  178. mlog_entry_void();
  179. i_size_write(inode, new_i_size);
  180. inode->i_blocks = ocfs2_inode_sector_count(inode);
  181. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  182. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  183. if (status < 0) {
  184. mlog_errno(status);
  185. goto bail;
  186. }
  187. bail:
  188. mlog_exit(status);
  189. return status;
  190. }
  191. static int ocfs2_simple_size_update(struct inode *inode,
  192. struct buffer_head *di_bh,
  193. u64 new_i_size)
  194. {
  195. int ret;
  196. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  197. handle_t *handle = NULL;
  198. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  199. if (handle == NULL) {
  200. ret = -ENOMEM;
  201. mlog_errno(ret);
  202. goto out;
  203. }
  204. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  205. new_i_size);
  206. if (ret < 0)
  207. mlog_errno(ret);
  208. ocfs2_commit_trans(osb, handle);
  209. out:
  210. return ret;
  211. }
  212. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  213. struct inode *inode,
  214. struct buffer_head *fe_bh,
  215. u64 new_i_size)
  216. {
  217. int status;
  218. handle_t *handle;
  219. struct ocfs2_dinode *di;
  220. mlog_entry_void();
  221. /* TODO: This needs to actually orphan the inode in this
  222. * transaction. */
  223. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  224. if (IS_ERR(handle)) {
  225. status = PTR_ERR(handle);
  226. mlog_errno(status);
  227. goto out;
  228. }
  229. status = ocfs2_journal_access(handle, inode, fe_bh,
  230. OCFS2_JOURNAL_ACCESS_WRITE);
  231. if (status < 0) {
  232. mlog_errno(status);
  233. goto out_commit;
  234. }
  235. /*
  236. * Do this before setting i_size.
  237. */
  238. status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
  239. if (status) {
  240. mlog_errno(status);
  241. goto out_commit;
  242. }
  243. i_size_write(inode, new_i_size);
  244. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  245. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  246. di = (struct ocfs2_dinode *) fe_bh->b_data;
  247. di->i_size = cpu_to_le64(new_i_size);
  248. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  249. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  250. status = ocfs2_journal_dirty(handle, fe_bh);
  251. if (status < 0)
  252. mlog_errno(status);
  253. out_commit:
  254. ocfs2_commit_trans(osb, handle);
  255. out:
  256. mlog_exit(status);
  257. return status;
  258. }
  259. static int ocfs2_truncate_file(struct inode *inode,
  260. struct buffer_head *di_bh,
  261. u64 new_i_size)
  262. {
  263. int status = 0;
  264. struct ocfs2_dinode *fe = NULL;
  265. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  266. struct ocfs2_truncate_context *tc = NULL;
  267. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  268. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  269. (unsigned long long)new_i_size);
  270. fe = (struct ocfs2_dinode *) di_bh->b_data;
  271. if (!OCFS2_IS_VALID_DINODE(fe)) {
  272. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  273. status = -EIO;
  274. goto bail;
  275. }
  276. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  277. "Inode %llu, inode i_size = %lld != di "
  278. "i_size = %llu, i_flags = 0x%x\n",
  279. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  280. i_size_read(inode),
  281. (unsigned long long)le64_to_cpu(fe->i_size),
  282. le32_to_cpu(fe->i_flags));
  283. if (new_i_size > le64_to_cpu(fe->i_size)) {
  284. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  285. (unsigned long long)le64_to_cpu(fe->i_size),
  286. (unsigned long long)new_i_size);
  287. status = -EINVAL;
  288. mlog_errno(status);
  289. goto bail;
  290. }
  291. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  292. (unsigned long long)le64_to_cpu(fe->i_blkno),
  293. (unsigned long long)le64_to_cpu(fe->i_size),
  294. (unsigned long long)new_i_size);
  295. /* lets handle the simple truncate cases before doing any more
  296. * cluster locking. */
  297. if (new_i_size == le64_to_cpu(fe->i_size))
  298. goto bail;
  299. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  300. /* This forces other nodes to sync and drop their pages. Do
  301. * this even if we have a truncate without allocation change -
  302. * ocfs2 cluster sizes can be much greater than page size, so
  303. * we have to truncate them anyway. */
  304. status = ocfs2_data_lock(inode, 1);
  305. if (status < 0) {
  306. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  307. mlog_errno(status);
  308. goto bail;
  309. }
  310. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  311. truncate_inode_pages(inode->i_mapping, new_i_size);
  312. /* alright, we're going to need to do a full blown alloc size
  313. * change. Orphan the inode so that recovery can complete the
  314. * truncate if necessary. This does the task of marking
  315. * i_size. */
  316. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  317. if (status < 0) {
  318. mlog_errno(status);
  319. goto bail_unlock_data;
  320. }
  321. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  322. if (status < 0) {
  323. mlog_errno(status);
  324. goto bail_unlock_data;
  325. }
  326. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  327. if (status < 0) {
  328. mlog_errno(status);
  329. goto bail_unlock_data;
  330. }
  331. /* TODO: orphan dir cleanup here. */
  332. bail_unlock_data:
  333. ocfs2_data_unlock(inode, 1);
  334. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  335. bail:
  336. mlog_exit(status);
  337. return status;
  338. }
  339. /*
  340. * extend allocation only here.
  341. * we'll update all the disk stuff, and oip->alloc_size
  342. *
  343. * expect stuff to be locked, a transaction started and enough data /
  344. * metadata reservations in the contexts.
  345. *
  346. * Will return -EAGAIN, and a reason if a restart is needed.
  347. * If passed in, *reason will always be set, even in error.
  348. */
  349. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  350. struct inode *inode,
  351. u32 *logical_offset,
  352. u32 clusters_to_add,
  353. struct buffer_head *fe_bh,
  354. handle_t *handle,
  355. struct ocfs2_alloc_context *data_ac,
  356. struct ocfs2_alloc_context *meta_ac,
  357. enum ocfs2_alloc_restarted *reason_ret)
  358. {
  359. int status = 0;
  360. int free_extents;
  361. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  362. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  363. u32 bit_off, num_bits;
  364. u64 block;
  365. BUG_ON(!clusters_to_add);
  366. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  367. if (free_extents < 0) {
  368. status = free_extents;
  369. mlog_errno(status);
  370. goto leave;
  371. }
  372. /* there are two cases which could cause us to EAGAIN in the
  373. * we-need-more-metadata case:
  374. * 1) we haven't reserved *any*
  375. * 2) we are so fragmented, we've needed to add metadata too
  376. * many times. */
  377. if (!free_extents && !meta_ac) {
  378. mlog(0, "we haven't reserved any metadata!\n");
  379. status = -EAGAIN;
  380. reason = RESTART_META;
  381. goto leave;
  382. } else if ((!free_extents)
  383. && (ocfs2_alloc_context_bits_left(meta_ac)
  384. < ocfs2_extend_meta_needed(fe))) {
  385. mlog(0, "filesystem is really fragmented...\n");
  386. status = -EAGAIN;
  387. reason = RESTART_META;
  388. goto leave;
  389. }
  390. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  391. &bit_off, &num_bits);
  392. if (status < 0) {
  393. if (status != -ENOSPC)
  394. mlog_errno(status);
  395. goto leave;
  396. }
  397. BUG_ON(num_bits > clusters_to_add);
  398. /* reserve our write early -- insert_extent may update the inode */
  399. status = ocfs2_journal_access(handle, inode, fe_bh,
  400. OCFS2_JOURNAL_ACCESS_WRITE);
  401. if (status < 0) {
  402. mlog_errno(status);
  403. goto leave;
  404. }
  405. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  406. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  407. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  408. status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
  409. *logical_offset, block, num_bits,
  410. meta_ac);
  411. if (status < 0) {
  412. mlog_errno(status);
  413. goto leave;
  414. }
  415. status = ocfs2_journal_dirty(handle, fe_bh);
  416. if (status < 0) {
  417. mlog_errno(status);
  418. goto leave;
  419. }
  420. clusters_to_add -= num_bits;
  421. *logical_offset += num_bits;
  422. if (clusters_to_add) {
  423. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  424. "%u\n", fe->i_clusters, clusters_to_add);
  425. status = -EAGAIN;
  426. reason = RESTART_TRANS;
  427. }
  428. leave:
  429. mlog_exit(status);
  430. if (reason_ret)
  431. *reason_ret = reason;
  432. return status;
  433. }
  434. /*
  435. * For a given allocation, determine which allocators will need to be
  436. * accessed, and lock them, reserving the appropriate number of bits.
  437. *
  438. * Called from ocfs2_extend_allocation() for file systems which don't
  439. * support holes, and from ocfs2_write() for file systems which
  440. * understand sparse inodes.
  441. */
  442. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
  443. u32 clusters_to_add,
  444. struct ocfs2_alloc_context **data_ac,
  445. struct ocfs2_alloc_context **meta_ac)
  446. {
  447. int ret, num_free_extents;
  448. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  449. *meta_ac = NULL;
  450. *data_ac = NULL;
  451. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  452. "clusters_to_add = %u\n",
  453. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  454. le32_to_cpu(di->i_clusters), clusters_to_add);
  455. num_free_extents = ocfs2_num_free_extents(osb, inode, di);
  456. if (num_free_extents < 0) {
  457. ret = num_free_extents;
  458. mlog_errno(ret);
  459. goto out;
  460. }
  461. /*
  462. * Sparse allocation file systems need to be more conservative
  463. * with reserving room for expansion - the actual allocation
  464. * happens while we've got a journal handle open so re-taking
  465. * a cluster lock (because we ran out of room for another
  466. * extent) will violate ordering rules.
  467. *
  468. * Most of the time we'll only be seeing this 1 cluster at a time
  469. * anyway.
  470. */
  471. if (!num_free_extents ||
  472. (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
  473. ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
  474. if (ret < 0) {
  475. if (ret != -ENOSPC)
  476. mlog_errno(ret);
  477. goto out;
  478. }
  479. }
  480. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  481. if (ret < 0) {
  482. if (ret != -ENOSPC)
  483. mlog_errno(ret);
  484. goto out;
  485. }
  486. out:
  487. if (ret) {
  488. if (*meta_ac) {
  489. ocfs2_free_alloc_context(*meta_ac);
  490. *meta_ac = NULL;
  491. }
  492. /*
  493. * We cannot have an error and a non null *data_ac.
  494. */
  495. }
  496. return ret;
  497. }
  498. static int ocfs2_extend_allocation(struct inode *inode,
  499. u32 clusters_to_add)
  500. {
  501. int status = 0;
  502. int restart_func = 0;
  503. int drop_alloc_sem = 0;
  504. int credits;
  505. u32 prev_clusters, logical_start;
  506. struct buffer_head *bh = NULL;
  507. struct ocfs2_dinode *fe = NULL;
  508. handle_t *handle = NULL;
  509. struct ocfs2_alloc_context *data_ac = NULL;
  510. struct ocfs2_alloc_context *meta_ac = NULL;
  511. enum ocfs2_alloc_restarted why;
  512. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  513. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  514. /*
  515. * This function only exists for file systems which don't
  516. * support holes.
  517. */
  518. BUG_ON(ocfs2_sparse_alloc(osb));
  519. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  520. OCFS2_BH_CACHED, inode);
  521. if (status < 0) {
  522. mlog_errno(status);
  523. goto leave;
  524. }
  525. fe = (struct ocfs2_dinode *) bh->b_data;
  526. if (!OCFS2_IS_VALID_DINODE(fe)) {
  527. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  528. status = -EIO;
  529. goto leave;
  530. }
  531. logical_start = OCFS2_I(inode)->ip_clusters;
  532. restart_all:
  533. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  534. /* blocks peope in read/write from reading our allocation
  535. * until we're done changing it. We depend on i_mutex to block
  536. * other extend/truncate calls while we're here. Ordering wrt
  537. * start_trans is important here -- always do it before! */
  538. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  539. drop_alloc_sem = 1;
  540. status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
  541. &meta_ac);
  542. if (status) {
  543. mlog_errno(status);
  544. goto leave;
  545. }
  546. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  547. handle = ocfs2_start_trans(osb, credits);
  548. if (IS_ERR(handle)) {
  549. status = PTR_ERR(handle);
  550. handle = NULL;
  551. mlog_errno(status);
  552. goto leave;
  553. }
  554. restarted_transaction:
  555. /* reserve a write to the file entry early on - that we if we
  556. * run out of credits in the allocation path, we can still
  557. * update i_size. */
  558. status = ocfs2_journal_access(handle, inode, bh,
  559. OCFS2_JOURNAL_ACCESS_WRITE);
  560. if (status < 0) {
  561. mlog_errno(status);
  562. goto leave;
  563. }
  564. prev_clusters = OCFS2_I(inode)->ip_clusters;
  565. status = ocfs2_do_extend_allocation(osb,
  566. inode,
  567. &logical_start,
  568. clusters_to_add,
  569. bh,
  570. handle,
  571. data_ac,
  572. meta_ac,
  573. &why);
  574. if ((status < 0) && (status != -EAGAIN)) {
  575. if (status != -ENOSPC)
  576. mlog_errno(status);
  577. goto leave;
  578. }
  579. status = ocfs2_journal_dirty(handle, bh);
  580. if (status < 0) {
  581. mlog_errno(status);
  582. goto leave;
  583. }
  584. spin_lock(&OCFS2_I(inode)->ip_lock);
  585. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  586. spin_unlock(&OCFS2_I(inode)->ip_lock);
  587. if (why != RESTART_NONE && clusters_to_add) {
  588. if (why == RESTART_META) {
  589. mlog(0, "restarting function.\n");
  590. restart_func = 1;
  591. } else {
  592. BUG_ON(why != RESTART_TRANS);
  593. mlog(0, "restarting transaction.\n");
  594. /* TODO: This can be more intelligent. */
  595. credits = ocfs2_calc_extend_credits(osb->sb,
  596. fe,
  597. clusters_to_add);
  598. status = ocfs2_extend_trans(handle, credits);
  599. if (status < 0) {
  600. /* handle still has to be committed at
  601. * this point. */
  602. status = -ENOMEM;
  603. mlog_errno(status);
  604. goto leave;
  605. }
  606. goto restarted_transaction;
  607. }
  608. }
  609. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  610. le32_to_cpu(fe->i_clusters),
  611. (unsigned long long)le64_to_cpu(fe->i_size));
  612. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  613. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  614. leave:
  615. if (drop_alloc_sem) {
  616. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  617. drop_alloc_sem = 0;
  618. }
  619. if (handle) {
  620. ocfs2_commit_trans(osb, handle);
  621. handle = NULL;
  622. }
  623. if (data_ac) {
  624. ocfs2_free_alloc_context(data_ac);
  625. data_ac = NULL;
  626. }
  627. if (meta_ac) {
  628. ocfs2_free_alloc_context(meta_ac);
  629. meta_ac = NULL;
  630. }
  631. if ((!status) && restart_func) {
  632. restart_func = 0;
  633. goto restart_all;
  634. }
  635. if (bh) {
  636. brelse(bh);
  637. bh = NULL;
  638. }
  639. mlog_exit(status);
  640. return status;
  641. }
  642. /* Some parts of this taken from generic_cont_expand, which turned out
  643. * to be too fragile to do exactly what we need without us having to
  644. * worry about recursive locking in ->prepare_write() and
  645. * ->commit_write(). */
  646. static int ocfs2_write_zero_page(struct inode *inode,
  647. u64 size)
  648. {
  649. struct address_space *mapping = inode->i_mapping;
  650. struct page *page;
  651. unsigned long index;
  652. unsigned int offset;
  653. handle_t *handle = NULL;
  654. int ret;
  655. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  656. /* ugh. in prepare/commit_write, if from==to==start of block, we
  657. ** skip the prepare. make sure we never send an offset for the start
  658. ** of a block
  659. */
  660. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  661. offset++;
  662. }
  663. index = size >> PAGE_CACHE_SHIFT;
  664. page = grab_cache_page(mapping, index);
  665. if (!page) {
  666. ret = -ENOMEM;
  667. mlog_errno(ret);
  668. goto out;
  669. }
  670. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  671. if (ret < 0) {
  672. mlog_errno(ret);
  673. goto out_unlock;
  674. }
  675. if (ocfs2_should_order_data(inode)) {
  676. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  677. offset);
  678. if (IS_ERR(handle)) {
  679. ret = PTR_ERR(handle);
  680. handle = NULL;
  681. goto out_unlock;
  682. }
  683. }
  684. /* must not update i_size! */
  685. ret = block_commit_write(page, offset, offset);
  686. if (ret < 0)
  687. mlog_errno(ret);
  688. else
  689. ret = 0;
  690. if (handle)
  691. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  692. out_unlock:
  693. unlock_page(page);
  694. page_cache_release(page);
  695. out:
  696. return ret;
  697. }
  698. static int ocfs2_zero_extend(struct inode *inode,
  699. u64 zero_to_size)
  700. {
  701. int ret = 0;
  702. u64 start_off;
  703. struct super_block *sb = inode->i_sb;
  704. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  705. while (start_off < zero_to_size) {
  706. ret = ocfs2_write_zero_page(inode, start_off);
  707. if (ret < 0) {
  708. mlog_errno(ret);
  709. goto out;
  710. }
  711. start_off += sb->s_blocksize;
  712. /*
  713. * Very large extends have the potential to lock up
  714. * the cpu for extended periods of time.
  715. */
  716. cond_resched();
  717. }
  718. out:
  719. return ret;
  720. }
  721. /*
  722. * A tail_to_skip value > 0 indicates that we're being called from
  723. * ocfs2_file_aio_write(). This has the following implications:
  724. *
  725. * - we don't want to update i_size
  726. * - di_bh will be NULL, which is fine because it's only used in the
  727. * case where we want to update i_size.
  728. * - ocfs2_zero_extend() will then only be filling the hole created
  729. * between i_size and the start of the write.
  730. */
  731. static int ocfs2_extend_file(struct inode *inode,
  732. struct buffer_head *di_bh,
  733. u64 new_i_size,
  734. size_t tail_to_skip)
  735. {
  736. int ret = 0;
  737. u32 clusters_to_add = 0;
  738. BUG_ON(!tail_to_skip && !di_bh);
  739. /* setattr sometimes calls us like this. */
  740. if (new_i_size == 0)
  741. goto out;
  742. if (i_size_read(inode) == new_i_size)
  743. goto out;
  744. BUG_ON(new_i_size < i_size_read(inode));
  745. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  746. BUG_ON(tail_to_skip != 0);
  747. goto out_update_size;
  748. }
  749. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  750. OCFS2_I(inode)->ip_clusters;
  751. /*
  752. * protect the pages that ocfs2_zero_extend is going to be
  753. * pulling into the page cache.. we do this before the
  754. * metadata extend so that we don't get into the situation
  755. * where we've extended the metadata but can't get the data
  756. * lock to zero.
  757. */
  758. ret = ocfs2_data_lock(inode, 1);
  759. if (ret < 0) {
  760. mlog_errno(ret);
  761. goto out;
  762. }
  763. if (clusters_to_add) {
  764. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  765. if (ret < 0) {
  766. mlog_errno(ret);
  767. goto out_unlock;
  768. }
  769. }
  770. /*
  771. * Call this even if we don't add any clusters to the tree. We
  772. * still need to zero the area between the old i_size and the
  773. * new i_size.
  774. */
  775. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  776. if (ret < 0) {
  777. mlog_errno(ret);
  778. goto out_unlock;
  779. }
  780. out_update_size:
  781. if (!tail_to_skip) {
  782. /* We're being called from ocfs2_setattr() which wants
  783. * us to update i_size */
  784. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  785. if (ret < 0)
  786. mlog_errno(ret);
  787. }
  788. out_unlock:
  789. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  790. ocfs2_data_unlock(inode, 1);
  791. out:
  792. return ret;
  793. }
  794. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  795. {
  796. int status = 0, size_change;
  797. struct inode *inode = dentry->d_inode;
  798. struct super_block *sb = inode->i_sb;
  799. struct ocfs2_super *osb = OCFS2_SB(sb);
  800. struct buffer_head *bh = NULL;
  801. handle_t *handle = NULL;
  802. mlog_entry("(0x%p, '%.*s')\n", dentry,
  803. dentry->d_name.len, dentry->d_name.name);
  804. if (attr->ia_valid & ATTR_MODE)
  805. mlog(0, "mode change: %d\n", attr->ia_mode);
  806. if (attr->ia_valid & ATTR_UID)
  807. mlog(0, "uid change: %d\n", attr->ia_uid);
  808. if (attr->ia_valid & ATTR_GID)
  809. mlog(0, "gid change: %d\n", attr->ia_gid);
  810. if (attr->ia_valid & ATTR_SIZE)
  811. mlog(0, "size change...\n");
  812. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  813. mlog(0, "time change...\n");
  814. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  815. | ATTR_GID | ATTR_UID | ATTR_MODE)
  816. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  817. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  818. return 0;
  819. }
  820. status = inode_change_ok(inode, attr);
  821. if (status)
  822. return status;
  823. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  824. if (size_change) {
  825. status = ocfs2_rw_lock(inode, 1);
  826. if (status < 0) {
  827. mlog_errno(status);
  828. goto bail;
  829. }
  830. }
  831. status = ocfs2_meta_lock(inode, &bh, 1);
  832. if (status < 0) {
  833. if (status != -ENOENT)
  834. mlog_errno(status);
  835. goto bail_unlock_rw;
  836. }
  837. if (size_change && attr->ia_size != i_size_read(inode)) {
  838. if (i_size_read(inode) > attr->ia_size)
  839. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  840. else
  841. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  842. if (status < 0) {
  843. if (status != -ENOSPC)
  844. mlog_errno(status);
  845. status = -ENOSPC;
  846. goto bail_unlock;
  847. }
  848. }
  849. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  850. if (IS_ERR(handle)) {
  851. status = PTR_ERR(handle);
  852. mlog_errno(status);
  853. goto bail_unlock;
  854. }
  855. /*
  856. * This will intentionally not wind up calling vmtruncate(),
  857. * since all the work for a size change has been done above.
  858. * Otherwise, we could get into problems with truncate as
  859. * ip_alloc_sem is used there to protect against i_size
  860. * changes.
  861. */
  862. status = inode_setattr(inode, attr);
  863. if (status < 0) {
  864. mlog_errno(status);
  865. goto bail_commit;
  866. }
  867. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  868. if (status < 0)
  869. mlog_errno(status);
  870. bail_commit:
  871. ocfs2_commit_trans(osb, handle);
  872. bail_unlock:
  873. ocfs2_meta_unlock(inode, 1);
  874. bail_unlock_rw:
  875. if (size_change)
  876. ocfs2_rw_unlock(inode, 1);
  877. bail:
  878. if (bh)
  879. brelse(bh);
  880. mlog_exit(status);
  881. return status;
  882. }
  883. int ocfs2_getattr(struct vfsmount *mnt,
  884. struct dentry *dentry,
  885. struct kstat *stat)
  886. {
  887. struct inode *inode = dentry->d_inode;
  888. struct super_block *sb = dentry->d_inode->i_sb;
  889. struct ocfs2_super *osb = sb->s_fs_info;
  890. int err;
  891. mlog_entry_void();
  892. err = ocfs2_inode_revalidate(dentry);
  893. if (err) {
  894. if (err != -ENOENT)
  895. mlog_errno(err);
  896. goto bail;
  897. }
  898. generic_fillattr(inode, stat);
  899. /* We set the blksize from the cluster size for performance */
  900. stat->blksize = osb->s_clustersize;
  901. bail:
  902. mlog_exit(err);
  903. return err;
  904. }
  905. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  906. {
  907. int ret;
  908. mlog_entry_void();
  909. ret = ocfs2_meta_lock(inode, NULL, 0);
  910. if (ret) {
  911. if (ret != -ENOENT)
  912. mlog_errno(ret);
  913. goto out;
  914. }
  915. ret = generic_permission(inode, mask, NULL);
  916. ocfs2_meta_unlock(inode, 0);
  917. out:
  918. mlog_exit(ret);
  919. return ret;
  920. }
  921. static int ocfs2_write_remove_suid(struct inode *inode)
  922. {
  923. int ret;
  924. struct buffer_head *bh = NULL;
  925. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  926. handle_t *handle;
  927. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  928. struct ocfs2_dinode *di;
  929. mlog_entry("(Inode %llu, mode 0%o)\n",
  930. (unsigned long long)oi->ip_blkno, inode->i_mode);
  931. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  932. if (handle == NULL) {
  933. ret = -ENOMEM;
  934. mlog_errno(ret);
  935. goto out;
  936. }
  937. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  938. if (ret < 0) {
  939. mlog_errno(ret);
  940. goto out_trans;
  941. }
  942. ret = ocfs2_journal_access(handle, inode, bh,
  943. OCFS2_JOURNAL_ACCESS_WRITE);
  944. if (ret < 0) {
  945. mlog_errno(ret);
  946. goto out_bh;
  947. }
  948. inode->i_mode &= ~S_ISUID;
  949. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  950. inode->i_mode &= ~S_ISGID;
  951. di = (struct ocfs2_dinode *) bh->b_data;
  952. di->i_mode = cpu_to_le16(inode->i_mode);
  953. ret = ocfs2_journal_dirty(handle, bh);
  954. if (ret < 0)
  955. mlog_errno(ret);
  956. out_bh:
  957. brelse(bh);
  958. out_trans:
  959. ocfs2_commit_trans(osb, handle);
  960. out:
  961. mlog_exit(ret);
  962. return ret;
  963. }
  964. /*
  965. * Will look for holes and unwritten extents in the range starting at
  966. * pos for count bytes (inclusive).
  967. */
  968. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  969. size_t count)
  970. {
  971. int ret = 0;
  972. unsigned int extent_flags;
  973. u32 cpos, clusters, extent_len, phys_cpos;
  974. struct super_block *sb = inode->i_sb;
  975. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  976. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  977. while (clusters) {
  978. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  979. &extent_flags);
  980. if (ret < 0) {
  981. mlog_errno(ret);
  982. goto out;
  983. }
  984. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  985. ret = 1;
  986. break;
  987. }
  988. if (extent_len > clusters)
  989. extent_len = clusters;
  990. clusters -= extent_len;
  991. cpos += extent_len;
  992. }
  993. out:
  994. return ret;
  995. }
  996. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  997. loff_t *ppos,
  998. size_t count,
  999. int appending,
  1000. int *direct_io)
  1001. {
  1002. int ret = 0, meta_level = appending;
  1003. struct inode *inode = dentry->d_inode;
  1004. u32 clusters;
  1005. loff_t newsize, saved_pos;
  1006. /*
  1007. * We sample i_size under a read level meta lock to see if our write
  1008. * is extending the file, if it is we back off and get a write level
  1009. * meta lock.
  1010. */
  1011. for(;;) {
  1012. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1013. if (ret < 0) {
  1014. meta_level = -1;
  1015. mlog_errno(ret);
  1016. goto out;
  1017. }
  1018. /* Clear suid / sgid if necessary. We do this here
  1019. * instead of later in the write path because
  1020. * remove_suid() calls ->setattr without any hint that
  1021. * we may have already done our cluster locking. Since
  1022. * ocfs2_setattr() *must* take cluster locks to
  1023. * proceeed, this will lead us to recursively lock the
  1024. * inode. There's also the dinode i_size state which
  1025. * can be lost via setattr during extending writes (we
  1026. * set inode->i_size at the end of a write. */
  1027. if (should_remove_suid(dentry)) {
  1028. if (meta_level == 0) {
  1029. ocfs2_meta_unlock(inode, meta_level);
  1030. meta_level = 1;
  1031. continue;
  1032. }
  1033. ret = ocfs2_write_remove_suid(inode);
  1034. if (ret < 0) {
  1035. mlog_errno(ret);
  1036. goto out_unlock;
  1037. }
  1038. }
  1039. /* work on a copy of ppos until we're sure that we won't have
  1040. * to recalculate it due to relocking. */
  1041. if (appending) {
  1042. saved_pos = i_size_read(inode);
  1043. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1044. } else {
  1045. saved_pos = *ppos;
  1046. }
  1047. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  1048. loff_t end = saved_pos + count;
  1049. /*
  1050. * Skip the O_DIRECT checks if we don't need
  1051. * them.
  1052. */
  1053. if (!direct_io || !(*direct_io))
  1054. break;
  1055. /*
  1056. * Allowing concurrent direct writes means
  1057. * i_size changes wouldn't be synchronized, so
  1058. * one node could wind up truncating another
  1059. * nodes writes.
  1060. */
  1061. if (end > i_size_read(inode)) {
  1062. *direct_io = 0;
  1063. break;
  1064. }
  1065. /*
  1066. * We don't fill holes during direct io, so
  1067. * check for them here. If any are found, the
  1068. * caller will have to retake some cluster
  1069. * locks and initiate the io as buffered.
  1070. */
  1071. ret = ocfs2_check_range_for_holes(inode, saved_pos,
  1072. count);
  1073. if (ret == 1) {
  1074. *direct_io = 0;
  1075. ret = 0;
  1076. } else if (ret < 0)
  1077. mlog_errno(ret);
  1078. break;
  1079. }
  1080. /*
  1081. * The rest of this loop is concerned with legacy file
  1082. * systems which don't support sparse files.
  1083. */
  1084. newsize = count + saved_pos;
  1085. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  1086. (long long) saved_pos, (long long) newsize,
  1087. (long long) i_size_read(inode));
  1088. /* No need for a higher level metadata lock if we're
  1089. * never going past i_size. */
  1090. if (newsize <= i_size_read(inode))
  1091. break;
  1092. if (meta_level == 0) {
  1093. ocfs2_meta_unlock(inode, meta_level);
  1094. meta_level = 1;
  1095. continue;
  1096. }
  1097. spin_lock(&OCFS2_I(inode)->ip_lock);
  1098. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  1099. OCFS2_I(inode)->ip_clusters;
  1100. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1101. mlog(0, "Writing at EOF, may need more allocation: "
  1102. "i_size = %lld, newsize = %lld, need %u clusters\n",
  1103. (long long) i_size_read(inode), (long long) newsize,
  1104. clusters);
  1105. /* We only want to continue the rest of this loop if
  1106. * our extend will actually require more
  1107. * allocation. */
  1108. if (!clusters)
  1109. break;
  1110. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  1111. if (ret < 0) {
  1112. if (ret != -ENOSPC)
  1113. mlog_errno(ret);
  1114. goto out_unlock;
  1115. }
  1116. break;
  1117. }
  1118. if (appending)
  1119. *ppos = saved_pos;
  1120. out_unlock:
  1121. ocfs2_meta_unlock(inode, meta_level);
  1122. out:
  1123. return ret;
  1124. }
  1125. static inline void
  1126. ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  1127. {
  1128. const struct iovec *iov = *iovp;
  1129. size_t base = *basep;
  1130. do {
  1131. int copy = min(bytes, iov->iov_len - base);
  1132. bytes -= copy;
  1133. base += copy;
  1134. if (iov->iov_len == base) {
  1135. iov++;
  1136. base = 0;
  1137. }
  1138. } while (bytes);
  1139. *iovp = iov;
  1140. *basep = base;
  1141. }
  1142. static struct page * ocfs2_get_write_source(char **ret_src_buf,
  1143. const struct iovec *cur_iov,
  1144. size_t iov_offset)
  1145. {
  1146. int ret;
  1147. char *buf = cur_iov->iov_base + iov_offset;
  1148. struct page *src_page = NULL;
  1149. unsigned long off;
  1150. off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
  1151. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1152. /*
  1153. * Pull in the user page. We want to do this outside
  1154. * of the meta data locks in order to preserve locking
  1155. * order in case of page fault.
  1156. */
  1157. ret = get_user_pages(current, current->mm,
  1158. (unsigned long)buf & PAGE_CACHE_MASK, 1,
  1159. 0, 0, &src_page, NULL);
  1160. if (ret == 1)
  1161. *ret_src_buf = kmap(src_page) + off;
  1162. else
  1163. src_page = ERR_PTR(-EFAULT);
  1164. } else {
  1165. *ret_src_buf = buf;
  1166. }
  1167. return src_page;
  1168. }
  1169. static void ocfs2_put_write_source(struct page *page)
  1170. {
  1171. if (page) {
  1172. kunmap(page);
  1173. page_cache_release(page);
  1174. }
  1175. }
  1176. static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
  1177. const struct iovec *iov,
  1178. unsigned long nr_segs,
  1179. size_t count,
  1180. ssize_t o_direct_written)
  1181. {
  1182. int ret = 0;
  1183. ssize_t copied, total = 0;
  1184. size_t iov_offset = 0, bytes;
  1185. loff_t pos;
  1186. const struct iovec *cur_iov = iov;
  1187. struct page *user_page, *page;
  1188. char *buf, *dst;
  1189. void *fsdata;
  1190. /*
  1191. * handle partial DIO write. Adjust cur_iov if needed.
  1192. */
  1193. ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
  1194. do {
  1195. pos = *ppos;
  1196. user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
  1197. if (IS_ERR(user_page)) {
  1198. ret = PTR_ERR(user_page);
  1199. goto out;
  1200. }
  1201. /* Stay within our page boundaries */
  1202. bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
  1203. (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
  1204. /* Stay within the vector boundary */
  1205. bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
  1206. /* Stay within count */
  1207. bytes = min(bytes, count);
  1208. page = NULL;
  1209. ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
  1210. &page, &fsdata);
  1211. if (ret) {
  1212. mlog_errno(ret);
  1213. goto out;
  1214. }
  1215. dst = kmap_atomic(page, KM_USER0);
  1216. memcpy(dst + (pos & (PAGE_CACHE_SIZE - 1)), buf, bytes);
  1217. kunmap_atomic(dst, KM_USER0);
  1218. flush_dcache_page(page);
  1219. ocfs2_put_write_source(user_page);
  1220. copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
  1221. bytes, page, fsdata);
  1222. if (copied < 0) {
  1223. mlog_errno(copied);
  1224. ret = copied;
  1225. goto out;
  1226. }
  1227. total += copied;
  1228. *ppos = pos + copied;
  1229. count -= copied;
  1230. ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
  1231. } while(count);
  1232. out:
  1233. return total ? total : ret;
  1234. }
  1235. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1236. const struct iovec *iov,
  1237. unsigned long nr_segs,
  1238. loff_t pos)
  1239. {
  1240. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1241. int can_do_direct, sync = 0;
  1242. ssize_t written = 0;
  1243. size_t ocount; /* original count */
  1244. size_t count; /* after file limit checks */
  1245. loff_t *ppos = &iocb->ki_pos;
  1246. struct file *file = iocb->ki_filp;
  1247. struct inode *inode = file->f_path.dentry->d_inode;
  1248. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1249. (unsigned int)nr_segs,
  1250. file->f_path.dentry->d_name.len,
  1251. file->f_path.dentry->d_name.name);
  1252. if (iocb->ki_left == 0)
  1253. return 0;
  1254. ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1255. if (ret)
  1256. return ret;
  1257. count = ocount;
  1258. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1259. appending = file->f_flags & O_APPEND ? 1 : 0;
  1260. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1261. mutex_lock(&inode->i_mutex);
  1262. relock:
  1263. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1264. if (direct_io) {
  1265. down_read(&inode->i_alloc_sem);
  1266. have_alloc_sem = 1;
  1267. }
  1268. /* concurrent O_DIRECT writes are allowed */
  1269. rw_level = !direct_io;
  1270. ret = ocfs2_rw_lock(inode, rw_level);
  1271. if (ret < 0) {
  1272. mlog_errno(ret);
  1273. goto out_sems;
  1274. }
  1275. can_do_direct = direct_io;
  1276. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1277. iocb->ki_left, appending,
  1278. &can_do_direct);
  1279. if (ret < 0) {
  1280. mlog_errno(ret);
  1281. goto out;
  1282. }
  1283. /*
  1284. * We can't complete the direct I/O as requested, fall back to
  1285. * buffered I/O.
  1286. */
  1287. if (direct_io && !can_do_direct) {
  1288. ocfs2_rw_unlock(inode, rw_level);
  1289. up_read(&inode->i_alloc_sem);
  1290. have_alloc_sem = 0;
  1291. rw_level = -1;
  1292. direct_io = 0;
  1293. sync = 1;
  1294. goto relock;
  1295. }
  1296. if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
  1297. sync = 1;
  1298. /*
  1299. * XXX: Is it ok to execute these checks a second time?
  1300. */
  1301. ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
  1302. if (ret)
  1303. goto out;
  1304. /*
  1305. * Set pos so that sync_page_range_nolock() below understands
  1306. * where to start from. We might've moved it around via the
  1307. * calls above. The range we want to actually sync starts from
  1308. * *ppos here.
  1309. *
  1310. */
  1311. pos = *ppos;
  1312. /* communicate with ocfs2_dio_end_io */
  1313. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1314. if (direct_io) {
  1315. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1316. ppos, count, ocount);
  1317. if (written < 0) {
  1318. ret = written;
  1319. goto out_dio;
  1320. }
  1321. } else {
  1322. written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
  1323. count, written);
  1324. if (written < 0) {
  1325. ret = written;
  1326. if (ret != -EFAULT || ret != -ENOSPC)
  1327. mlog_errno(ret);
  1328. goto out;
  1329. }
  1330. }
  1331. out_dio:
  1332. /* buffered aio wouldn't have proper lock coverage today */
  1333. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1334. /*
  1335. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1336. * function pointer which is called when o_direct io completes so that
  1337. * it can unlock our rw lock. (it's the clustered equivalent of
  1338. * i_alloc_sem; protects truncate from racing with pending ios).
  1339. * Unfortunately there are error cases which call end_io and others
  1340. * that don't. so we don't have to unlock the rw_lock if either an
  1341. * async dio is going to do it in the future or an end_io after an
  1342. * error has already done it.
  1343. */
  1344. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1345. rw_level = -1;
  1346. have_alloc_sem = 0;
  1347. }
  1348. out:
  1349. if (rw_level != -1)
  1350. ocfs2_rw_unlock(inode, rw_level);
  1351. out_sems:
  1352. if (have_alloc_sem)
  1353. up_read(&inode->i_alloc_sem);
  1354. if (written > 0 && sync) {
  1355. ssize_t err;
  1356. err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
  1357. if (err < 0)
  1358. written = err;
  1359. }
  1360. mutex_unlock(&inode->i_mutex);
  1361. mlog_exit(ret);
  1362. return written ? written : ret;
  1363. }
  1364. static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
  1365. struct pipe_buffer *buf,
  1366. struct splice_desc *sd)
  1367. {
  1368. int ret, count;
  1369. ssize_t copied = 0;
  1370. struct file *file = sd->u.file;
  1371. unsigned int offset;
  1372. struct page *page = NULL;
  1373. void *fsdata;
  1374. char *src, *dst;
  1375. ret = buf->ops->confirm(pipe, buf);
  1376. if (ret)
  1377. goto out;
  1378. offset = sd->pos & ~PAGE_CACHE_MASK;
  1379. count = sd->len;
  1380. if (count + offset > PAGE_CACHE_SIZE)
  1381. count = PAGE_CACHE_SIZE - offset;
  1382. ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
  1383. &page, &fsdata);
  1384. if (ret) {
  1385. mlog_errno(ret);
  1386. goto out;
  1387. }
  1388. src = buf->ops->map(pipe, buf, 1);
  1389. dst = kmap_atomic(page, KM_USER1);
  1390. memcpy(dst + offset, src + buf->offset, count);
  1391. kunmap_atomic(page, KM_USER1);
  1392. buf->ops->unmap(pipe, buf, src);
  1393. copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
  1394. page, fsdata);
  1395. if (copied < 0) {
  1396. mlog_errno(copied);
  1397. ret = copied;
  1398. goto out;
  1399. }
  1400. out:
  1401. return copied ? copied : ret;
  1402. }
  1403. static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1404. struct file *out,
  1405. loff_t *ppos,
  1406. size_t len,
  1407. unsigned int flags)
  1408. {
  1409. int ret, err;
  1410. struct address_space *mapping = out->f_mapping;
  1411. struct inode *inode = mapping->host;
  1412. struct splice_desc sd = {
  1413. .total_len = len,
  1414. .flags = flags,
  1415. .pos = *ppos,
  1416. .u.file = out,
  1417. };
  1418. ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
  1419. if (ret > 0) {
  1420. *ppos += ret;
  1421. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1422. err = generic_osync_inode(inode, mapping,
  1423. OSYNC_METADATA|OSYNC_DATA);
  1424. if (err)
  1425. ret = err;
  1426. }
  1427. }
  1428. return ret;
  1429. }
  1430. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1431. struct file *out,
  1432. loff_t *ppos,
  1433. size_t len,
  1434. unsigned int flags)
  1435. {
  1436. int ret;
  1437. struct inode *inode = out->f_path.dentry->d_inode;
  1438. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1439. (unsigned int)len,
  1440. out->f_path.dentry->d_name.len,
  1441. out->f_path.dentry->d_name.name);
  1442. inode_double_lock(inode, pipe->inode);
  1443. ret = ocfs2_rw_lock(inode, 1);
  1444. if (ret < 0) {
  1445. mlog_errno(ret);
  1446. goto out;
  1447. }
  1448. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1449. NULL);
  1450. if (ret < 0) {
  1451. mlog_errno(ret);
  1452. goto out_unlock;
  1453. }
  1454. /* ok, we're done with i_size and alloc work */
  1455. ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
  1456. out_unlock:
  1457. ocfs2_rw_unlock(inode, 1);
  1458. out:
  1459. inode_double_unlock(inode, pipe->inode);
  1460. mlog_exit(ret);
  1461. return ret;
  1462. }
  1463. static ssize_t ocfs2_file_splice_read(struct file *in,
  1464. loff_t *ppos,
  1465. struct pipe_inode_info *pipe,
  1466. size_t len,
  1467. unsigned int flags)
  1468. {
  1469. int ret = 0;
  1470. struct inode *inode = in->f_path.dentry->d_inode;
  1471. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1472. (unsigned int)len,
  1473. in->f_path.dentry->d_name.len,
  1474. in->f_path.dentry->d_name.name);
  1475. /*
  1476. * See the comment in ocfs2_file_aio_read()
  1477. */
  1478. ret = ocfs2_meta_lock(inode, NULL, 0);
  1479. if (ret < 0) {
  1480. mlog_errno(ret);
  1481. goto bail;
  1482. }
  1483. ocfs2_meta_unlock(inode, 0);
  1484. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1485. bail:
  1486. mlog_exit(ret);
  1487. return ret;
  1488. }
  1489. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1490. const struct iovec *iov,
  1491. unsigned long nr_segs,
  1492. loff_t pos)
  1493. {
  1494. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1495. struct file *filp = iocb->ki_filp;
  1496. struct inode *inode = filp->f_path.dentry->d_inode;
  1497. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1498. (unsigned int)nr_segs,
  1499. filp->f_path.dentry->d_name.len,
  1500. filp->f_path.dentry->d_name.name);
  1501. if (!inode) {
  1502. ret = -EINVAL;
  1503. mlog_errno(ret);
  1504. goto bail;
  1505. }
  1506. /*
  1507. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1508. * need locks to protect pending reads from racing with truncate.
  1509. */
  1510. if (filp->f_flags & O_DIRECT) {
  1511. down_read(&inode->i_alloc_sem);
  1512. have_alloc_sem = 1;
  1513. ret = ocfs2_rw_lock(inode, 0);
  1514. if (ret < 0) {
  1515. mlog_errno(ret);
  1516. goto bail;
  1517. }
  1518. rw_level = 0;
  1519. /* communicate with ocfs2_dio_end_io */
  1520. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1521. }
  1522. /*
  1523. * We're fine letting folks race truncates and extending
  1524. * writes with read across the cluster, just like they can
  1525. * locally. Hence no rw_lock during read.
  1526. *
  1527. * Take and drop the meta data lock to update inode fields
  1528. * like i_size. This allows the checks down below
  1529. * generic_file_aio_read() a chance of actually working.
  1530. */
  1531. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1532. if (ret < 0) {
  1533. mlog_errno(ret);
  1534. goto bail;
  1535. }
  1536. ocfs2_meta_unlock(inode, lock_level);
  1537. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1538. if (ret == -EINVAL)
  1539. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1540. /* buffered aio wouldn't have proper lock coverage today */
  1541. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1542. /* see ocfs2_file_aio_write */
  1543. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1544. rw_level = -1;
  1545. have_alloc_sem = 0;
  1546. }
  1547. bail:
  1548. if (have_alloc_sem)
  1549. up_read(&inode->i_alloc_sem);
  1550. if (rw_level != -1)
  1551. ocfs2_rw_unlock(inode, rw_level);
  1552. mlog_exit(ret);
  1553. return ret;
  1554. }
  1555. const struct inode_operations ocfs2_file_iops = {
  1556. .setattr = ocfs2_setattr,
  1557. .getattr = ocfs2_getattr,
  1558. .permission = ocfs2_permission,
  1559. };
  1560. const struct inode_operations ocfs2_special_file_iops = {
  1561. .setattr = ocfs2_setattr,
  1562. .getattr = ocfs2_getattr,
  1563. .permission = ocfs2_permission,
  1564. };
  1565. const struct file_operations ocfs2_fops = {
  1566. .read = do_sync_read,
  1567. .write = do_sync_write,
  1568. .mmap = ocfs2_mmap,
  1569. .fsync = ocfs2_sync_file,
  1570. .release = ocfs2_file_release,
  1571. .open = ocfs2_file_open,
  1572. .aio_read = ocfs2_file_aio_read,
  1573. .aio_write = ocfs2_file_aio_write,
  1574. .ioctl = ocfs2_ioctl,
  1575. #ifdef CONFIG_COMPAT
  1576. .compat_ioctl = ocfs2_compat_ioctl,
  1577. #endif
  1578. .splice_read = ocfs2_file_splice_read,
  1579. .splice_write = ocfs2_file_splice_write,
  1580. };
  1581. const struct file_operations ocfs2_dops = {
  1582. .read = generic_read_dir,
  1583. .readdir = ocfs2_readdir,
  1584. .fsync = ocfs2_sync_file,
  1585. .ioctl = ocfs2_ioctl,
  1586. #ifdef CONFIG_COMPAT
  1587. .compat_ioctl = ocfs2_compat_ioctl,
  1588. #endif
  1589. };