file.c 56 KB

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