file.c 56 KB

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