file.c 55 KB

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