file.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  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. ocfs2_journal_dirty(handle, bh);
  232. out_commit:
  233. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  234. out:
  235. mlog_exit(ret);
  236. return ret;
  237. }
  238. static int ocfs2_set_inode_size(handle_t *handle,
  239. struct inode *inode,
  240. struct buffer_head *fe_bh,
  241. u64 new_i_size)
  242. {
  243. int status;
  244. mlog_entry_void();
  245. i_size_write(inode, new_i_size);
  246. inode->i_blocks = ocfs2_inode_sector_count(inode);
  247. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  248. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  249. if (status < 0) {
  250. mlog_errno(status);
  251. goto bail;
  252. }
  253. bail:
  254. mlog_exit(status);
  255. return status;
  256. }
  257. int ocfs2_simple_size_update(struct inode *inode,
  258. struct buffer_head *di_bh,
  259. u64 new_i_size)
  260. {
  261. int ret;
  262. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  263. handle_t *handle = NULL;
  264. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  265. if (IS_ERR(handle)) {
  266. ret = PTR_ERR(handle);
  267. mlog_errno(ret);
  268. goto out;
  269. }
  270. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  271. new_i_size);
  272. if (ret < 0)
  273. mlog_errno(ret);
  274. ocfs2_commit_trans(osb, handle);
  275. out:
  276. return ret;
  277. }
  278. static int ocfs2_cow_file_pos(struct inode *inode,
  279. struct buffer_head *fe_bh,
  280. u64 offset)
  281. {
  282. int status;
  283. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  284. unsigned int num_clusters = 0;
  285. unsigned int ext_flags = 0;
  286. /*
  287. * If the new offset is aligned to the range of the cluster, there is
  288. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  289. * CoW either.
  290. */
  291. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  292. return 0;
  293. status = ocfs2_get_clusters(inode, cpos, &phys,
  294. &num_clusters, &ext_flags);
  295. if (status) {
  296. mlog_errno(status);
  297. goto out;
  298. }
  299. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  300. goto out;
  301. return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
  302. out:
  303. return status;
  304. }
  305. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  306. struct inode *inode,
  307. struct buffer_head *fe_bh,
  308. u64 new_i_size)
  309. {
  310. int status;
  311. handle_t *handle;
  312. struct ocfs2_dinode *di;
  313. u64 cluster_bytes;
  314. mlog_entry_void();
  315. /*
  316. * We need to CoW the cluster contains the offset if it is reflinked
  317. * since we will call ocfs2_zero_range_for_truncate later which will
  318. * write "0" from offset to the end of the cluster.
  319. */
  320. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  321. if (status) {
  322. mlog_errno(status);
  323. return status;
  324. }
  325. /* TODO: This needs to actually orphan the inode in this
  326. * transaction. */
  327. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  328. if (IS_ERR(handle)) {
  329. status = PTR_ERR(handle);
  330. mlog_errno(status);
  331. goto out;
  332. }
  333. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  334. OCFS2_JOURNAL_ACCESS_WRITE);
  335. if (status < 0) {
  336. mlog_errno(status);
  337. goto out_commit;
  338. }
  339. /*
  340. * Do this before setting i_size.
  341. */
  342. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  343. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  344. cluster_bytes);
  345. if (status) {
  346. mlog_errno(status);
  347. goto out_commit;
  348. }
  349. i_size_write(inode, new_i_size);
  350. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  351. di = (struct ocfs2_dinode *) fe_bh->b_data;
  352. di->i_size = cpu_to_le64(new_i_size);
  353. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  354. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  355. ocfs2_journal_dirty(handle, fe_bh);
  356. out_commit:
  357. ocfs2_commit_trans(osb, handle);
  358. out:
  359. mlog_exit(status);
  360. return status;
  361. }
  362. static int ocfs2_truncate_file(struct inode *inode,
  363. struct buffer_head *di_bh,
  364. u64 new_i_size)
  365. {
  366. int status = 0;
  367. struct ocfs2_dinode *fe = NULL;
  368. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  369. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  370. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  371. (unsigned long long)new_i_size);
  372. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  373. * already validated it */
  374. fe = (struct ocfs2_dinode *) di_bh->b_data;
  375. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  376. "Inode %llu, inode i_size = %lld != di "
  377. "i_size = %llu, i_flags = 0x%x\n",
  378. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  379. i_size_read(inode),
  380. (unsigned long long)le64_to_cpu(fe->i_size),
  381. le32_to_cpu(fe->i_flags));
  382. if (new_i_size > le64_to_cpu(fe->i_size)) {
  383. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  384. (unsigned long long)le64_to_cpu(fe->i_size),
  385. (unsigned long long)new_i_size);
  386. status = -EINVAL;
  387. mlog_errno(status);
  388. goto bail;
  389. }
  390. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  391. (unsigned long long)le64_to_cpu(fe->i_blkno),
  392. (unsigned long long)le64_to_cpu(fe->i_size),
  393. (unsigned long long)new_i_size);
  394. /* lets handle the simple truncate cases before doing any more
  395. * cluster locking. */
  396. if (new_i_size == le64_to_cpu(fe->i_size))
  397. goto bail;
  398. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  399. ocfs2_resv_discard(&osb->osb_la_resmap,
  400. &OCFS2_I(inode)->ip_la_data_resv);
  401. /*
  402. * The inode lock forced other nodes to sync and drop their
  403. * pages, which (correctly) happens even if we have a truncate
  404. * without allocation change - ocfs2 cluster sizes can be much
  405. * greater than page size, so we have to truncate them
  406. * anyway.
  407. */
  408. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  409. truncate_inode_pages(inode->i_mapping, new_i_size);
  410. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  411. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  412. i_size_read(inode), 1);
  413. if (status)
  414. mlog_errno(status);
  415. goto bail_unlock_sem;
  416. }
  417. /* alright, we're going to need to do a full blown alloc size
  418. * change. Orphan the inode so that recovery can complete the
  419. * truncate if necessary. This does the task of marking
  420. * i_size. */
  421. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  422. if (status < 0) {
  423. mlog_errno(status);
  424. goto bail_unlock_sem;
  425. }
  426. status = ocfs2_commit_truncate(osb, inode, di_bh);
  427. if (status < 0) {
  428. mlog_errno(status);
  429. goto bail_unlock_sem;
  430. }
  431. /* TODO: orphan dir cleanup here. */
  432. bail_unlock_sem:
  433. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  434. bail:
  435. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  436. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  437. mlog_exit(status);
  438. return status;
  439. }
  440. /*
  441. * extend file allocation only here.
  442. * we'll update all the disk stuff, and oip->alloc_size
  443. *
  444. * expect stuff to be locked, a transaction started and enough data /
  445. * metadata reservations in the contexts.
  446. *
  447. * Will return -EAGAIN, and a reason if a restart is needed.
  448. * If passed in, *reason will always be set, even in error.
  449. */
  450. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  451. struct inode *inode,
  452. u32 *logical_offset,
  453. u32 clusters_to_add,
  454. int mark_unwritten,
  455. struct buffer_head *fe_bh,
  456. handle_t *handle,
  457. struct ocfs2_alloc_context *data_ac,
  458. struct ocfs2_alloc_context *meta_ac,
  459. enum ocfs2_alloc_restarted *reason_ret)
  460. {
  461. int ret;
  462. struct ocfs2_extent_tree et;
  463. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
  464. ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
  465. clusters_to_add, mark_unwritten,
  466. data_ac, meta_ac, reason_ret);
  467. return ret;
  468. }
  469. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  470. u32 clusters_to_add, int mark_unwritten)
  471. {
  472. int status = 0;
  473. int restart_func = 0;
  474. int credits;
  475. u32 prev_clusters;
  476. struct buffer_head *bh = NULL;
  477. struct ocfs2_dinode *fe = NULL;
  478. handle_t *handle = NULL;
  479. struct ocfs2_alloc_context *data_ac = NULL;
  480. struct ocfs2_alloc_context *meta_ac = NULL;
  481. enum ocfs2_alloc_restarted why;
  482. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  483. struct ocfs2_extent_tree et;
  484. int did_quota = 0;
  485. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  486. /*
  487. * This function only exists for file systems which don't
  488. * support holes.
  489. */
  490. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  491. status = ocfs2_read_inode_block(inode, &bh);
  492. if (status < 0) {
  493. mlog_errno(status);
  494. goto leave;
  495. }
  496. fe = (struct ocfs2_dinode *) bh->b_data;
  497. restart_all:
  498. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  499. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  500. "clusters_to_add = %u\n",
  501. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  502. (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
  503. clusters_to_add);
  504. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  505. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  506. &data_ac, &meta_ac);
  507. if (status) {
  508. mlog_errno(status);
  509. goto leave;
  510. }
  511. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  512. clusters_to_add);
  513. handle = ocfs2_start_trans(osb, credits);
  514. if (IS_ERR(handle)) {
  515. status = PTR_ERR(handle);
  516. handle = NULL;
  517. mlog_errno(status);
  518. goto leave;
  519. }
  520. restarted_transaction:
  521. status = dquot_alloc_space_nodirty(inode,
  522. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  523. if (status)
  524. goto leave;
  525. did_quota = 1;
  526. /* reserve a write to the file entry early on - that we if we
  527. * run out of credits in the allocation path, we can still
  528. * update i_size. */
  529. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  530. OCFS2_JOURNAL_ACCESS_WRITE);
  531. if (status < 0) {
  532. mlog_errno(status);
  533. goto leave;
  534. }
  535. prev_clusters = OCFS2_I(inode)->ip_clusters;
  536. status = ocfs2_add_inode_data(osb,
  537. inode,
  538. &logical_start,
  539. clusters_to_add,
  540. mark_unwritten,
  541. bh,
  542. handle,
  543. data_ac,
  544. meta_ac,
  545. &why);
  546. if ((status < 0) && (status != -EAGAIN)) {
  547. if (status != -ENOSPC)
  548. mlog_errno(status);
  549. goto leave;
  550. }
  551. ocfs2_journal_dirty(handle, bh);
  552. spin_lock(&OCFS2_I(inode)->ip_lock);
  553. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  554. spin_unlock(&OCFS2_I(inode)->ip_lock);
  555. /* Release unused quota reservation */
  556. dquot_free_space(inode,
  557. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  558. did_quota = 0;
  559. if (why != RESTART_NONE && clusters_to_add) {
  560. if (why == RESTART_META) {
  561. mlog(0, "restarting function.\n");
  562. restart_func = 1;
  563. } else {
  564. BUG_ON(why != RESTART_TRANS);
  565. mlog(0, "restarting transaction.\n");
  566. /* TODO: This can be more intelligent. */
  567. credits = ocfs2_calc_extend_credits(osb->sb,
  568. &fe->id2.i_list,
  569. clusters_to_add);
  570. status = ocfs2_extend_trans(handle, credits);
  571. if (status < 0) {
  572. /* handle still has to be committed at
  573. * this point. */
  574. status = -ENOMEM;
  575. mlog_errno(status);
  576. goto leave;
  577. }
  578. goto restarted_transaction;
  579. }
  580. }
  581. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  582. le32_to_cpu(fe->i_clusters),
  583. (unsigned long long)le64_to_cpu(fe->i_size));
  584. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  585. OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
  586. leave:
  587. if (status < 0 && did_quota)
  588. dquot_free_space(inode,
  589. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  590. if (handle) {
  591. ocfs2_commit_trans(osb, handle);
  592. handle = NULL;
  593. }
  594. if (data_ac) {
  595. ocfs2_free_alloc_context(data_ac);
  596. data_ac = NULL;
  597. }
  598. if (meta_ac) {
  599. ocfs2_free_alloc_context(meta_ac);
  600. meta_ac = NULL;
  601. }
  602. if ((!status) && restart_func) {
  603. restart_func = 0;
  604. goto restart_all;
  605. }
  606. brelse(bh);
  607. bh = NULL;
  608. mlog_exit(status);
  609. return status;
  610. }
  611. /* Some parts of this taken from generic_cont_expand, which turned out
  612. * to be too fragile to do exactly what we need without us having to
  613. * worry about recursive locking in ->write_begin() and ->write_end(). */
  614. static int ocfs2_write_zero_page(struct inode *inode,
  615. u64 size)
  616. {
  617. struct address_space *mapping = inode->i_mapping;
  618. struct page *page;
  619. unsigned long index;
  620. unsigned int offset;
  621. handle_t *handle = NULL;
  622. int ret;
  623. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  624. /* ugh. in prepare/commit_write, if from==to==start of block, we
  625. ** skip the prepare. make sure we never send an offset for the start
  626. ** of a block
  627. */
  628. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  629. offset++;
  630. }
  631. index = size >> PAGE_CACHE_SHIFT;
  632. page = grab_cache_page(mapping, index);
  633. if (!page) {
  634. ret = -ENOMEM;
  635. mlog_errno(ret);
  636. goto out;
  637. }
  638. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  639. if (ret < 0) {
  640. mlog_errno(ret);
  641. goto out_unlock;
  642. }
  643. if (ocfs2_should_order_data(inode)) {
  644. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  645. offset);
  646. if (IS_ERR(handle)) {
  647. ret = PTR_ERR(handle);
  648. handle = NULL;
  649. goto out_unlock;
  650. }
  651. }
  652. /* must not update i_size! */
  653. ret = block_commit_write(page, offset, offset);
  654. if (ret < 0)
  655. mlog_errno(ret);
  656. else
  657. ret = 0;
  658. if (handle)
  659. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  660. out_unlock:
  661. unlock_page(page);
  662. page_cache_release(page);
  663. out:
  664. return ret;
  665. }
  666. static int ocfs2_zero_extend(struct inode *inode,
  667. u64 zero_to_size)
  668. {
  669. int ret = 0;
  670. u64 start_off;
  671. struct super_block *sb = inode->i_sb;
  672. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  673. while (start_off < zero_to_size) {
  674. ret = ocfs2_write_zero_page(inode, start_off);
  675. if (ret < 0) {
  676. mlog_errno(ret);
  677. goto out;
  678. }
  679. start_off += sb->s_blocksize;
  680. /*
  681. * Very large extends have the potential to lock up
  682. * the cpu for extended periods of time.
  683. */
  684. cond_resched();
  685. }
  686. out:
  687. return ret;
  688. }
  689. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  690. {
  691. int ret;
  692. u32 clusters_to_add;
  693. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  694. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  695. if (clusters_to_add < oi->ip_clusters)
  696. clusters_to_add = 0;
  697. else
  698. clusters_to_add -= oi->ip_clusters;
  699. if (clusters_to_add) {
  700. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  701. clusters_to_add, 0);
  702. if (ret) {
  703. mlog_errno(ret);
  704. goto out;
  705. }
  706. }
  707. /*
  708. * Call this even if we don't add any clusters to the tree. We
  709. * still need to zero the area between the old i_size and the
  710. * new i_size.
  711. */
  712. ret = ocfs2_zero_extend(inode, zero_to);
  713. if (ret < 0)
  714. mlog_errno(ret);
  715. out:
  716. return ret;
  717. }
  718. static int ocfs2_extend_file(struct inode *inode,
  719. struct buffer_head *di_bh,
  720. u64 new_i_size)
  721. {
  722. int ret = 0;
  723. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  724. BUG_ON(!di_bh);
  725. /* setattr sometimes calls us like this. */
  726. if (new_i_size == 0)
  727. goto out;
  728. if (i_size_read(inode) == new_i_size)
  729. goto out;
  730. BUG_ON(new_i_size < i_size_read(inode));
  731. /*
  732. * Fall through for converting inline data, even if the fs
  733. * supports sparse files.
  734. *
  735. * The check for inline data here is legal - nobody can add
  736. * the feature since we have i_mutex. We must check it again
  737. * after acquiring ip_alloc_sem though, as paths like mmap
  738. * might have raced us to converting the inode to extents.
  739. */
  740. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  741. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  742. goto out_update_size;
  743. /*
  744. * The alloc sem blocks people in read/write from reading our
  745. * allocation until we're done changing it. We depend on
  746. * i_mutex to block other extend/truncate calls while we're
  747. * here.
  748. */
  749. down_write(&oi->ip_alloc_sem);
  750. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  751. /*
  752. * We can optimize small extends by keeping the inodes
  753. * inline data.
  754. */
  755. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  756. up_write(&oi->ip_alloc_sem);
  757. goto out_update_size;
  758. }
  759. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  760. if (ret) {
  761. up_write(&oi->ip_alloc_sem);
  762. mlog_errno(ret);
  763. goto out;
  764. }
  765. }
  766. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  767. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  768. up_write(&oi->ip_alloc_sem);
  769. if (ret < 0) {
  770. mlog_errno(ret);
  771. goto out;
  772. }
  773. out_update_size:
  774. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  775. if (ret < 0)
  776. mlog_errno(ret);
  777. out:
  778. return ret;
  779. }
  780. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  781. {
  782. int status = 0, size_change;
  783. struct inode *inode = dentry->d_inode;
  784. struct super_block *sb = inode->i_sb;
  785. struct ocfs2_super *osb = OCFS2_SB(sb);
  786. struct buffer_head *bh = NULL;
  787. handle_t *handle = NULL;
  788. int qtype;
  789. struct dquot *transfer_from[MAXQUOTAS] = { };
  790. struct dquot *transfer_to[MAXQUOTAS] = { };
  791. mlog_entry("(0x%p, '%.*s')\n", dentry,
  792. dentry->d_name.len, dentry->d_name.name);
  793. /* ensuring we don't even attempt to truncate a symlink */
  794. if (S_ISLNK(inode->i_mode))
  795. attr->ia_valid &= ~ATTR_SIZE;
  796. if (attr->ia_valid & ATTR_MODE)
  797. mlog(0, "mode change: %d\n", attr->ia_mode);
  798. if (attr->ia_valid & ATTR_UID)
  799. mlog(0, "uid change: %d\n", attr->ia_uid);
  800. if (attr->ia_valid & ATTR_GID)
  801. mlog(0, "gid change: %d\n", attr->ia_gid);
  802. if (attr->ia_valid & ATTR_SIZE)
  803. mlog(0, "size change...\n");
  804. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  805. mlog(0, "time change...\n");
  806. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  807. | ATTR_GID | ATTR_UID | ATTR_MODE)
  808. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  809. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  810. return 0;
  811. }
  812. status = inode_change_ok(inode, attr);
  813. if (status)
  814. return status;
  815. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  816. if (size_change) {
  817. dquot_initialize(inode);
  818. status = ocfs2_rw_lock(inode, 1);
  819. if (status < 0) {
  820. mlog_errno(status);
  821. goto bail;
  822. }
  823. }
  824. status = ocfs2_inode_lock(inode, &bh, 1);
  825. if (status < 0) {
  826. if (status != -ENOENT)
  827. mlog_errno(status);
  828. goto bail_unlock_rw;
  829. }
  830. if (size_change && attr->ia_size != i_size_read(inode)) {
  831. status = inode_newsize_ok(inode, attr->ia_size);
  832. if (status)
  833. goto bail_unlock;
  834. if (i_size_read(inode) > attr->ia_size) {
  835. if (ocfs2_should_order_data(inode)) {
  836. status = ocfs2_begin_ordered_truncate(inode,
  837. attr->ia_size);
  838. if (status)
  839. goto bail_unlock;
  840. }
  841. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  842. } else
  843. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  844. if (status < 0) {
  845. if (status != -ENOSPC)
  846. mlog_errno(status);
  847. status = -ENOSPC;
  848. goto bail_unlock;
  849. }
  850. }
  851. if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  852. (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  853. /*
  854. * Gather pointers to quota structures so that allocation /
  855. * freeing of quota structures happens here and not inside
  856. * dquot_transfer() where we have problems with lock ordering
  857. */
  858. if (attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid
  859. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  860. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  861. transfer_to[USRQUOTA] = dqget(sb, attr->ia_uid,
  862. USRQUOTA);
  863. transfer_from[USRQUOTA] = dqget(sb, inode->i_uid,
  864. USRQUOTA);
  865. if (!transfer_to[USRQUOTA] || !transfer_from[USRQUOTA]) {
  866. status = -ESRCH;
  867. goto bail_unlock;
  868. }
  869. }
  870. if (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid
  871. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  872. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  873. transfer_to[GRPQUOTA] = dqget(sb, attr->ia_gid,
  874. GRPQUOTA);
  875. transfer_from[GRPQUOTA] = dqget(sb, inode->i_gid,
  876. GRPQUOTA);
  877. if (!transfer_to[GRPQUOTA] || !transfer_from[GRPQUOTA]) {
  878. status = -ESRCH;
  879. goto bail_unlock;
  880. }
  881. }
  882. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  883. 2 * ocfs2_quota_trans_credits(sb));
  884. if (IS_ERR(handle)) {
  885. status = PTR_ERR(handle);
  886. mlog_errno(status);
  887. goto bail_unlock;
  888. }
  889. status = dquot_transfer(inode, attr);
  890. if (status < 0)
  891. goto bail_commit;
  892. } else {
  893. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  894. if (IS_ERR(handle)) {
  895. status = PTR_ERR(handle);
  896. mlog_errno(status);
  897. goto bail_unlock;
  898. }
  899. }
  900. /*
  901. * This will intentionally not wind up calling vmtruncate(),
  902. * since all the work for a size change has been done above.
  903. * Otherwise, we could get into problems with truncate as
  904. * ip_alloc_sem is used there to protect against i_size
  905. * changes.
  906. */
  907. status = inode_setattr(inode, attr);
  908. if (status < 0) {
  909. mlog_errno(status);
  910. goto bail_commit;
  911. }
  912. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  913. if (status < 0)
  914. mlog_errno(status);
  915. bail_commit:
  916. ocfs2_commit_trans(osb, handle);
  917. bail_unlock:
  918. ocfs2_inode_unlock(inode, 1);
  919. bail_unlock_rw:
  920. if (size_change)
  921. ocfs2_rw_unlock(inode, 1);
  922. bail:
  923. brelse(bh);
  924. /* Release quota pointers in case we acquired them */
  925. for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
  926. dqput(transfer_to[qtype]);
  927. dqput(transfer_from[qtype]);
  928. }
  929. if (!status && attr->ia_valid & ATTR_MODE) {
  930. status = ocfs2_acl_chmod(inode);
  931. if (status < 0)
  932. mlog_errno(status);
  933. }
  934. mlog_exit(status);
  935. return status;
  936. }
  937. int ocfs2_getattr(struct vfsmount *mnt,
  938. struct dentry *dentry,
  939. struct kstat *stat)
  940. {
  941. struct inode *inode = dentry->d_inode;
  942. struct super_block *sb = dentry->d_inode->i_sb;
  943. struct ocfs2_super *osb = sb->s_fs_info;
  944. int err;
  945. mlog_entry_void();
  946. err = ocfs2_inode_revalidate(dentry);
  947. if (err) {
  948. if (err != -ENOENT)
  949. mlog_errno(err);
  950. goto bail;
  951. }
  952. generic_fillattr(inode, stat);
  953. /* We set the blksize from the cluster size for performance */
  954. stat->blksize = osb->s_clustersize;
  955. bail:
  956. mlog_exit(err);
  957. return err;
  958. }
  959. int ocfs2_permission(struct inode *inode, int mask)
  960. {
  961. int ret;
  962. mlog_entry_void();
  963. ret = ocfs2_inode_lock(inode, NULL, 0);
  964. if (ret) {
  965. if (ret != -ENOENT)
  966. mlog_errno(ret);
  967. goto out;
  968. }
  969. ret = generic_permission(inode, mask, ocfs2_check_acl);
  970. ocfs2_inode_unlock(inode, 0);
  971. out:
  972. mlog_exit(ret);
  973. return ret;
  974. }
  975. static int __ocfs2_write_remove_suid(struct inode *inode,
  976. struct buffer_head *bh)
  977. {
  978. int ret;
  979. handle_t *handle;
  980. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  981. struct ocfs2_dinode *di;
  982. mlog_entry("(Inode %llu, mode 0%o)\n",
  983. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  984. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  985. if (IS_ERR(handle)) {
  986. ret = PTR_ERR(handle);
  987. mlog_errno(ret);
  988. goto out;
  989. }
  990. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  991. OCFS2_JOURNAL_ACCESS_WRITE);
  992. if (ret < 0) {
  993. mlog_errno(ret);
  994. goto out_trans;
  995. }
  996. inode->i_mode &= ~S_ISUID;
  997. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  998. inode->i_mode &= ~S_ISGID;
  999. di = (struct ocfs2_dinode *) bh->b_data;
  1000. di->i_mode = cpu_to_le16(inode->i_mode);
  1001. ocfs2_journal_dirty(handle, bh);
  1002. out_trans:
  1003. ocfs2_commit_trans(osb, handle);
  1004. out:
  1005. mlog_exit(ret);
  1006. return ret;
  1007. }
  1008. /*
  1009. * Will look for holes and unwritten extents in the range starting at
  1010. * pos for count bytes (inclusive).
  1011. */
  1012. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1013. size_t count)
  1014. {
  1015. int ret = 0;
  1016. unsigned int extent_flags;
  1017. u32 cpos, clusters, extent_len, phys_cpos;
  1018. struct super_block *sb = inode->i_sb;
  1019. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1020. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1021. while (clusters) {
  1022. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1023. &extent_flags);
  1024. if (ret < 0) {
  1025. mlog_errno(ret);
  1026. goto out;
  1027. }
  1028. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1029. ret = 1;
  1030. break;
  1031. }
  1032. if (extent_len > clusters)
  1033. extent_len = clusters;
  1034. clusters -= extent_len;
  1035. cpos += extent_len;
  1036. }
  1037. out:
  1038. return ret;
  1039. }
  1040. static int ocfs2_write_remove_suid(struct inode *inode)
  1041. {
  1042. int ret;
  1043. struct buffer_head *bh = NULL;
  1044. ret = ocfs2_read_inode_block(inode, &bh);
  1045. if (ret < 0) {
  1046. mlog_errno(ret);
  1047. goto out;
  1048. }
  1049. ret = __ocfs2_write_remove_suid(inode, bh);
  1050. out:
  1051. brelse(bh);
  1052. return ret;
  1053. }
  1054. /*
  1055. * Allocate enough extents to cover the region starting at byte offset
  1056. * start for len bytes. Existing extents are skipped, any extents
  1057. * added are marked as "unwritten".
  1058. */
  1059. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1060. u64 start, u64 len)
  1061. {
  1062. int ret;
  1063. u32 cpos, phys_cpos, clusters, alloc_size;
  1064. u64 end = start + len;
  1065. struct buffer_head *di_bh = NULL;
  1066. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1067. ret = ocfs2_read_inode_block(inode, &di_bh);
  1068. if (ret) {
  1069. mlog_errno(ret);
  1070. goto out;
  1071. }
  1072. /*
  1073. * Nothing to do if the requested reservation range
  1074. * fits within the inode.
  1075. */
  1076. if (ocfs2_size_fits_inline_data(di_bh, end))
  1077. goto out;
  1078. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1079. if (ret) {
  1080. mlog_errno(ret);
  1081. goto out;
  1082. }
  1083. }
  1084. /*
  1085. * We consider both start and len to be inclusive.
  1086. */
  1087. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1088. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1089. clusters -= cpos;
  1090. while (clusters) {
  1091. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1092. &alloc_size, NULL);
  1093. if (ret) {
  1094. mlog_errno(ret);
  1095. goto out;
  1096. }
  1097. /*
  1098. * Hole or existing extent len can be arbitrary, so
  1099. * cap it to our own allocation request.
  1100. */
  1101. if (alloc_size > clusters)
  1102. alloc_size = clusters;
  1103. if (phys_cpos) {
  1104. /*
  1105. * We already have an allocation at this
  1106. * region so we can safely skip it.
  1107. */
  1108. goto next;
  1109. }
  1110. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1111. if (ret) {
  1112. if (ret != -ENOSPC)
  1113. mlog_errno(ret);
  1114. goto out;
  1115. }
  1116. next:
  1117. cpos += alloc_size;
  1118. clusters -= alloc_size;
  1119. }
  1120. ret = 0;
  1121. out:
  1122. brelse(di_bh);
  1123. return ret;
  1124. }
  1125. /*
  1126. * Truncate a byte range, avoiding pages within partial clusters. This
  1127. * preserves those pages for the zeroing code to write to.
  1128. */
  1129. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1130. u64 byte_len)
  1131. {
  1132. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1133. loff_t start, end;
  1134. struct address_space *mapping = inode->i_mapping;
  1135. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1136. end = byte_start + byte_len;
  1137. end = end & ~(osb->s_clustersize - 1);
  1138. if (start < end) {
  1139. unmap_mapping_range(mapping, start, end - start, 0);
  1140. truncate_inode_pages_range(mapping, start, end - 1);
  1141. }
  1142. }
  1143. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1144. u64 start, u64 len)
  1145. {
  1146. int ret = 0;
  1147. u64 tmpend, end = start + len;
  1148. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1149. unsigned int csize = osb->s_clustersize;
  1150. handle_t *handle;
  1151. /*
  1152. * The "start" and "end" values are NOT necessarily part of
  1153. * the range whose allocation is being deleted. Rather, this
  1154. * is what the user passed in with the request. We must zero
  1155. * partial clusters here. There's no need to worry about
  1156. * physical allocation - the zeroing code knows to skip holes.
  1157. */
  1158. mlog(0, "byte start: %llu, end: %llu\n",
  1159. (unsigned long long)start, (unsigned long long)end);
  1160. /*
  1161. * If both edges are on a cluster boundary then there's no
  1162. * zeroing required as the region is part of the allocation to
  1163. * be truncated.
  1164. */
  1165. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1166. goto out;
  1167. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1168. if (IS_ERR(handle)) {
  1169. ret = PTR_ERR(handle);
  1170. mlog_errno(ret);
  1171. goto out;
  1172. }
  1173. /*
  1174. * We want to get the byte offset of the end of the 1st cluster.
  1175. */
  1176. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1177. if (tmpend > end)
  1178. tmpend = end;
  1179. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1180. (unsigned long long)start, (unsigned long long)tmpend);
  1181. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1182. if (ret)
  1183. mlog_errno(ret);
  1184. if (tmpend < end) {
  1185. /*
  1186. * This may make start and end equal, but the zeroing
  1187. * code will skip any work in that case so there's no
  1188. * need to catch it up here.
  1189. */
  1190. start = end & ~(osb->s_clustersize - 1);
  1191. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1192. (unsigned long long)start, (unsigned long long)end);
  1193. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1194. if (ret)
  1195. mlog_errno(ret);
  1196. }
  1197. ocfs2_commit_trans(osb, handle);
  1198. out:
  1199. return ret;
  1200. }
  1201. static int ocfs2_remove_inode_range(struct inode *inode,
  1202. struct buffer_head *di_bh, u64 byte_start,
  1203. u64 byte_len)
  1204. {
  1205. int ret = 0;
  1206. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1207. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1208. struct ocfs2_cached_dealloc_ctxt dealloc;
  1209. struct address_space *mapping = inode->i_mapping;
  1210. struct ocfs2_extent_tree et;
  1211. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1212. ocfs2_init_dealloc_ctxt(&dealloc);
  1213. if (byte_len == 0)
  1214. return 0;
  1215. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1216. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1217. byte_start + byte_len, 0);
  1218. if (ret) {
  1219. mlog_errno(ret);
  1220. goto out;
  1221. }
  1222. /*
  1223. * There's no need to get fancy with the page cache
  1224. * truncate of an inline-data inode. We're talking
  1225. * about less than a page here, which will be cached
  1226. * in the dinode buffer anyway.
  1227. */
  1228. unmap_mapping_range(mapping, 0, 0, 0);
  1229. truncate_inode_pages(mapping, 0);
  1230. goto out;
  1231. }
  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_btree_range(inode, &et, cpos,
  1260. phys_cpos, alloc_size,
  1261. 0, &dealloc, 0);
  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_inode_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_inode_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_inode_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_inode_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_inode_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_inode_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_inode_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_inode_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_inode_unlock:
  1388. brelse(di_bh);
  1389. ocfs2_inode_unlock(inode, 1);
  1390. out_rw_unlock:
  1391. ocfs2_rw_unlock(inode, 1);
  1392. out:
  1393. mutex_unlock(&inode->i_mutex);
  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. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1432. size_t count)
  1433. {
  1434. int ret = 0;
  1435. unsigned int extent_flags;
  1436. u32 cpos, clusters, extent_len, phys_cpos;
  1437. struct super_block *sb = inode->i_sb;
  1438. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1439. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1440. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1441. return 0;
  1442. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1443. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1444. while (clusters) {
  1445. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1446. &extent_flags);
  1447. if (ret < 0) {
  1448. mlog_errno(ret);
  1449. goto out;
  1450. }
  1451. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1452. ret = 1;
  1453. break;
  1454. }
  1455. if (extent_len > clusters)
  1456. extent_len = clusters;
  1457. clusters -= extent_len;
  1458. cpos += extent_len;
  1459. }
  1460. out:
  1461. return ret;
  1462. }
  1463. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1464. loff_t pos, size_t count,
  1465. int *meta_level)
  1466. {
  1467. int ret;
  1468. struct buffer_head *di_bh = NULL;
  1469. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1470. u32 clusters =
  1471. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1472. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1473. if (ret) {
  1474. mlog_errno(ret);
  1475. goto out;
  1476. }
  1477. *meta_level = 1;
  1478. ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
  1479. if (ret)
  1480. mlog_errno(ret);
  1481. out:
  1482. brelse(di_bh);
  1483. return ret;
  1484. }
  1485. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1486. loff_t *ppos,
  1487. size_t count,
  1488. int appending,
  1489. int *direct_io,
  1490. int *has_refcount)
  1491. {
  1492. int ret = 0, meta_level = 0;
  1493. struct inode *inode = dentry->d_inode;
  1494. loff_t saved_pos, end;
  1495. /*
  1496. * We start with a read level meta lock and only jump to an ex
  1497. * if we need to make modifications here.
  1498. */
  1499. for(;;) {
  1500. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1501. if (ret < 0) {
  1502. meta_level = -1;
  1503. mlog_errno(ret);
  1504. goto out;
  1505. }
  1506. /* Clear suid / sgid if necessary. We do this here
  1507. * instead of later in the write path because
  1508. * remove_suid() calls ->setattr without any hint that
  1509. * we may have already done our cluster locking. Since
  1510. * ocfs2_setattr() *must* take cluster locks to
  1511. * proceeed, this will lead us to recursively lock the
  1512. * inode. There's also the dinode i_size state which
  1513. * can be lost via setattr during extending writes (we
  1514. * set inode->i_size at the end of a write. */
  1515. if (should_remove_suid(dentry)) {
  1516. if (meta_level == 0) {
  1517. ocfs2_inode_unlock(inode, meta_level);
  1518. meta_level = 1;
  1519. continue;
  1520. }
  1521. ret = ocfs2_write_remove_suid(inode);
  1522. if (ret < 0) {
  1523. mlog_errno(ret);
  1524. goto out_unlock;
  1525. }
  1526. }
  1527. /* work on a copy of ppos until we're sure that we won't have
  1528. * to recalculate it due to relocking. */
  1529. if (appending) {
  1530. saved_pos = i_size_read(inode);
  1531. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1532. } else {
  1533. saved_pos = *ppos;
  1534. }
  1535. end = saved_pos + count;
  1536. ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
  1537. if (ret == 1) {
  1538. ocfs2_inode_unlock(inode, meta_level);
  1539. meta_level = -1;
  1540. ret = ocfs2_prepare_inode_for_refcount(inode,
  1541. saved_pos,
  1542. count,
  1543. &meta_level);
  1544. if (has_refcount)
  1545. *has_refcount = 1;
  1546. if (direct_io)
  1547. *direct_io = 0;
  1548. }
  1549. if (ret < 0) {
  1550. mlog_errno(ret);
  1551. goto out_unlock;
  1552. }
  1553. /*
  1554. * Skip the O_DIRECT checks if we don't need
  1555. * them.
  1556. */
  1557. if (!direct_io || !(*direct_io))
  1558. break;
  1559. /*
  1560. * There's no sane way to do direct writes to an inode
  1561. * with inline data.
  1562. */
  1563. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1564. *direct_io = 0;
  1565. break;
  1566. }
  1567. /*
  1568. * Allowing concurrent direct writes means
  1569. * i_size changes wouldn't be synchronized, so
  1570. * one node could wind up truncating another
  1571. * nodes writes.
  1572. */
  1573. if (end > i_size_read(inode)) {
  1574. *direct_io = 0;
  1575. break;
  1576. }
  1577. /*
  1578. * We don't fill holes during direct io, so
  1579. * check for them here. If any are found, the
  1580. * caller will have to retake some cluster
  1581. * locks and initiate the io as buffered.
  1582. */
  1583. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1584. if (ret == 1) {
  1585. *direct_io = 0;
  1586. ret = 0;
  1587. } else if (ret < 0)
  1588. mlog_errno(ret);
  1589. break;
  1590. }
  1591. if (appending)
  1592. *ppos = saved_pos;
  1593. out_unlock:
  1594. if (meta_level >= 0)
  1595. ocfs2_inode_unlock(inode, meta_level);
  1596. out:
  1597. return ret;
  1598. }
  1599. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1600. const struct iovec *iov,
  1601. unsigned long nr_segs,
  1602. loff_t pos)
  1603. {
  1604. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1605. int can_do_direct, has_refcount = 0;
  1606. ssize_t written = 0;
  1607. size_t ocount; /* original count */
  1608. size_t count; /* after file limit checks */
  1609. loff_t old_size, *ppos = &iocb->ki_pos;
  1610. u32 old_clusters;
  1611. struct file *file = iocb->ki_filp;
  1612. struct inode *inode = file->f_path.dentry->d_inode;
  1613. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1614. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1615. (unsigned int)nr_segs,
  1616. file->f_path.dentry->d_name.len,
  1617. file->f_path.dentry->d_name.name);
  1618. if (iocb->ki_left == 0)
  1619. return 0;
  1620. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1621. appending = file->f_flags & O_APPEND ? 1 : 0;
  1622. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1623. mutex_lock(&inode->i_mutex);
  1624. relock:
  1625. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1626. if (direct_io) {
  1627. down_read(&inode->i_alloc_sem);
  1628. have_alloc_sem = 1;
  1629. }
  1630. /* concurrent O_DIRECT writes are allowed */
  1631. rw_level = !direct_io;
  1632. ret = ocfs2_rw_lock(inode, rw_level);
  1633. if (ret < 0) {
  1634. mlog_errno(ret);
  1635. goto out_sems;
  1636. }
  1637. can_do_direct = direct_io;
  1638. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1639. iocb->ki_left, appending,
  1640. &can_do_direct, &has_refcount);
  1641. if (ret < 0) {
  1642. mlog_errno(ret);
  1643. goto out;
  1644. }
  1645. /*
  1646. * We can't complete the direct I/O as requested, fall back to
  1647. * buffered I/O.
  1648. */
  1649. if (direct_io && !can_do_direct) {
  1650. ocfs2_rw_unlock(inode, rw_level);
  1651. up_read(&inode->i_alloc_sem);
  1652. have_alloc_sem = 0;
  1653. rw_level = -1;
  1654. direct_io = 0;
  1655. goto relock;
  1656. }
  1657. /*
  1658. * To later detect whether a journal commit for sync writes is
  1659. * necessary, we sample i_size, and cluster count here.
  1660. */
  1661. old_size = i_size_read(inode);
  1662. old_clusters = OCFS2_I(inode)->ip_clusters;
  1663. /* communicate with ocfs2_dio_end_io */
  1664. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1665. if (direct_io) {
  1666. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1667. VERIFY_READ);
  1668. if (ret)
  1669. goto out_dio;
  1670. count = ocount;
  1671. ret = generic_write_checks(file, ppos, &count,
  1672. S_ISBLK(inode->i_mode));
  1673. if (ret)
  1674. goto out_dio;
  1675. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1676. ppos, count, ocount);
  1677. if (written < 0) {
  1678. /*
  1679. * direct write may have instantiated a few
  1680. * blocks outside i_size. Trim these off again.
  1681. * Don't need i_size_read because we hold i_mutex.
  1682. */
  1683. if (*ppos + count > inode->i_size)
  1684. vmtruncate(inode, inode->i_size);
  1685. ret = written;
  1686. goto out_dio;
  1687. }
  1688. } else {
  1689. written = __generic_file_aio_write(iocb, iov, nr_segs, ppos);
  1690. }
  1691. out_dio:
  1692. /* buffered aio wouldn't have proper lock coverage today */
  1693. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1694. if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
  1695. ((file->f_flags & O_DIRECT) && has_refcount)) {
  1696. ret = filemap_fdatawrite_range(file->f_mapping, pos,
  1697. pos + count - 1);
  1698. if (ret < 0)
  1699. written = ret;
  1700. if (!ret && (old_size != i_size_read(inode) ||
  1701. old_clusters != OCFS2_I(inode)->ip_clusters ||
  1702. has_refcount)) {
  1703. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  1704. if (ret < 0)
  1705. written = ret;
  1706. }
  1707. if (!ret)
  1708. ret = filemap_fdatawait_range(file->f_mapping, pos,
  1709. pos + count - 1);
  1710. }
  1711. /*
  1712. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1713. * function pointer which is called when o_direct io completes so that
  1714. * it can unlock our rw lock. (it's the clustered equivalent of
  1715. * i_alloc_sem; protects truncate from racing with pending ios).
  1716. * Unfortunately there are error cases which call end_io and others
  1717. * that don't. so we don't have to unlock the rw_lock if either an
  1718. * async dio is going to do it in the future or an end_io after an
  1719. * error has already done it.
  1720. */
  1721. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  1722. rw_level = -1;
  1723. have_alloc_sem = 0;
  1724. }
  1725. out:
  1726. if (rw_level != -1)
  1727. ocfs2_rw_unlock(inode, rw_level);
  1728. out_sems:
  1729. if (have_alloc_sem)
  1730. up_read(&inode->i_alloc_sem);
  1731. mutex_unlock(&inode->i_mutex);
  1732. if (written)
  1733. ret = written;
  1734. mlog_exit(ret);
  1735. return ret;
  1736. }
  1737. static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
  1738. struct file *out,
  1739. struct splice_desc *sd)
  1740. {
  1741. int ret;
  1742. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, &sd->pos,
  1743. sd->total_len, 0, NULL, NULL);
  1744. if (ret < 0) {
  1745. mlog_errno(ret);
  1746. return ret;
  1747. }
  1748. return splice_from_pipe_feed(pipe, sd, pipe_to_file);
  1749. }
  1750. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1751. struct file *out,
  1752. loff_t *ppos,
  1753. size_t len,
  1754. unsigned int flags)
  1755. {
  1756. int ret;
  1757. struct address_space *mapping = out->f_mapping;
  1758. struct inode *inode = mapping->host;
  1759. struct splice_desc sd = {
  1760. .total_len = len,
  1761. .flags = flags,
  1762. .pos = *ppos,
  1763. .u.file = out,
  1764. };
  1765. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1766. (unsigned int)len,
  1767. out->f_path.dentry->d_name.len,
  1768. out->f_path.dentry->d_name.name);
  1769. if (pipe->inode)
  1770. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_PARENT);
  1771. splice_from_pipe_begin(&sd);
  1772. do {
  1773. ret = splice_from_pipe_next(pipe, &sd);
  1774. if (ret <= 0)
  1775. break;
  1776. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1777. ret = ocfs2_rw_lock(inode, 1);
  1778. if (ret < 0)
  1779. mlog_errno(ret);
  1780. else {
  1781. ret = ocfs2_splice_to_file(pipe, out, &sd);
  1782. ocfs2_rw_unlock(inode, 1);
  1783. }
  1784. mutex_unlock(&inode->i_mutex);
  1785. } while (ret > 0);
  1786. splice_from_pipe_end(pipe, &sd);
  1787. if (pipe->inode)
  1788. mutex_unlock(&pipe->inode->i_mutex);
  1789. if (sd.num_spliced)
  1790. ret = sd.num_spliced;
  1791. if (ret > 0) {
  1792. unsigned long nr_pages;
  1793. int err;
  1794. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1795. err = generic_write_sync(out, *ppos, ret);
  1796. if (err)
  1797. ret = err;
  1798. else
  1799. *ppos += ret;
  1800. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  1801. }
  1802. mlog_exit(ret);
  1803. return ret;
  1804. }
  1805. static ssize_t ocfs2_file_splice_read(struct file *in,
  1806. loff_t *ppos,
  1807. struct pipe_inode_info *pipe,
  1808. size_t len,
  1809. unsigned int flags)
  1810. {
  1811. int ret = 0, lock_level = 0;
  1812. struct inode *inode = in->f_path.dentry->d_inode;
  1813. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1814. (unsigned int)len,
  1815. in->f_path.dentry->d_name.len,
  1816. in->f_path.dentry->d_name.name);
  1817. /*
  1818. * See the comment in ocfs2_file_aio_read()
  1819. */
  1820. ret = ocfs2_inode_lock_atime(inode, in->f_vfsmnt, &lock_level);
  1821. if (ret < 0) {
  1822. mlog_errno(ret);
  1823. goto bail;
  1824. }
  1825. ocfs2_inode_unlock(inode, lock_level);
  1826. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1827. bail:
  1828. mlog_exit(ret);
  1829. return ret;
  1830. }
  1831. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1832. const struct iovec *iov,
  1833. unsigned long nr_segs,
  1834. loff_t pos)
  1835. {
  1836. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1837. struct file *filp = iocb->ki_filp;
  1838. struct inode *inode = filp->f_path.dentry->d_inode;
  1839. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1840. (unsigned int)nr_segs,
  1841. filp->f_path.dentry->d_name.len,
  1842. filp->f_path.dentry->d_name.name);
  1843. if (!inode) {
  1844. ret = -EINVAL;
  1845. mlog_errno(ret);
  1846. goto bail;
  1847. }
  1848. /*
  1849. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1850. * need locks to protect pending reads from racing with truncate.
  1851. */
  1852. if (filp->f_flags & O_DIRECT) {
  1853. down_read(&inode->i_alloc_sem);
  1854. have_alloc_sem = 1;
  1855. ret = ocfs2_rw_lock(inode, 0);
  1856. if (ret < 0) {
  1857. mlog_errno(ret);
  1858. goto bail;
  1859. }
  1860. rw_level = 0;
  1861. /* communicate with ocfs2_dio_end_io */
  1862. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1863. }
  1864. /*
  1865. * We're fine letting folks race truncates and extending
  1866. * writes with read across the cluster, just like they can
  1867. * locally. Hence no rw_lock during read.
  1868. *
  1869. * Take and drop the meta data lock to update inode fields
  1870. * like i_size. This allows the checks down below
  1871. * generic_file_aio_read() a chance of actually working.
  1872. */
  1873. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1874. if (ret < 0) {
  1875. mlog_errno(ret);
  1876. goto bail;
  1877. }
  1878. ocfs2_inode_unlock(inode, lock_level);
  1879. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1880. if (ret == -EINVAL)
  1881. mlog(0, "generic_file_aio_read returned -EINVAL\n");
  1882. /* buffered aio wouldn't have proper lock coverage today */
  1883. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1884. /* see ocfs2_file_aio_write */
  1885. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1886. rw_level = -1;
  1887. have_alloc_sem = 0;
  1888. }
  1889. bail:
  1890. if (have_alloc_sem)
  1891. up_read(&inode->i_alloc_sem);
  1892. if (rw_level != -1)
  1893. ocfs2_rw_unlock(inode, rw_level);
  1894. mlog_exit(ret);
  1895. return ret;
  1896. }
  1897. const struct inode_operations ocfs2_file_iops = {
  1898. .setattr = ocfs2_setattr,
  1899. .getattr = ocfs2_getattr,
  1900. .permission = ocfs2_permission,
  1901. .setxattr = generic_setxattr,
  1902. .getxattr = generic_getxattr,
  1903. .listxattr = ocfs2_listxattr,
  1904. .removexattr = generic_removexattr,
  1905. .fallocate = ocfs2_fallocate,
  1906. .fiemap = ocfs2_fiemap,
  1907. };
  1908. const struct inode_operations ocfs2_special_file_iops = {
  1909. .setattr = ocfs2_setattr,
  1910. .getattr = ocfs2_getattr,
  1911. .permission = ocfs2_permission,
  1912. };
  1913. /*
  1914. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  1915. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  1916. */
  1917. const struct file_operations ocfs2_fops = {
  1918. .llseek = generic_file_llseek,
  1919. .read = do_sync_read,
  1920. .write = do_sync_write,
  1921. .mmap = ocfs2_mmap,
  1922. .fsync = ocfs2_sync_file,
  1923. .release = ocfs2_file_release,
  1924. .open = ocfs2_file_open,
  1925. .aio_read = ocfs2_file_aio_read,
  1926. .aio_write = ocfs2_file_aio_write,
  1927. .unlocked_ioctl = ocfs2_ioctl,
  1928. #ifdef CONFIG_COMPAT
  1929. .compat_ioctl = ocfs2_compat_ioctl,
  1930. #endif
  1931. .lock = ocfs2_lock,
  1932. .flock = ocfs2_flock,
  1933. .splice_read = ocfs2_file_splice_read,
  1934. .splice_write = ocfs2_file_splice_write,
  1935. };
  1936. const struct file_operations ocfs2_dops = {
  1937. .llseek = generic_file_llseek,
  1938. .read = generic_read_dir,
  1939. .readdir = ocfs2_readdir,
  1940. .fsync = ocfs2_sync_file,
  1941. .release = ocfs2_dir_release,
  1942. .open = ocfs2_dir_open,
  1943. .unlocked_ioctl = ocfs2_ioctl,
  1944. #ifdef CONFIG_COMPAT
  1945. .compat_ioctl = ocfs2_compat_ioctl,
  1946. #endif
  1947. .lock = ocfs2_lock,
  1948. .flock = ocfs2_flock,
  1949. };
  1950. /*
  1951. * POSIX-lockless variants of our file_operations.
  1952. *
  1953. * These will be used if the underlying cluster stack does not support
  1954. * posix file locking, if the user passes the "localflocks" mount
  1955. * option, or if we have a local-only fs.
  1956. *
  1957. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  1958. * so we still want it in the case of no stack support for
  1959. * plocks. Internally, it will do the right thing when asked to ignore
  1960. * the cluster.
  1961. */
  1962. const struct file_operations ocfs2_fops_no_plocks = {
  1963. .llseek = generic_file_llseek,
  1964. .read = do_sync_read,
  1965. .write = do_sync_write,
  1966. .mmap = ocfs2_mmap,
  1967. .fsync = ocfs2_sync_file,
  1968. .release = ocfs2_file_release,
  1969. .open = ocfs2_file_open,
  1970. .aio_read = ocfs2_file_aio_read,
  1971. .aio_write = ocfs2_file_aio_write,
  1972. .unlocked_ioctl = ocfs2_ioctl,
  1973. #ifdef CONFIG_COMPAT
  1974. .compat_ioctl = ocfs2_compat_ioctl,
  1975. #endif
  1976. .flock = ocfs2_flock,
  1977. .splice_read = ocfs2_file_splice_read,
  1978. .splice_write = ocfs2_file_splice_write,
  1979. };
  1980. const struct file_operations ocfs2_dops_no_plocks = {
  1981. .llseek = generic_file_llseek,
  1982. .read = generic_read_dir,
  1983. .readdir = ocfs2_readdir,
  1984. .fsync = ocfs2_sync_file,
  1985. .release = ocfs2_dir_release,
  1986. .open = ocfs2_dir_open,
  1987. .unlocked_ioctl = ocfs2_ioctl,
  1988. #ifdef CONFIG_COMPAT
  1989. .compat_ioctl = ocfs2_compat_ioctl,
  1990. #endif
  1991. .flock = ocfs2_flock,
  1992. };