file.c 57 KB

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