file.c 56 KB

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