file.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #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. /* Some parts of this taken from generic_cont_expand, which turned out
  664. * to be too fragile to do exactly what we need without us having to
  665. * worry about recursive locking in ->prepare_write() and
  666. * ->commit_write(). */
  667. static int ocfs2_write_zero_page(struct inode *inode,
  668. u64 size)
  669. {
  670. struct address_space *mapping = inode->i_mapping;
  671. struct page *page;
  672. unsigned long index;
  673. unsigned int offset;
  674. handle_t *handle = NULL;
  675. int ret;
  676. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  677. /* ugh. in prepare/commit_write, if from==to==start of block, we
  678. ** skip the prepare. make sure we never send an offset for the start
  679. ** of a block
  680. */
  681. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  682. offset++;
  683. }
  684. index = size >> PAGE_CACHE_SHIFT;
  685. page = grab_cache_page(mapping, index);
  686. if (!page) {
  687. ret = -ENOMEM;
  688. mlog_errno(ret);
  689. goto out;
  690. }
  691. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  692. if (ret < 0) {
  693. mlog_errno(ret);
  694. goto out_unlock;
  695. }
  696. if (ocfs2_should_order_data(inode)) {
  697. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  698. offset);
  699. if (IS_ERR(handle)) {
  700. ret = PTR_ERR(handle);
  701. handle = NULL;
  702. goto out_unlock;
  703. }
  704. }
  705. /* must not update i_size! */
  706. ret = block_commit_write(page, offset, offset);
  707. if (ret < 0)
  708. mlog_errno(ret);
  709. else
  710. ret = 0;
  711. if (handle)
  712. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  713. out_unlock:
  714. unlock_page(page);
  715. page_cache_release(page);
  716. out:
  717. return ret;
  718. }
  719. static int ocfs2_zero_extend(struct inode *inode,
  720. u64 zero_to_size)
  721. {
  722. int ret = 0;
  723. u64 start_off;
  724. struct super_block *sb = inode->i_sb;
  725. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  726. while (start_off < zero_to_size) {
  727. ret = ocfs2_write_zero_page(inode, start_off);
  728. if (ret < 0) {
  729. mlog_errno(ret);
  730. goto out;
  731. }
  732. start_off += sb->s_blocksize;
  733. /*
  734. * Very large extends have the potential to lock up
  735. * the cpu for extended periods of time.
  736. */
  737. cond_resched();
  738. }
  739. out:
  740. return ret;
  741. }
  742. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  743. {
  744. int ret;
  745. u32 clusters_to_add;
  746. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  747. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  748. if (clusters_to_add < oi->ip_clusters)
  749. clusters_to_add = 0;
  750. else
  751. clusters_to_add -= oi->ip_clusters;
  752. if (clusters_to_add) {
  753. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  754. clusters_to_add, 0);
  755. if (ret) {
  756. mlog_errno(ret);
  757. goto out;
  758. }
  759. }
  760. /*
  761. * Call this even if we don't add any clusters to the tree. We
  762. * still need to zero the area between the old i_size and the
  763. * new i_size.
  764. */
  765. ret = ocfs2_zero_extend(inode, zero_to);
  766. if (ret < 0)
  767. mlog_errno(ret);
  768. out:
  769. return ret;
  770. }
  771. static int ocfs2_extend_file(struct inode *inode,
  772. struct buffer_head *di_bh,
  773. u64 new_i_size)
  774. {
  775. int ret = 0;
  776. BUG_ON(!di_bh);
  777. /* setattr sometimes calls us like this. */
  778. if (new_i_size == 0)
  779. goto out;
  780. if (i_size_read(inode) == new_i_size)
  781. goto out;
  782. BUG_ON(new_i_size < i_size_read(inode));
  783. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  784. goto out_update_size;
  785. /*
  786. * protect the pages that ocfs2_zero_extend is going to be
  787. * pulling into the page cache.. we do this before the
  788. * metadata extend so that we don't get into the situation
  789. * where we've extended the metadata but can't get the data
  790. * lock to zero.
  791. */
  792. ret = ocfs2_data_lock(inode, 1);
  793. if (ret < 0) {
  794. mlog_errno(ret);
  795. goto out;
  796. }
  797. /*
  798. * The alloc sem blocks people in read/write from reading our
  799. * allocation until we're done changing it. We depend on
  800. * i_mutex to block other extend/truncate calls while we're
  801. * here.
  802. */
  803. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  804. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  805. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  806. if (ret < 0) {
  807. mlog_errno(ret);
  808. goto out_unlock;
  809. }
  810. out_update_size:
  811. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  812. if (ret < 0)
  813. mlog_errno(ret);
  814. out_unlock:
  815. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  816. ocfs2_data_unlock(inode, 1);
  817. out:
  818. return ret;
  819. }
  820. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  821. {
  822. int status = 0, size_change;
  823. struct inode *inode = dentry->d_inode;
  824. struct super_block *sb = inode->i_sb;
  825. struct ocfs2_super *osb = OCFS2_SB(sb);
  826. struct buffer_head *bh = NULL;
  827. handle_t *handle = NULL;
  828. mlog_entry("(0x%p, '%.*s')\n", dentry,
  829. dentry->d_name.len, dentry->d_name.name);
  830. if (attr->ia_valid & ATTR_MODE)
  831. mlog(0, "mode change: %d\n", attr->ia_mode);
  832. if (attr->ia_valid & ATTR_UID)
  833. mlog(0, "uid change: %d\n", attr->ia_uid);
  834. if (attr->ia_valid & ATTR_GID)
  835. mlog(0, "gid change: %d\n", attr->ia_gid);
  836. if (attr->ia_valid & ATTR_SIZE)
  837. mlog(0, "size change...\n");
  838. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  839. mlog(0, "time change...\n");
  840. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  841. | ATTR_GID | ATTR_UID | ATTR_MODE)
  842. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  843. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  844. return 0;
  845. }
  846. status = inode_change_ok(inode, attr);
  847. if (status)
  848. return status;
  849. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  850. if (size_change) {
  851. status = ocfs2_rw_lock(inode, 1);
  852. if (status < 0) {
  853. mlog_errno(status);
  854. goto bail;
  855. }
  856. }
  857. status = ocfs2_meta_lock(inode, &bh, 1);
  858. if (status < 0) {
  859. if (status != -ENOENT)
  860. mlog_errno(status);
  861. goto bail_unlock_rw;
  862. }
  863. if (size_change && attr->ia_size != i_size_read(inode)) {
  864. if (attr->ia_size > sb->s_maxbytes) {
  865. status = -EFBIG;
  866. goto bail_unlock;
  867. }
  868. if (i_size_read(inode) > attr->ia_size)
  869. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  870. else
  871. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  872. if (status < 0) {
  873. if (status != -ENOSPC)
  874. mlog_errno(status);
  875. status = -ENOSPC;
  876. goto bail_unlock;
  877. }
  878. }
  879. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  880. if (IS_ERR(handle)) {
  881. status = PTR_ERR(handle);
  882. mlog_errno(status);
  883. goto bail_unlock;
  884. }
  885. /*
  886. * This will intentionally not wind up calling vmtruncate(),
  887. * since all the work for a size change has been done above.
  888. * Otherwise, we could get into problems with truncate as
  889. * ip_alloc_sem is used there to protect against i_size
  890. * changes.
  891. */
  892. status = inode_setattr(inode, attr);
  893. if (status < 0) {
  894. mlog_errno(status);
  895. goto bail_commit;
  896. }
  897. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  898. if (status < 0)
  899. mlog_errno(status);
  900. bail_commit:
  901. ocfs2_commit_trans(osb, handle);
  902. bail_unlock:
  903. ocfs2_meta_unlock(inode, 1);
  904. bail_unlock_rw:
  905. if (size_change)
  906. ocfs2_rw_unlock(inode, 1);
  907. bail:
  908. if (bh)
  909. brelse(bh);
  910. mlog_exit(status);
  911. return status;
  912. }
  913. int ocfs2_getattr(struct vfsmount *mnt,
  914. struct dentry *dentry,
  915. struct kstat *stat)
  916. {
  917. struct inode *inode = dentry->d_inode;
  918. struct super_block *sb = dentry->d_inode->i_sb;
  919. struct ocfs2_super *osb = sb->s_fs_info;
  920. int err;
  921. mlog_entry_void();
  922. err = ocfs2_inode_revalidate(dentry);
  923. if (err) {
  924. if (err != -ENOENT)
  925. mlog_errno(err);
  926. goto bail;
  927. }
  928. generic_fillattr(inode, stat);
  929. /* We set the blksize from the cluster size for performance */
  930. stat->blksize = osb->s_clustersize;
  931. bail:
  932. mlog_exit(err);
  933. return err;
  934. }
  935. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  936. {
  937. int ret;
  938. mlog_entry_void();
  939. ret = ocfs2_meta_lock(inode, NULL, 0);
  940. if (ret) {
  941. if (ret != -ENOENT)
  942. mlog_errno(ret);
  943. goto out;
  944. }
  945. ret = generic_permission(inode, mask, NULL);
  946. ocfs2_meta_unlock(inode, 0);
  947. out:
  948. mlog_exit(ret);
  949. return ret;
  950. }
  951. static int __ocfs2_write_remove_suid(struct inode *inode,
  952. struct buffer_head *bh)
  953. {
  954. int ret;
  955. handle_t *handle;
  956. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  957. struct ocfs2_dinode *di;
  958. mlog_entry("(Inode %llu, mode 0%o)\n",
  959. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  960. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  961. if (handle == NULL) {
  962. ret = -ENOMEM;
  963. mlog_errno(ret);
  964. goto out;
  965. }
  966. ret = ocfs2_journal_access(handle, inode, bh,
  967. OCFS2_JOURNAL_ACCESS_WRITE);
  968. if (ret < 0) {
  969. mlog_errno(ret);
  970. goto out_trans;
  971. }
  972. inode->i_mode &= ~S_ISUID;
  973. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  974. inode->i_mode &= ~S_ISGID;
  975. di = (struct ocfs2_dinode *) bh->b_data;
  976. di->i_mode = cpu_to_le16(inode->i_mode);
  977. ret = ocfs2_journal_dirty(handle, bh);
  978. if (ret < 0)
  979. mlog_errno(ret);
  980. out_trans:
  981. ocfs2_commit_trans(osb, handle);
  982. out:
  983. mlog_exit(ret);
  984. return ret;
  985. }
  986. /*
  987. * Will look for holes and unwritten extents in the range starting at
  988. * pos for count bytes (inclusive).
  989. */
  990. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  991. size_t count)
  992. {
  993. int ret = 0;
  994. unsigned int extent_flags;
  995. u32 cpos, clusters, extent_len, phys_cpos;
  996. struct super_block *sb = inode->i_sb;
  997. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  998. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  999. while (clusters) {
  1000. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1001. &extent_flags);
  1002. if (ret < 0) {
  1003. mlog_errno(ret);
  1004. goto out;
  1005. }
  1006. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1007. ret = 1;
  1008. break;
  1009. }
  1010. if (extent_len > clusters)
  1011. extent_len = clusters;
  1012. clusters -= extent_len;
  1013. cpos += extent_len;
  1014. }
  1015. out:
  1016. return ret;
  1017. }
  1018. static int ocfs2_write_remove_suid(struct inode *inode)
  1019. {
  1020. int ret;
  1021. struct buffer_head *bh = NULL;
  1022. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1023. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1024. oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  1025. if (ret < 0) {
  1026. mlog_errno(ret);
  1027. goto out;
  1028. }
  1029. ret = __ocfs2_write_remove_suid(inode, bh);
  1030. out:
  1031. brelse(bh);
  1032. return ret;
  1033. }
  1034. /*
  1035. * Allocate enough extents to cover the region starting at byte offset
  1036. * start for len bytes. Existing extents are skipped, any extents
  1037. * added are marked as "unwritten".
  1038. */
  1039. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1040. u64 start, u64 len)
  1041. {
  1042. int ret;
  1043. u32 cpos, phys_cpos, clusters, alloc_size;
  1044. /*
  1045. * We consider both start and len to be inclusive.
  1046. */
  1047. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1048. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1049. clusters -= cpos;
  1050. while (clusters) {
  1051. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1052. &alloc_size, NULL);
  1053. if (ret) {
  1054. mlog_errno(ret);
  1055. goto out;
  1056. }
  1057. /*
  1058. * Hole or existing extent len can be arbitrary, so
  1059. * cap it to our own allocation request.
  1060. */
  1061. if (alloc_size > clusters)
  1062. alloc_size = clusters;
  1063. if (phys_cpos) {
  1064. /*
  1065. * We already have an allocation at this
  1066. * region so we can safely skip it.
  1067. */
  1068. goto next;
  1069. }
  1070. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1071. if (ret) {
  1072. if (ret != -ENOSPC)
  1073. mlog_errno(ret);
  1074. goto out;
  1075. }
  1076. next:
  1077. cpos += alloc_size;
  1078. clusters -= alloc_size;
  1079. }
  1080. ret = 0;
  1081. out:
  1082. return ret;
  1083. }
  1084. static int __ocfs2_remove_inode_range(struct inode *inode,
  1085. struct buffer_head *di_bh,
  1086. u32 cpos, u32 phys_cpos, u32 len,
  1087. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1088. {
  1089. int ret;
  1090. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  1091. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1092. struct inode *tl_inode = osb->osb_tl_inode;
  1093. handle_t *handle;
  1094. struct ocfs2_alloc_context *meta_ac = NULL;
  1095. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1096. ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
  1097. if (ret) {
  1098. mlog_errno(ret);
  1099. return ret;
  1100. }
  1101. mutex_lock(&tl_inode->i_mutex);
  1102. if (ocfs2_truncate_log_needs_flush(osb)) {
  1103. ret = __ocfs2_flush_truncate_log(osb);
  1104. if (ret < 0) {
  1105. mlog_errno(ret);
  1106. goto out;
  1107. }
  1108. }
  1109. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  1110. if (handle == NULL) {
  1111. ret = -ENOMEM;
  1112. mlog_errno(ret);
  1113. goto out;
  1114. }
  1115. ret = ocfs2_journal_access(handle, inode, di_bh,
  1116. OCFS2_JOURNAL_ACCESS_WRITE);
  1117. if (ret) {
  1118. mlog_errno(ret);
  1119. goto out;
  1120. }
  1121. ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
  1122. dealloc);
  1123. if (ret) {
  1124. mlog_errno(ret);
  1125. goto out_commit;
  1126. }
  1127. OCFS2_I(inode)->ip_clusters -= len;
  1128. di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1129. ret = ocfs2_journal_dirty(handle, di_bh);
  1130. if (ret) {
  1131. mlog_errno(ret);
  1132. goto out_commit;
  1133. }
  1134. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  1135. if (ret)
  1136. mlog_errno(ret);
  1137. out_commit:
  1138. ocfs2_commit_trans(osb, handle);
  1139. out:
  1140. mutex_unlock(&tl_inode->i_mutex);
  1141. if (meta_ac)
  1142. ocfs2_free_alloc_context(meta_ac);
  1143. return ret;
  1144. }
  1145. /*
  1146. * Truncate a byte range, avoiding pages within partial clusters. This
  1147. * preserves those pages for the zeroing code to write to.
  1148. */
  1149. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1150. u64 byte_len)
  1151. {
  1152. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1153. loff_t start, end;
  1154. struct address_space *mapping = inode->i_mapping;
  1155. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1156. end = byte_start + byte_len;
  1157. end = end & ~(osb->s_clustersize - 1);
  1158. if (start < end) {
  1159. unmap_mapping_range(mapping, start, end - start, 0);
  1160. truncate_inode_pages_range(mapping, start, end - 1);
  1161. }
  1162. }
  1163. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1164. u64 start, u64 len)
  1165. {
  1166. int ret = 0;
  1167. u64 tmpend, end = start + len;
  1168. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1169. unsigned int csize = osb->s_clustersize;
  1170. handle_t *handle;
  1171. /*
  1172. * The "start" and "end" values are NOT necessarily part of
  1173. * the range whose allocation is being deleted. Rather, this
  1174. * is what the user passed in with the request. We must zero
  1175. * partial clusters here. There's no need to worry about
  1176. * physical allocation - the zeroing code knows to skip holes.
  1177. */
  1178. mlog(0, "byte start: %llu, end: %llu\n",
  1179. (unsigned long long)start, (unsigned long long)end);
  1180. /*
  1181. * If both edges are on a cluster boundary then there's no
  1182. * zeroing required as the region is part of the allocation to
  1183. * be truncated.
  1184. */
  1185. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1186. goto out;
  1187. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1188. if (handle == NULL) {
  1189. ret = -ENOMEM;
  1190. mlog_errno(ret);
  1191. goto out;
  1192. }
  1193. /*
  1194. * We want to get the byte offset of the end of the 1st cluster.
  1195. */
  1196. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1197. if (tmpend > end)
  1198. tmpend = end;
  1199. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1200. (unsigned long long)start, (unsigned long long)tmpend);
  1201. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1202. if (ret)
  1203. mlog_errno(ret);
  1204. if (tmpend < end) {
  1205. /*
  1206. * This may make start and end equal, but the zeroing
  1207. * code will skip any work in that case so there's no
  1208. * need to catch it up here.
  1209. */
  1210. start = end & ~(osb->s_clustersize - 1);
  1211. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1212. (unsigned long long)start, (unsigned long long)end);
  1213. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1214. if (ret)
  1215. mlog_errno(ret);
  1216. }
  1217. ocfs2_commit_trans(osb, handle);
  1218. out:
  1219. return ret;
  1220. }
  1221. static int ocfs2_remove_inode_range(struct inode *inode,
  1222. struct buffer_head *di_bh, u64 byte_start,
  1223. u64 byte_len)
  1224. {
  1225. int ret = 0;
  1226. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1227. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1228. struct ocfs2_cached_dealloc_ctxt dealloc;
  1229. ocfs2_init_dealloc_ctxt(&dealloc);
  1230. if (byte_len == 0)
  1231. return 0;
  1232. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1233. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1234. if (trunc_len >= trunc_start)
  1235. trunc_len -= trunc_start;
  1236. else
  1237. trunc_len = 0;
  1238. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1239. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1240. (unsigned long long)byte_start,
  1241. (unsigned long long)byte_len, trunc_start, trunc_len);
  1242. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1243. if (ret) {
  1244. mlog_errno(ret);
  1245. goto out;
  1246. }
  1247. cpos = trunc_start;
  1248. while (trunc_len) {
  1249. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1250. &alloc_size, NULL);
  1251. if (ret) {
  1252. mlog_errno(ret);
  1253. goto out;
  1254. }
  1255. if (alloc_size > trunc_len)
  1256. alloc_size = trunc_len;
  1257. /* Only do work for non-holes */
  1258. if (phys_cpos != 0) {
  1259. ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
  1260. phys_cpos, alloc_size,
  1261. &dealloc);
  1262. if (ret) {
  1263. mlog_errno(ret);
  1264. goto out;
  1265. }
  1266. }
  1267. cpos += alloc_size;
  1268. trunc_len -= alloc_size;
  1269. }
  1270. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1271. out:
  1272. ocfs2_schedule_truncate_log_flush(osb, 1);
  1273. ocfs2_run_deallocs(osb, &dealloc);
  1274. return ret;
  1275. }
  1276. /*
  1277. * Parts of this function taken from xfs_change_file_space()
  1278. */
  1279. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1280. loff_t f_pos, unsigned int cmd,
  1281. struct ocfs2_space_resv *sr,
  1282. int change_size)
  1283. {
  1284. int ret;
  1285. s64 llen;
  1286. loff_t size;
  1287. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1288. struct buffer_head *di_bh = NULL;
  1289. handle_t *handle;
  1290. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1291. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1292. return -EROFS;
  1293. mutex_lock(&inode->i_mutex);
  1294. /*
  1295. * This prevents concurrent writes on other nodes
  1296. */
  1297. ret = ocfs2_rw_lock(inode, 1);
  1298. if (ret) {
  1299. mlog_errno(ret);
  1300. goto out;
  1301. }
  1302. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1303. if (ret) {
  1304. mlog_errno(ret);
  1305. goto out_rw_unlock;
  1306. }
  1307. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1308. ret = -EPERM;
  1309. goto out_meta_unlock;
  1310. }
  1311. switch (sr->l_whence) {
  1312. case 0: /*SEEK_SET*/
  1313. break;
  1314. case 1: /*SEEK_CUR*/
  1315. sr->l_start += f_pos;
  1316. break;
  1317. case 2: /*SEEK_END*/
  1318. sr->l_start += i_size_read(inode);
  1319. break;
  1320. default:
  1321. ret = -EINVAL;
  1322. goto out_meta_unlock;
  1323. }
  1324. sr->l_whence = 0;
  1325. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1326. if (sr->l_start < 0
  1327. || sr->l_start > max_off
  1328. || (sr->l_start + llen) < 0
  1329. || (sr->l_start + llen) > max_off) {
  1330. ret = -EINVAL;
  1331. goto out_meta_unlock;
  1332. }
  1333. size = sr->l_start + sr->l_len;
  1334. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1335. if (sr->l_len <= 0) {
  1336. ret = -EINVAL;
  1337. goto out_meta_unlock;
  1338. }
  1339. }
  1340. if (file && should_remove_suid(file->f_path.dentry)) {
  1341. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1342. if (ret) {
  1343. mlog_errno(ret);
  1344. goto out_meta_unlock;
  1345. }
  1346. }
  1347. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1348. switch (cmd) {
  1349. case OCFS2_IOC_RESVSP:
  1350. case OCFS2_IOC_RESVSP64:
  1351. /*
  1352. * This takes unsigned offsets, but the signed ones we
  1353. * pass have been checked against overflow above.
  1354. */
  1355. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1356. sr->l_len);
  1357. break;
  1358. case OCFS2_IOC_UNRESVSP:
  1359. case OCFS2_IOC_UNRESVSP64:
  1360. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1361. sr->l_len);
  1362. break;
  1363. default:
  1364. ret = -EINVAL;
  1365. }
  1366. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1367. if (ret) {
  1368. mlog_errno(ret);
  1369. goto out_meta_unlock;
  1370. }
  1371. /*
  1372. * We update c/mtime for these changes
  1373. */
  1374. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1375. if (IS_ERR(handle)) {
  1376. ret = PTR_ERR(handle);
  1377. mlog_errno(ret);
  1378. goto out_meta_unlock;
  1379. }
  1380. if (change_size && i_size_read(inode) < size)
  1381. i_size_write(inode, size);
  1382. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1383. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1384. if (ret < 0)
  1385. mlog_errno(ret);
  1386. ocfs2_commit_trans(osb, handle);
  1387. out_meta_unlock:
  1388. brelse(di_bh);
  1389. ocfs2_meta_unlock(inode, 1);
  1390. out_rw_unlock:
  1391. ocfs2_rw_unlock(inode, 1);
  1392. mutex_unlock(&inode->i_mutex);
  1393. out:
  1394. return ret;
  1395. }
  1396. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1397. struct ocfs2_space_resv *sr)
  1398. {
  1399. struct inode *inode = file->f_path.dentry->d_inode;
  1400. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
  1401. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1402. !ocfs2_writes_unwritten_extents(osb))
  1403. return -ENOTTY;
  1404. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1405. !ocfs2_sparse_alloc(osb))
  1406. return -ENOTTY;
  1407. if (!S_ISREG(inode->i_mode))
  1408. return -EINVAL;
  1409. if (!(file->f_mode & FMODE_WRITE))
  1410. return -EBADF;
  1411. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1412. }
  1413. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1414. loff_t len)
  1415. {
  1416. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1417. struct ocfs2_space_resv sr;
  1418. int change_size = 1;
  1419. if (!ocfs2_writes_unwritten_extents(osb))
  1420. return -EOPNOTSUPP;
  1421. if (S_ISDIR(inode->i_mode))
  1422. return -ENODEV;
  1423. if (mode & FALLOC_FL_KEEP_SIZE)
  1424. change_size = 0;
  1425. sr.l_whence = 0;
  1426. sr.l_start = (s64)offset;
  1427. sr.l_len = (s64)len;
  1428. return __ocfs2_change_file_space(NULL, inode, offset,
  1429. OCFS2_IOC_RESVSP64, &sr, change_size);
  1430. }
  1431. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1432. loff_t *ppos,
  1433. size_t count,
  1434. int appending,
  1435. int *direct_io)
  1436. {
  1437. int ret = 0, meta_level = 0;
  1438. struct inode *inode = dentry->d_inode;
  1439. loff_t saved_pos, end;
  1440. /*
  1441. * We start with a read level meta lock and only jump to an ex
  1442. * if we need to make modifications here.
  1443. */
  1444. for(;;) {
  1445. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1446. if (ret < 0) {
  1447. meta_level = -1;
  1448. mlog_errno(ret);
  1449. goto out;
  1450. }
  1451. /* Clear suid / sgid if necessary. We do this here
  1452. * instead of later in the write path because
  1453. * remove_suid() calls ->setattr without any hint that
  1454. * we may have already done our cluster locking. Since
  1455. * ocfs2_setattr() *must* take cluster locks to
  1456. * proceeed, this will lead us to recursively lock the
  1457. * inode. There's also the dinode i_size state which
  1458. * can be lost via setattr during extending writes (we
  1459. * set inode->i_size at the end of a write. */
  1460. if (should_remove_suid(dentry)) {
  1461. if (meta_level == 0) {
  1462. ocfs2_meta_unlock(inode, meta_level);
  1463. meta_level = 1;
  1464. continue;
  1465. }
  1466. ret = ocfs2_write_remove_suid(inode);
  1467. if (ret < 0) {
  1468. mlog_errno(ret);
  1469. goto out_unlock;
  1470. }
  1471. }
  1472. /* work on a copy of ppos until we're sure that we won't have
  1473. * to recalculate it due to relocking. */
  1474. if (appending) {
  1475. saved_pos = i_size_read(inode);
  1476. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1477. } else {
  1478. saved_pos = *ppos;
  1479. }
  1480. end = saved_pos + count;
  1481. /*
  1482. * Skip the O_DIRECT checks if we don't need
  1483. * them.
  1484. */
  1485. if (!direct_io || !(*direct_io))
  1486. break;
  1487. /*
  1488. * Allowing concurrent direct writes means
  1489. * i_size changes wouldn't be synchronized, so
  1490. * one node could wind up truncating another
  1491. * nodes writes.
  1492. */
  1493. if (end > i_size_read(inode)) {
  1494. *direct_io = 0;
  1495. break;
  1496. }
  1497. /*
  1498. * We don't fill holes during direct io, so
  1499. * check for them here. If any are found, the
  1500. * caller will have to retake some cluster
  1501. * locks and initiate the io as buffered.
  1502. */
  1503. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1504. if (ret == 1) {
  1505. *direct_io = 0;
  1506. ret = 0;
  1507. } else if (ret < 0)
  1508. mlog_errno(ret);
  1509. break;
  1510. }
  1511. if (appending)
  1512. *ppos = saved_pos;
  1513. out_unlock:
  1514. ocfs2_meta_unlock(inode, meta_level);
  1515. out:
  1516. return ret;
  1517. }
  1518. static inline void
  1519. ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  1520. {
  1521. const struct iovec *iov = *iovp;
  1522. size_t base = *basep;
  1523. do {
  1524. int copy = min(bytes, iov->iov_len - base);
  1525. bytes -= copy;
  1526. base += copy;
  1527. if (iov->iov_len == base) {
  1528. iov++;
  1529. base = 0;
  1530. }
  1531. } while (bytes);
  1532. *iovp = iov;
  1533. *basep = base;
  1534. }
  1535. static struct page * ocfs2_get_write_source(char **ret_src_buf,
  1536. const struct iovec *cur_iov,
  1537. size_t iov_offset)
  1538. {
  1539. int ret;
  1540. char *buf = cur_iov->iov_base + iov_offset;
  1541. struct page *src_page = NULL;
  1542. unsigned long off;
  1543. off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
  1544. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1545. /*
  1546. * Pull in the user page. We want to do this outside
  1547. * of the meta data locks in order to preserve locking
  1548. * order in case of page fault.
  1549. */
  1550. ret = get_user_pages(current, current->mm,
  1551. (unsigned long)buf & PAGE_CACHE_MASK, 1,
  1552. 0, 0, &src_page, NULL);
  1553. if (ret == 1)
  1554. *ret_src_buf = kmap(src_page) + off;
  1555. else
  1556. src_page = ERR_PTR(-EFAULT);
  1557. } else {
  1558. *ret_src_buf = buf;
  1559. }
  1560. return src_page;
  1561. }
  1562. static void ocfs2_put_write_source(struct page *page)
  1563. {
  1564. if (page) {
  1565. kunmap(page);
  1566. page_cache_release(page);
  1567. }
  1568. }
  1569. static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
  1570. const struct iovec *iov,
  1571. unsigned long nr_segs,
  1572. size_t count,
  1573. ssize_t o_direct_written)
  1574. {
  1575. int ret = 0;
  1576. ssize_t copied, total = 0;
  1577. size_t iov_offset = 0, bytes;
  1578. loff_t pos;
  1579. const struct iovec *cur_iov = iov;
  1580. struct page *user_page, *page;
  1581. char * uninitialized_var(buf);
  1582. char *dst;
  1583. void *fsdata;
  1584. /*
  1585. * handle partial DIO write. Adjust cur_iov if needed.
  1586. */
  1587. ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
  1588. do {
  1589. pos = *ppos;
  1590. user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
  1591. if (IS_ERR(user_page)) {
  1592. ret = PTR_ERR(user_page);
  1593. goto out;
  1594. }
  1595. /* Stay within our page boundaries */
  1596. bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
  1597. (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
  1598. /* Stay within the vector boundary */
  1599. bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
  1600. /* Stay within count */
  1601. bytes = min(bytes, count);
  1602. page = NULL;
  1603. ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
  1604. &page, &fsdata);
  1605. if (ret) {
  1606. mlog_errno(ret);
  1607. goto out;
  1608. }
  1609. dst = kmap_atomic(page, KM_USER0);
  1610. memcpy(dst + (pos & (loff_t)(PAGE_CACHE_SIZE - 1)), buf, bytes);
  1611. kunmap_atomic(dst, KM_USER0);
  1612. flush_dcache_page(page);
  1613. ocfs2_put_write_source(user_page);
  1614. copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
  1615. bytes, page, fsdata);
  1616. if (copied < 0) {
  1617. mlog_errno(copied);
  1618. ret = copied;
  1619. goto out;
  1620. }
  1621. total += copied;
  1622. *ppos = pos + copied;
  1623. count -= copied;
  1624. ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
  1625. } while(count);
  1626. out:
  1627. return total ? total : ret;
  1628. }
  1629. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1630. const struct iovec *iov,
  1631. unsigned long nr_segs,
  1632. loff_t pos)
  1633. {
  1634. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1635. int can_do_direct, sync = 0;
  1636. ssize_t written = 0;
  1637. size_t ocount; /* original count */
  1638. size_t count; /* after file limit checks */
  1639. loff_t *ppos = &iocb->ki_pos;
  1640. struct file *file = iocb->ki_filp;
  1641. struct inode *inode = file->f_path.dentry->d_inode;
  1642. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1643. (unsigned int)nr_segs,
  1644. file->f_path.dentry->d_name.len,
  1645. file->f_path.dentry->d_name.name);
  1646. if (iocb->ki_left == 0)
  1647. return 0;
  1648. ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1649. if (ret)
  1650. return ret;
  1651. count = ocount;
  1652. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1653. appending = file->f_flags & O_APPEND ? 1 : 0;
  1654. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1655. mutex_lock(&inode->i_mutex);
  1656. relock:
  1657. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1658. if (direct_io) {
  1659. down_read(&inode->i_alloc_sem);
  1660. have_alloc_sem = 1;
  1661. }
  1662. /* concurrent O_DIRECT writes are allowed */
  1663. rw_level = !direct_io;
  1664. ret = ocfs2_rw_lock(inode, rw_level);
  1665. if (ret < 0) {
  1666. mlog_errno(ret);
  1667. goto out_sems;
  1668. }
  1669. can_do_direct = direct_io;
  1670. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1671. iocb->ki_left, appending,
  1672. &can_do_direct);
  1673. if (ret < 0) {
  1674. mlog_errno(ret);
  1675. goto out;
  1676. }
  1677. /*
  1678. * We can't complete the direct I/O as requested, fall back to
  1679. * buffered I/O.
  1680. */
  1681. if (direct_io && !can_do_direct) {
  1682. ocfs2_rw_unlock(inode, rw_level);
  1683. up_read(&inode->i_alloc_sem);
  1684. have_alloc_sem = 0;
  1685. rw_level = -1;
  1686. direct_io = 0;
  1687. sync = 1;
  1688. goto relock;
  1689. }
  1690. if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
  1691. sync = 1;
  1692. /*
  1693. * XXX: Is it ok to execute these checks a second time?
  1694. */
  1695. ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
  1696. if (ret)
  1697. goto out;
  1698. /*
  1699. * Set pos so that sync_page_range_nolock() below understands
  1700. * where to start from. We might've moved it around via the
  1701. * calls above. The range we want to actually sync starts from
  1702. * *ppos here.
  1703. *
  1704. */
  1705. pos = *ppos;
  1706. /* communicate with ocfs2_dio_end_io */
  1707. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1708. if (direct_io) {
  1709. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1710. ppos, count, ocount);
  1711. if (written < 0) {
  1712. ret = written;
  1713. goto out_dio;
  1714. }
  1715. } else {
  1716. written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
  1717. count, written);
  1718. if (written < 0) {
  1719. ret = written;
  1720. if (ret != -EFAULT || ret != -ENOSPC)
  1721. mlog_errno(ret);
  1722. goto out;
  1723. }
  1724. }
  1725. out_dio:
  1726. /* buffered aio wouldn't have proper lock coverage today */
  1727. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1728. /*
  1729. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1730. * function pointer which is called when o_direct io completes so that
  1731. * it can unlock our rw lock. (it's the clustered equivalent of
  1732. * i_alloc_sem; protects truncate from racing with pending ios).
  1733. * Unfortunately there are error cases which call end_io and others
  1734. * that don't. so we don't have to unlock the rw_lock if either an
  1735. * async dio is going to do it in the future or an end_io after an
  1736. * error has already done it.
  1737. */
  1738. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1739. rw_level = -1;
  1740. have_alloc_sem = 0;
  1741. }
  1742. out:
  1743. if (rw_level != -1)
  1744. ocfs2_rw_unlock(inode, rw_level);
  1745. out_sems:
  1746. if (have_alloc_sem)
  1747. up_read(&inode->i_alloc_sem);
  1748. if (written > 0 && sync) {
  1749. ssize_t err;
  1750. err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
  1751. if (err < 0)
  1752. written = err;
  1753. }
  1754. mutex_unlock(&inode->i_mutex);
  1755. mlog_exit(ret);
  1756. return written ? written : ret;
  1757. }
  1758. static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
  1759. struct pipe_buffer *buf,
  1760. struct splice_desc *sd)
  1761. {
  1762. int ret, count;
  1763. ssize_t copied = 0;
  1764. struct file *file = sd->u.file;
  1765. unsigned int offset;
  1766. struct page *page = NULL;
  1767. void *fsdata;
  1768. char *src, *dst;
  1769. ret = buf->ops->confirm(pipe, buf);
  1770. if (ret)
  1771. goto out;
  1772. offset = sd->pos & ~PAGE_CACHE_MASK;
  1773. count = sd->len;
  1774. if (count + offset > PAGE_CACHE_SIZE)
  1775. count = PAGE_CACHE_SIZE - offset;
  1776. ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
  1777. &page, &fsdata);
  1778. if (ret) {
  1779. mlog_errno(ret);
  1780. goto out;
  1781. }
  1782. src = buf->ops->map(pipe, buf, 1);
  1783. dst = kmap_atomic(page, KM_USER1);
  1784. memcpy(dst + offset, src + buf->offset, count);
  1785. kunmap_atomic(dst, KM_USER1);
  1786. buf->ops->unmap(pipe, buf, src);
  1787. copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
  1788. page, fsdata);
  1789. if (copied < 0) {
  1790. mlog_errno(copied);
  1791. ret = copied;
  1792. goto out;
  1793. }
  1794. out:
  1795. return copied ? copied : ret;
  1796. }
  1797. static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1798. struct file *out,
  1799. loff_t *ppos,
  1800. size_t len,
  1801. unsigned int flags)
  1802. {
  1803. int ret, err;
  1804. struct address_space *mapping = out->f_mapping;
  1805. struct inode *inode = mapping->host;
  1806. struct splice_desc sd = {
  1807. .total_len = len,
  1808. .flags = flags,
  1809. .pos = *ppos,
  1810. .u.file = out,
  1811. };
  1812. ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
  1813. if (ret > 0) {
  1814. *ppos += ret;
  1815. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1816. err = generic_osync_inode(inode, mapping,
  1817. OSYNC_METADATA|OSYNC_DATA);
  1818. if (err)
  1819. ret = err;
  1820. }
  1821. }
  1822. return ret;
  1823. }
  1824. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1825. struct file *out,
  1826. loff_t *ppos,
  1827. size_t len,
  1828. unsigned int flags)
  1829. {
  1830. int ret;
  1831. struct inode *inode = out->f_path.dentry->d_inode;
  1832. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1833. (unsigned int)len,
  1834. out->f_path.dentry->d_name.len,
  1835. out->f_path.dentry->d_name.name);
  1836. inode_double_lock(inode, pipe->inode);
  1837. ret = ocfs2_rw_lock(inode, 1);
  1838. if (ret < 0) {
  1839. mlog_errno(ret);
  1840. goto out;
  1841. }
  1842. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1843. NULL);
  1844. if (ret < 0) {
  1845. mlog_errno(ret);
  1846. goto out_unlock;
  1847. }
  1848. /* ok, we're done with i_size and alloc work */
  1849. ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
  1850. out_unlock:
  1851. ocfs2_rw_unlock(inode, 1);
  1852. out:
  1853. inode_double_unlock(inode, pipe->inode);
  1854. mlog_exit(ret);
  1855. return ret;
  1856. }
  1857. static ssize_t ocfs2_file_splice_read(struct file *in,
  1858. loff_t *ppos,
  1859. struct pipe_inode_info *pipe,
  1860. size_t len,
  1861. unsigned int flags)
  1862. {
  1863. int ret = 0;
  1864. struct inode *inode = in->f_path.dentry->d_inode;
  1865. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1866. (unsigned int)len,
  1867. in->f_path.dentry->d_name.len,
  1868. in->f_path.dentry->d_name.name);
  1869. /*
  1870. * See the comment in ocfs2_file_aio_read()
  1871. */
  1872. ret = ocfs2_meta_lock(inode, NULL, 0);
  1873. if (ret < 0) {
  1874. mlog_errno(ret);
  1875. goto bail;
  1876. }
  1877. ocfs2_meta_unlock(inode, 0);
  1878. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1879. bail:
  1880. mlog_exit(ret);
  1881. return ret;
  1882. }
  1883. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1884. const struct iovec *iov,
  1885. unsigned long nr_segs,
  1886. loff_t pos)
  1887. {
  1888. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1889. struct file *filp = iocb->ki_filp;
  1890. struct inode *inode = filp->f_path.dentry->d_inode;
  1891. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1892. (unsigned int)nr_segs,
  1893. filp->f_path.dentry->d_name.len,
  1894. filp->f_path.dentry->d_name.name);
  1895. if (!inode) {
  1896. ret = -EINVAL;
  1897. mlog_errno(ret);
  1898. goto bail;
  1899. }
  1900. /*
  1901. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1902. * need locks to protect pending reads from racing with truncate.
  1903. */
  1904. if (filp->f_flags & O_DIRECT) {
  1905. down_read(&inode->i_alloc_sem);
  1906. have_alloc_sem = 1;
  1907. ret = ocfs2_rw_lock(inode, 0);
  1908. if (ret < 0) {
  1909. mlog_errno(ret);
  1910. goto bail;
  1911. }
  1912. rw_level = 0;
  1913. /* communicate with ocfs2_dio_end_io */
  1914. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1915. }
  1916. /*
  1917. * We're fine letting folks race truncates and extending
  1918. * writes with read across the cluster, just like they can
  1919. * locally. Hence no rw_lock during read.
  1920. *
  1921. * Take and drop the meta data lock to update inode fields
  1922. * like i_size. This allows the checks down below
  1923. * generic_file_aio_read() a chance of actually working.
  1924. */
  1925. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1926. if (ret < 0) {
  1927. mlog_errno(ret);
  1928. goto bail;
  1929. }
  1930. ocfs2_meta_unlock(inode, lock_level);
  1931. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1932. if (ret == -EINVAL)
  1933. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1934. /* buffered aio wouldn't have proper lock coverage today */
  1935. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1936. /* see ocfs2_file_aio_write */
  1937. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1938. rw_level = -1;
  1939. have_alloc_sem = 0;
  1940. }
  1941. bail:
  1942. if (have_alloc_sem)
  1943. up_read(&inode->i_alloc_sem);
  1944. if (rw_level != -1)
  1945. ocfs2_rw_unlock(inode, rw_level);
  1946. mlog_exit(ret);
  1947. return ret;
  1948. }
  1949. const struct inode_operations ocfs2_file_iops = {
  1950. .setattr = ocfs2_setattr,
  1951. .getattr = ocfs2_getattr,
  1952. .permission = ocfs2_permission,
  1953. .fallocate = ocfs2_fallocate,
  1954. };
  1955. const struct inode_operations ocfs2_special_file_iops = {
  1956. .setattr = ocfs2_setattr,
  1957. .getattr = ocfs2_getattr,
  1958. .permission = ocfs2_permission,
  1959. };
  1960. const struct file_operations ocfs2_fops = {
  1961. .read = do_sync_read,
  1962. .write = do_sync_write,
  1963. .mmap = ocfs2_mmap,
  1964. .fsync = ocfs2_sync_file,
  1965. .release = ocfs2_file_release,
  1966. .open = ocfs2_file_open,
  1967. .aio_read = ocfs2_file_aio_read,
  1968. .aio_write = ocfs2_file_aio_write,
  1969. .ioctl = ocfs2_ioctl,
  1970. #ifdef CONFIG_COMPAT
  1971. .compat_ioctl = ocfs2_compat_ioctl,
  1972. #endif
  1973. .splice_read = ocfs2_file_splice_read,
  1974. .splice_write = ocfs2_file_splice_write,
  1975. };
  1976. const struct file_operations ocfs2_dops = {
  1977. .read = generic_read_dir,
  1978. .readdir = ocfs2_readdir,
  1979. .fsync = ocfs2_sync_file,
  1980. .ioctl = ocfs2_ioctl,
  1981. #ifdef CONFIG_COMPAT
  1982. .compat_ioctl = ocfs2_compat_ioctl,
  1983. #endif
  1984. };