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