file.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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. status = 0;
  564. } else {
  565. BUG_ON(why != RESTART_TRANS);
  566. mlog(0, "restarting transaction.\n");
  567. /* TODO: This can be more intelligent. */
  568. credits = ocfs2_calc_extend_credits(osb->sb,
  569. &fe->id2.i_list,
  570. clusters_to_add);
  571. status = ocfs2_extend_trans(handle, credits);
  572. if (status < 0) {
  573. /* handle still has to be committed at
  574. * this point. */
  575. status = -ENOMEM;
  576. mlog_errno(status);
  577. goto leave;
  578. }
  579. goto restarted_transaction;
  580. }
  581. }
  582. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  583. le32_to_cpu(fe->i_clusters),
  584. (unsigned long long)le64_to_cpu(fe->i_size));
  585. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  586. OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
  587. leave:
  588. if (status < 0 && did_quota)
  589. dquot_free_space(inode,
  590. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  591. if (handle) {
  592. ocfs2_commit_trans(osb, handle);
  593. handle = NULL;
  594. }
  595. if (data_ac) {
  596. ocfs2_free_alloc_context(data_ac);
  597. data_ac = NULL;
  598. }
  599. if (meta_ac) {
  600. ocfs2_free_alloc_context(meta_ac);
  601. meta_ac = NULL;
  602. }
  603. if ((!status) && restart_func) {
  604. restart_func = 0;
  605. goto restart_all;
  606. }
  607. brelse(bh);
  608. bh = NULL;
  609. mlog_exit(status);
  610. return status;
  611. }
  612. /* Some parts of this taken from generic_cont_expand, which turned out
  613. * to be too fragile to do exactly what we need without us having to
  614. * worry about recursive locking in ->write_begin() and ->write_end(). */
  615. static int ocfs2_write_zero_page(struct inode *inode,
  616. u64 size)
  617. {
  618. struct address_space *mapping = inode->i_mapping;
  619. struct page *page;
  620. unsigned long index;
  621. unsigned int offset;
  622. handle_t *handle = NULL;
  623. int ret;
  624. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  625. /* ugh. in prepare/commit_write, if from==to==start of block, we
  626. ** skip the prepare. make sure we never send an offset for the start
  627. ** of a block
  628. */
  629. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  630. offset++;
  631. }
  632. index = size >> PAGE_CACHE_SHIFT;
  633. page = grab_cache_page(mapping, index);
  634. if (!page) {
  635. ret = -ENOMEM;
  636. mlog_errno(ret);
  637. goto out;
  638. }
  639. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  640. if (ret < 0) {
  641. mlog_errno(ret);
  642. goto out_unlock;
  643. }
  644. if (ocfs2_should_order_data(inode)) {
  645. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  646. offset);
  647. if (IS_ERR(handle)) {
  648. ret = PTR_ERR(handle);
  649. handle = NULL;
  650. goto out_unlock;
  651. }
  652. }
  653. /* must not update i_size! */
  654. ret = block_commit_write(page, offset, offset);
  655. if (ret < 0)
  656. mlog_errno(ret);
  657. else
  658. ret = 0;
  659. if (handle)
  660. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  661. out_unlock:
  662. unlock_page(page);
  663. page_cache_release(page);
  664. out:
  665. return ret;
  666. }
  667. static int ocfs2_zero_extend(struct inode *inode,
  668. u64 zero_to_size)
  669. {
  670. int ret = 0;
  671. u64 start_off;
  672. struct super_block *sb = inode->i_sb;
  673. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  674. while (start_off < zero_to_size) {
  675. ret = ocfs2_write_zero_page(inode, start_off);
  676. if (ret < 0) {
  677. mlog_errno(ret);
  678. goto out;
  679. }
  680. start_off += sb->s_blocksize;
  681. /*
  682. * Very large extends have the potential to lock up
  683. * the cpu for extended periods of time.
  684. */
  685. cond_resched();
  686. }
  687. out:
  688. return ret;
  689. }
  690. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  691. {
  692. int ret;
  693. u32 clusters_to_add;
  694. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  695. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  696. if (clusters_to_add < oi->ip_clusters)
  697. clusters_to_add = 0;
  698. else
  699. clusters_to_add -= oi->ip_clusters;
  700. if (clusters_to_add) {
  701. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  702. clusters_to_add, 0);
  703. if (ret) {
  704. mlog_errno(ret);
  705. goto out;
  706. }
  707. }
  708. /*
  709. * Call this even if we don't add any clusters to the tree. We
  710. * still need to zero the area between the old i_size and the
  711. * new i_size.
  712. */
  713. ret = ocfs2_zero_extend(inode, zero_to);
  714. if (ret < 0)
  715. mlog_errno(ret);
  716. out:
  717. return ret;
  718. }
  719. static int ocfs2_extend_file(struct inode *inode,
  720. struct buffer_head *di_bh,
  721. u64 new_i_size)
  722. {
  723. int ret = 0;
  724. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  725. BUG_ON(!di_bh);
  726. /* setattr sometimes calls us like this. */
  727. if (new_i_size == 0)
  728. goto out;
  729. if (i_size_read(inode) == new_i_size)
  730. goto out;
  731. BUG_ON(new_i_size < i_size_read(inode));
  732. /*
  733. * Fall through for converting inline data, even if the fs
  734. * supports sparse files.
  735. *
  736. * The check for inline data here is legal - nobody can add
  737. * the feature since we have i_mutex. We must check it again
  738. * after acquiring ip_alloc_sem though, as paths like mmap
  739. * might have raced us to converting the inode to extents.
  740. */
  741. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  742. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  743. goto out_update_size;
  744. /*
  745. * The alloc sem blocks people in read/write from reading our
  746. * allocation until we're done changing it. We depend on
  747. * i_mutex to block other extend/truncate calls while we're
  748. * here.
  749. */
  750. down_write(&oi->ip_alloc_sem);
  751. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  752. /*
  753. * We can optimize small extends by keeping the inodes
  754. * inline data.
  755. */
  756. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  757. up_write(&oi->ip_alloc_sem);
  758. goto out_update_size;
  759. }
  760. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  761. if (ret) {
  762. up_write(&oi->ip_alloc_sem);
  763. mlog_errno(ret);
  764. goto out;
  765. }
  766. }
  767. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  768. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  769. up_write(&oi->ip_alloc_sem);
  770. if (ret < 0) {
  771. mlog_errno(ret);
  772. goto out;
  773. }
  774. out_update_size:
  775. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  776. if (ret < 0)
  777. mlog_errno(ret);
  778. out:
  779. return ret;
  780. }
  781. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  782. {
  783. int status = 0, size_change;
  784. struct inode *inode = dentry->d_inode;
  785. struct super_block *sb = inode->i_sb;
  786. struct ocfs2_super *osb = OCFS2_SB(sb);
  787. struct buffer_head *bh = NULL;
  788. handle_t *handle = NULL;
  789. int qtype;
  790. struct dquot *transfer_from[MAXQUOTAS] = { };
  791. struct dquot *transfer_to[MAXQUOTAS] = { };
  792. mlog_entry("(0x%p, '%.*s')\n", dentry,
  793. dentry->d_name.len, dentry->d_name.name);
  794. /* ensuring we don't even attempt to truncate a symlink */
  795. if (S_ISLNK(inode->i_mode))
  796. attr->ia_valid &= ~ATTR_SIZE;
  797. if (attr->ia_valid & ATTR_MODE)
  798. mlog(0, "mode change: %d\n", attr->ia_mode);
  799. if (attr->ia_valid & ATTR_UID)
  800. mlog(0, "uid change: %d\n", attr->ia_uid);
  801. if (attr->ia_valid & ATTR_GID)
  802. mlog(0, "gid change: %d\n", attr->ia_gid);
  803. if (attr->ia_valid & ATTR_SIZE)
  804. mlog(0, "size change...\n");
  805. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  806. mlog(0, "time change...\n");
  807. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  808. | ATTR_GID | ATTR_UID | ATTR_MODE)
  809. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  810. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  811. return 0;
  812. }
  813. status = inode_change_ok(inode, attr);
  814. if (status)
  815. return status;
  816. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  817. if (size_change) {
  818. dquot_initialize(inode);
  819. status = ocfs2_rw_lock(inode, 1);
  820. if (status < 0) {
  821. mlog_errno(status);
  822. goto bail;
  823. }
  824. }
  825. status = ocfs2_inode_lock(inode, &bh, 1);
  826. if (status < 0) {
  827. if (status != -ENOENT)
  828. mlog_errno(status);
  829. goto bail_unlock_rw;
  830. }
  831. if (size_change && attr->ia_size != i_size_read(inode)) {
  832. status = inode_newsize_ok(inode, attr->ia_size);
  833. if (status)
  834. goto bail_unlock;
  835. if (i_size_read(inode) > attr->ia_size) {
  836. if (ocfs2_should_order_data(inode)) {
  837. status = ocfs2_begin_ordered_truncate(inode,
  838. attr->ia_size);
  839. if (status)
  840. goto bail_unlock;
  841. }
  842. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  843. } else
  844. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  845. if (status < 0) {
  846. if (status != -ENOSPC)
  847. mlog_errno(status);
  848. status = -ENOSPC;
  849. goto bail_unlock;
  850. }
  851. }
  852. if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  853. (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  854. /*
  855. * Gather pointers to quota structures so that allocation /
  856. * freeing of quota structures happens here and not inside
  857. * dquot_transfer() where we have problems with lock ordering
  858. */
  859. if (attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid
  860. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  861. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  862. transfer_to[USRQUOTA] = dqget(sb, attr->ia_uid,
  863. USRQUOTA);
  864. transfer_from[USRQUOTA] = dqget(sb, inode->i_uid,
  865. USRQUOTA);
  866. if (!transfer_to[USRQUOTA] || !transfer_from[USRQUOTA]) {
  867. status = -ESRCH;
  868. goto bail_unlock;
  869. }
  870. }
  871. if (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid
  872. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  873. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  874. transfer_to[GRPQUOTA] = dqget(sb, attr->ia_gid,
  875. GRPQUOTA);
  876. transfer_from[GRPQUOTA] = dqget(sb, inode->i_gid,
  877. GRPQUOTA);
  878. if (!transfer_to[GRPQUOTA] || !transfer_from[GRPQUOTA]) {
  879. status = -ESRCH;
  880. goto bail_unlock;
  881. }
  882. }
  883. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  884. 2 * ocfs2_quota_trans_credits(sb));
  885. if (IS_ERR(handle)) {
  886. status = PTR_ERR(handle);
  887. mlog_errno(status);
  888. goto bail_unlock;
  889. }
  890. status = dquot_transfer(inode, attr);
  891. if (status < 0)
  892. goto bail_commit;
  893. } else {
  894. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  895. if (IS_ERR(handle)) {
  896. status = PTR_ERR(handle);
  897. mlog_errno(status);
  898. goto bail_unlock;
  899. }
  900. }
  901. /*
  902. * This will intentionally not wind up calling vmtruncate(),
  903. * since all the work for a size change has been done above.
  904. * Otherwise, we could get into problems with truncate as
  905. * ip_alloc_sem is used there to protect against i_size
  906. * changes.
  907. */
  908. status = inode_setattr(inode, attr);
  909. if (status < 0) {
  910. mlog_errno(status);
  911. goto bail_commit;
  912. }
  913. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  914. if (status < 0)
  915. mlog_errno(status);
  916. bail_commit:
  917. ocfs2_commit_trans(osb, handle);
  918. bail_unlock:
  919. ocfs2_inode_unlock(inode, 1);
  920. bail_unlock_rw:
  921. if (size_change)
  922. ocfs2_rw_unlock(inode, 1);
  923. bail:
  924. brelse(bh);
  925. /* Release quota pointers in case we acquired them */
  926. for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
  927. dqput(transfer_to[qtype]);
  928. dqput(transfer_from[qtype]);
  929. }
  930. if (!status && attr->ia_valid & ATTR_MODE) {
  931. status = ocfs2_acl_chmod(inode);
  932. if (status < 0)
  933. mlog_errno(status);
  934. }
  935. mlog_exit(status);
  936. return status;
  937. }
  938. int ocfs2_getattr(struct vfsmount *mnt,
  939. struct dentry *dentry,
  940. struct kstat *stat)
  941. {
  942. struct inode *inode = dentry->d_inode;
  943. struct super_block *sb = dentry->d_inode->i_sb;
  944. struct ocfs2_super *osb = sb->s_fs_info;
  945. int err;
  946. mlog_entry_void();
  947. err = ocfs2_inode_revalidate(dentry);
  948. if (err) {
  949. if (err != -ENOENT)
  950. mlog_errno(err);
  951. goto bail;
  952. }
  953. generic_fillattr(inode, stat);
  954. /* We set the blksize from the cluster size for performance */
  955. stat->blksize = osb->s_clustersize;
  956. bail:
  957. mlog_exit(err);
  958. return err;
  959. }
  960. int ocfs2_permission(struct inode *inode, int mask)
  961. {
  962. int ret;
  963. mlog_entry_void();
  964. ret = ocfs2_inode_lock(inode, NULL, 0);
  965. if (ret) {
  966. if (ret != -ENOENT)
  967. mlog_errno(ret);
  968. goto out;
  969. }
  970. ret = generic_permission(inode, mask, ocfs2_check_acl);
  971. ocfs2_inode_unlock(inode, 0);
  972. out:
  973. mlog_exit(ret);
  974. return ret;
  975. }
  976. static int __ocfs2_write_remove_suid(struct inode *inode,
  977. struct buffer_head *bh)
  978. {
  979. int ret;
  980. handle_t *handle;
  981. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  982. struct ocfs2_dinode *di;
  983. mlog_entry("(Inode %llu, mode 0%o)\n",
  984. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  985. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  986. if (IS_ERR(handle)) {
  987. ret = PTR_ERR(handle);
  988. mlog_errno(ret);
  989. goto out;
  990. }
  991. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  992. OCFS2_JOURNAL_ACCESS_WRITE);
  993. if (ret < 0) {
  994. mlog_errno(ret);
  995. goto out_trans;
  996. }
  997. inode->i_mode &= ~S_ISUID;
  998. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  999. inode->i_mode &= ~S_ISGID;
  1000. di = (struct ocfs2_dinode *) bh->b_data;
  1001. di->i_mode = cpu_to_le16(inode->i_mode);
  1002. ocfs2_journal_dirty(handle, bh);
  1003. out_trans:
  1004. ocfs2_commit_trans(osb, handle);
  1005. out:
  1006. mlog_exit(ret);
  1007. return ret;
  1008. }
  1009. /*
  1010. * Will look for holes and unwritten extents in the range starting at
  1011. * pos for count bytes (inclusive).
  1012. */
  1013. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1014. size_t count)
  1015. {
  1016. int ret = 0;
  1017. unsigned int extent_flags;
  1018. u32 cpos, clusters, extent_len, phys_cpos;
  1019. struct super_block *sb = inode->i_sb;
  1020. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1021. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1022. while (clusters) {
  1023. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1024. &extent_flags);
  1025. if (ret < 0) {
  1026. mlog_errno(ret);
  1027. goto out;
  1028. }
  1029. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1030. ret = 1;
  1031. break;
  1032. }
  1033. if (extent_len > clusters)
  1034. extent_len = clusters;
  1035. clusters -= extent_len;
  1036. cpos += extent_len;
  1037. }
  1038. out:
  1039. return ret;
  1040. }
  1041. static int ocfs2_write_remove_suid(struct inode *inode)
  1042. {
  1043. int ret;
  1044. struct buffer_head *bh = NULL;
  1045. ret = ocfs2_read_inode_block(inode, &bh);
  1046. if (ret < 0) {
  1047. mlog_errno(ret);
  1048. goto out;
  1049. }
  1050. ret = __ocfs2_write_remove_suid(inode, bh);
  1051. out:
  1052. brelse(bh);
  1053. return ret;
  1054. }
  1055. /*
  1056. * Allocate enough extents to cover the region starting at byte offset
  1057. * start for len bytes. Existing extents are skipped, any extents
  1058. * added are marked as "unwritten".
  1059. */
  1060. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1061. u64 start, u64 len)
  1062. {
  1063. int ret;
  1064. u32 cpos, phys_cpos, clusters, alloc_size;
  1065. u64 end = start + len;
  1066. struct buffer_head *di_bh = NULL;
  1067. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1068. ret = ocfs2_read_inode_block(inode, &di_bh);
  1069. if (ret) {
  1070. mlog_errno(ret);
  1071. goto out;
  1072. }
  1073. /*
  1074. * Nothing to do if the requested reservation range
  1075. * fits within the inode.
  1076. */
  1077. if (ocfs2_size_fits_inline_data(di_bh, end))
  1078. goto out;
  1079. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1080. if (ret) {
  1081. mlog_errno(ret);
  1082. goto out;
  1083. }
  1084. }
  1085. /*
  1086. * We consider both start and len to be inclusive.
  1087. */
  1088. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1089. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1090. clusters -= cpos;
  1091. while (clusters) {
  1092. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1093. &alloc_size, NULL);
  1094. if (ret) {
  1095. mlog_errno(ret);
  1096. goto out;
  1097. }
  1098. /*
  1099. * Hole or existing extent len can be arbitrary, so
  1100. * cap it to our own allocation request.
  1101. */
  1102. if (alloc_size > clusters)
  1103. alloc_size = clusters;
  1104. if (phys_cpos) {
  1105. /*
  1106. * We already have an allocation at this
  1107. * region so we can safely skip it.
  1108. */
  1109. goto next;
  1110. }
  1111. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1112. if (ret) {
  1113. if (ret != -ENOSPC)
  1114. mlog_errno(ret);
  1115. goto out;
  1116. }
  1117. next:
  1118. cpos += alloc_size;
  1119. clusters -= alloc_size;
  1120. }
  1121. ret = 0;
  1122. out:
  1123. brelse(di_bh);
  1124. return ret;
  1125. }
  1126. /*
  1127. * Truncate a byte range, avoiding pages within partial clusters. This
  1128. * preserves those pages for the zeroing code to write to.
  1129. */
  1130. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1131. u64 byte_len)
  1132. {
  1133. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1134. loff_t start, end;
  1135. struct address_space *mapping = inode->i_mapping;
  1136. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1137. end = byte_start + byte_len;
  1138. end = end & ~(osb->s_clustersize - 1);
  1139. if (start < end) {
  1140. unmap_mapping_range(mapping, start, end - start, 0);
  1141. truncate_inode_pages_range(mapping, start, end - 1);
  1142. }
  1143. }
  1144. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1145. u64 start, u64 len)
  1146. {
  1147. int ret = 0;
  1148. u64 tmpend, end = start + len;
  1149. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1150. unsigned int csize = osb->s_clustersize;
  1151. handle_t *handle;
  1152. /*
  1153. * The "start" and "end" values are NOT necessarily part of
  1154. * the range whose allocation is being deleted. Rather, this
  1155. * is what the user passed in with the request. We must zero
  1156. * partial clusters here. There's no need to worry about
  1157. * physical allocation - the zeroing code knows to skip holes.
  1158. */
  1159. mlog(0, "byte start: %llu, end: %llu\n",
  1160. (unsigned long long)start, (unsigned long long)end);
  1161. /*
  1162. * If both edges are on a cluster boundary then there's no
  1163. * zeroing required as the region is part of the allocation to
  1164. * be truncated.
  1165. */
  1166. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1167. goto out;
  1168. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1169. if (IS_ERR(handle)) {
  1170. ret = PTR_ERR(handle);
  1171. mlog_errno(ret);
  1172. goto out;
  1173. }
  1174. /*
  1175. * We want to get the byte offset of the end of the 1st cluster.
  1176. */
  1177. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1178. if (tmpend > end)
  1179. tmpend = end;
  1180. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1181. (unsigned long long)start, (unsigned long long)tmpend);
  1182. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1183. if (ret)
  1184. mlog_errno(ret);
  1185. if (tmpend < end) {
  1186. /*
  1187. * This may make start and end equal, but the zeroing
  1188. * code will skip any work in that case so there's no
  1189. * need to catch it up here.
  1190. */
  1191. start = end & ~(osb->s_clustersize - 1);
  1192. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1193. (unsigned long long)start, (unsigned long long)end);
  1194. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1195. if (ret)
  1196. mlog_errno(ret);
  1197. }
  1198. ocfs2_commit_trans(osb, handle);
  1199. out:
  1200. return ret;
  1201. }
  1202. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1203. {
  1204. int i;
  1205. struct ocfs2_extent_rec *rec = NULL;
  1206. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1207. rec = &el->l_recs[i];
  1208. if (le32_to_cpu(rec->e_cpos) < pos)
  1209. break;
  1210. }
  1211. return i;
  1212. }
  1213. /*
  1214. * Helper to calculate the punching pos and length in one run, we handle the
  1215. * following three cases in order:
  1216. *
  1217. * - remove the entire record
  1218. * - remove a partial record
  1219. * - no record needs to be removed (hole-punching completed)
  1220. */
  1221. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1222. struct ocfs2_extent_list *el,
  1223. struct ocfs2_extent_rec *rec,
  1224. u32 trunc_start, u32 *trunc_cpos,
  1225. u32 *trunc_len, u32 *trunc_end,
  1226. u64 *blkno, int *done)
  1227. {
  1228. int ret = 0;
  1229. u32 coff, range;
  1230. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1231. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1232. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1233. /*
  1234. * Skip holes if any.
  1235. */
  1236. if (range < *trunc_end)
  1237. *trunc_end = range;
  1238. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1239. *blkno = le64_to_cpu(rec->e_blkno);
  1240. *trunc_end = le32_to_cpu(rec->e_cpos);
  1241. } else if (range > trunc_start) {
  1242. *trunc_cpos = trunc_start;
  1243. *trunc_len = *trunc_end - trunc_start;
  1244. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1245. *blkno = le64_to_cpu(rec->e_blkno) +
  1246. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1247. *trunc_end = trunc_start;
  1248. } else {
  1249. /*
  1250. * It may have two following possibilities:
  1251. *
  1252. * - last record has been removed
  1253. * - trunc_start was within a hole
  1254. *
  1255. * both two cases mean the completion of hole punching.
  1256. */
  1257. ret = 1;
  1258. }
  1259. *done = ret;
  1260. }
  1261. static int ocfs2_remove_inode_range(struct inode *inode,
  1262. struct buffer_head *di_bh, u64 byte_start,
  1263. u64 byte_len)
  1264. {
  1265. int ret = 0, flags = 0, done = 0, i;
  1266. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1267. u32 cluster_in_el;
  1268. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1269. struct ocfs2_cached_dealloc_ctxt dealloc;
  1270. struct address_space *mapping = inode->i_mapping;
  1271. struct ocfs2_extent_tree et;
  1272. struct ocfs2_path *path = NULL;
  1273. struct ocfs2_extent_list *el = NULL;
  1274. struct ocfs2_extent_rec *rec = NULL;
  1275. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1276. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1277. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1278. ocfs2_init_dealloc_ctxt(&dealloc);
  1279. if (byte_len == 0)
  1280. return 0;
  1281. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1282. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1283. byte_start + byte_len, 0);
  1284. if (ret) {
  1285. mlog_errno(ret);
  1286. goto out;
  1287. }
  1288. /*
  1289. * There's no need to get fancy with the page cache
  1290. * truncate of an inline-data inode. We're talking
  1291. * about less than a page here, which will be cached
  1292. * in the dinode buffer anyway.
  1293. */
  1294. unmap_mapping_range(mapping, 0, 0, 0);
  1295. truncate_inode_pages(mapping, 0);
  1296. goto out;
  1297. }
  1298. /*
  1299. * For reflinks, we may need to CoW 2 clusters which might be
  1300. * partially zero'd later, if hole's start and end offset were
  1301. * within one cluster(means is not exactly aligned to clustersize).
  1302. */
  1303. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  1304. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1305. if (ret) {
  1306. mlog_errno(ret);
  1307. goto out;
  1308. }
  1309. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1310. if (ret) {
  1311. mlog_errno(ret);
  1312. goto out;
  1313. }
  1314. }
  1315. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1316. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1317. cluster_in_el = trunc_end;
  1318. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, cend: %u\n",
  1319. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1320. (unsigned long long)byte_start,
  1321. (unsigned long long)byte_len, trunc_start, trunc_end);
  1322. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1323. if (ret) {
  1324. mlog_errno(ret);
  1325. goto out;
  1326. }
  1327. path = ocfs2_new_path_from_et(&et);
  1328. if (!path) {
  1329. ret = -ENOMEM;
  1330. mlog_errno(ret);
  1331. goto out;
  1332. }
  1333. while (trunc_end > trunc_start) {
  1334. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1335. cluster_in_el);
  1336. if (ret) {
  1337. mlog_errno(ret);
  1338. goto out;
  1339. }
  1340. el = path_leaf_el(path);
  1341. i = ocfs2_find_rec(el, trunc_end);
  1342. /*
  1343. * Need to go to previous extent block.
  1344. */
  1345. if (i < 0) {
  1346. if (path->p_tree_depth == 0)
  1347. break;
  1348. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1349. path,
  1350. &cluster_in_el);
  1351. if (ret) {
  1352. mlog_errno(ret);
  1353. goto out;
  1354. }
  1355. /*
  1356. * We've reached the leftmost extent block,
  1357. * it's safe to leave.
  1358. */
  1359. if (cluster_in_el == 0)
  1360. break;
  1361. /*
  1362. * The 'pos' searched for previous extent block is
  1363. * always one cluster less than actual trunc_end.
  1364. */
  1365. trunc_end = cluster_in_el + 1;
  1366. ocfs2_reinit_path(path, 1);
  1367. continue;
  1368. } else
  1369. rec = &el->l_recs[i];
  1370. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1371. &trunc_len, &trunc_end, &blkno, &done);
  1372. if (done)
  1373. break;
  1374. flags = rec->e_flags;
  1375. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1376. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1377. phys_cpos, trunc_len, flags,
  1378. &dealloc, refcount_loc);
  1379. if (ret < 0) {
  1380. mlog_errno(ret);
  1381. goto out;
  1382. }
  1383. cluster_in_el = trunc_end;
  1384. ocfs2_reinit_path(path, 1);
  1385. }
  1386. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1387. out:
  1388. ocfs2_schedule_truncate_log_flush(osb, 1);
  1389. ocfs2_run_deallocs(osb, &dealloc);
  1390. return ret;
  1391. }
  1392. /*
  1393. * Parts of this function taken from xfs_change_file_space()
  1394. */
  1395. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1396. loff_t f_pos, unsigned int cmd,
  1397. struct ocfs2_space_resv *sr,
  1398. int change_size)
  1399. {
  1400. int ret;
  1401. s64 llen;
  1402. loff_t size;
  1403. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1404. struct buffer_head *di_bh = NULL;
  1405. handle_t *handle;
  1406. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1407. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1408. return -EROFS;
  1409. mutex_lock(&inode->i_mutex);
  1410. /*
  1411. * This prevents concurrent writes on other nodes
  1412. */
  1413. ret = ocfs2_rw_lock(inode, 1);
  1414. if (ret) {
  1415. mlog_errno(ret);
  1416. goto out;
  1417. }
  1418. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1419. if (ret) {
  1420. mlog_errno(ret);
  1421. goto out_rw_unlock;
  1422. }
  1423. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1424. ret = -EPERM;
  1425. goto out_inode_unlock;
  1426. }
  1427. switch (sr->l_whence) {
  1428. case 0: /*SEEK_SET*/
  1429. break;
  1430. case 1: /*SEEK_CUR*/
  1431. sr->l_start += f_pos;
  1432. break;
  1433. case 2: /*SEEK_END*/
  1434. sr->l_start += i_size_read(inode);
  1435. break;
  1436. default:
  1437. ret = -EINVAL;
  1438. goto out_inode_unlock;
  1439. }
  1440. sr->l_whence = 0;
  1441. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1442. if (sr->l_start < 0
  1443. || sr->l_start > max_off
  1444. || (sr->l_start + llen) < 0
  1445. || (sr->l_start + llen) > max_off) {
  1446. ret = -EINVAL;
  1447. goto out_inode_unlock;
  1448. }
  1449. size = sr->l_start + sr->l_len;
  1450. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1451. if (sr->l_len <= 0) {
  1452. ret = -EINVAL;
  1453. goto out_inode_unlock;
  1454. }
  1455. }
  1456. if (file && should_remove_suid(file->f_path.dentry)) {
  1457. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1458. if (ret) {
  1459. mlog_errno(ret);
  1460. goto out_inode_unlock;
  1461. }
  1462. }
  1463. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1464. switch (cmd) {
  1465. case OCFS2_IOC_RESVSP:
  1466. case OCFS2_IOC_RESVSP64:
  1467. /*
  1468. * This takes unsigned offsets, but the signed ones we
  1469. * pass have been checked against overflow above.
  1470. */
  1471. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1472. sr->l_len);
  1473. break;
  1474. case OCFS2_IOC_UNRESVSP:
  1475. case OCFS2_IOC_UNRESVSP64:
  1476. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1477. sr->l_len);
  1478. break;
  1479. default:
  1480. ret = -EINVAL;
  1481. }
  1482. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1483. if (ret) {
  1484. mlog_errno(ret);
  1485. goto out_inode_unlock;
  1486. }
  1487. /*
  1488. * We update c/mtime for these changes
  1489. */
  1490. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1491. if (IS_ERR(handle)) {
  1492. ret = PTR_ERR(handle);
  1493. mlog_errno(ret);
  1494. goto out_inode_unlock;
  1495. }
  1496. if (change_size && i_size_read(inode) < size)
  1497. i_size_write(inode, size);
  1498. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1499. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1500. if (ret < 0)
  1501. mlog_errno(ret);
  1502. ocfs2_commit_trans(osb, handle);
  1503. out_inode_unlock:
  1504. brelse(di_bh);
  1505. ocfs2_inode_unlock(inode, 1);
  1506. out_rw_unlock:
  1507. ocfs2_rw_unlock(inode, 1);
  1508. out:
  1509. mutex_unlock(&inode->i_mutex);
  1510. return ret;
  1511. }
  1512. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1513. struct ocfs2_space_resv *sr)
  1514. {
  1515. struct inode *inode = file->f_path.dentry->d_inode;
  1516. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1517. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1518. !ocfs2_writes_unwritten_extents(osb))
  1519. return -ENOTTY;
  1520. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1521. !ocfs2_sparse_alloc(osb))
  1522. return -ENOTTY;
  1523. if (!S_ISREG(inode->i_mode))
  1524. return -EINVAL;
  1525. if (!(file->f_mode & FMODE_WRITE))
  1526. return -EBADF;
  1527. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1528. }
  1529. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1530. loff_t len)
  1531. {
  1532. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1533. struct ocfs2_space_resv sr;
  1534. int change_size = 1;
  1535. if (!ocfs2_writes_unwritten_extents(osb))
  1536. return -EOPNOTSUPP;
  1537. if (S_ISDIR(inode->i_mode))
  1538. return -ENODEV;
  1539. if (mode & FALLOC_FL_KEEP_SIZE)
  1540. change_size = 0;
  1541. sr.l_whence = 0;
  1542. sr.l_start = (s64)offset;
  1543. sr.l_len = (s64)len;
  1544. return __ocfs2_change_file_space(NULL, inode, offset,
  1545. OCFS2_IOC_RESVSP64, &sr, change_size);
  1546. }
  1547. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1548. size_t count)
  1549. {
  1550. int ret = 0;
  1551. unsigned int extent_flags;
  1552. u32 cpos, clusters, extent_len, phys_cpos;
  1553. struct super_block *sb = inode->i_sb;
  1554. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1555. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1556. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1557. return 0;
  1558. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1559. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1560. while (clusters) {
  1561. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1562. &extent_flags);
  1563. if (ret < 0) {
  1564. mlog_errno(ret);
  1565. goto out;
  1566. }
  1567. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1568. ret = 1;
  1569. break;
  1570. }
  1571. if (extent_len > clusters)
  1572. extent_len = clusters;
  1573. clusters -= extent_len;
  1574. cpos += extent_len;
  1575. }
  1576. out:
  1577. return ret;
  1578. }
  1579. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1580. loff_t pos, size_t count,
  1581. int *meta_level)
  1582. {
  1583. int ret;
  1584. struct buffer_head *di_bh = NULL;
  1585. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1586. u32 clusters =
  1587. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1588. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1589. if (ret) {
  1590. mlog_errno(ret);
  1591. goto out;
  1592. }
  1593. *meta_level = 1;
  1594. ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
  1595. if (ret)
  1596. mlog_errno(ret);
  1597. out:
  1598. brelse(di_bh);
  1599. return ret;
  1600. }
  1601. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1602. loff_t *ppos,
  1603. size_t count,
  1604. int appending,
  1605. int *direct_io,
  1606. int *has_refcount)
  1607. {
  1608. int ret = 0, meta_level = 0;
  1609. struct inode *inode = dentry->d_inode;
  1610. loff_t saved_pos, end;
  1611. /*
  1612. * We start with a read level meta lock and only jump to an ex
  1613. * if we need to make modifications here.
  1614. */
  1615. for(;;) {
  1616. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1617. if (ret < 0) {
  1618. meta_level = -1;
  1619. mlog_errno(ret);
  1620. goto out;
  1621. }
  1622. /* Clear suid / sgid if necessary. We do this here
  1623. * instead of later in the write path because
  1624. * remove_suid() calls ->setattr without any hint that
  1625. * we may have already done our cluster locking. Since
  1626. * ocfs2_setattr() *must* take cluster locks to
  1627. * proceeed, this will lead us to recursively lock the
  1628. * inode. There's also the dinode i_size state which
  1629. * can be lost via setattr during extending writes (we
  1630. * set inode->i_size at the end of a write. */
  1631. if (should_remove_suid(dentry)) {
  1632. if (meta_level == 0) {
  1633. ocfs2_inode_unlock(inode, meta_level);
  1634. meta_level = 1;
  1635. continue;
  1636. }
  1637. ret = ocfs2_write_remove_suid(inode);
  1638. if (ret < 0) {
  1639. mlog_errno(ret);
  1640. goto out_unlock;
  1641. }
  1642. }
  1643. /* work on a copy of ppos until we're sure that we won't have
  1644. * to recalculate it due to relocking. */
  1645. if (appending) {
  1646. saved_pos = i_size_read(inode);
  1647. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1648. } else {
  1649. saved_pos = *ppos;
  1650. }
  1651. end = saved_pos + count;
  1652. ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
  1653. if (ret == 1) {
  1654. ocfs2_inode_unlock(inode, meta_level);
  1655. meta_level = -1;
  1656. ret = ocfs2_prepare_inode_for_refcount(inode,
  1657. saved_pos,
  1658. count,
  1659. &meta_level);
  1660. if (has_refcount)
  1661. *has_refcount = 1;
  1662. if (direct_io)
  1663. *direct_io = 0;
  1664. }
  1665. if (ret < 0) {
  1666. mlog_errno(ret);
  1667. goto out_unlock;
  1668. }
  1669. /*
  1670. * Skip the O_DIRECT checks if we don't need
  1671. * them.
  1672. */
  1673. if (!direct_io || !(*direct_io))
  1674. break;
  1675. /*
  1676. * There's no sane way to do direct writes to an inode
  1677. * with inline data.
  1678. */
  1679. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1680. *direct_io = 0;
  1681. break;
  1682. }
  1683. /*
  1684. * Allowing concurrent direct writes means
  1685. * i_size changes wouldn't be synchronized, so
  1686. * one node could wind up truncating another
  1687. * nodes writes.
  1688. */
  1689. if (end > i_size_read(inode)) {
  1690. *direct_io = 0;
  1691. break;
  1692. }
  1693. /*
  1694. * We don't fill holes during direct io, so
  1695. * check for them here. If any are found, the
  1696. * caller will have to retake some cluster
  1697. * locks and initiate the io as buffered.
  1698. */
  1699. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1700. if (ret == 1) {
  1701. *direct_io = 0;
  1702. ret = 0;
  1703. } else if (ret < 0)
  1704. mlog_errno(ret);
  1705. break;
  1706. }
  1707. if (appending)
  1708. *ppos = saved_pos;
  1709. out_unlock:
  1710. if (meta_level >= 0)
  1711. ocfs2_inode_unlock(inode, meta_level);
  1712. out:
  1713. return ret;
  1714. }
  1715. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1716. const struct iovec *iov,
  1717. unsigned long nr_segs,
  1718. loff_t pos)
  1719. {
  1720. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1721. int can_do_direct, has_refcount = 0;
  1722. ssize_t written = 0;
  1723. size_t ocount; /* original count */
  1724. size_t count; /* after file limit checks */
  1725. loff_t old_size, *ppos = &iocb->ki_pos;
  1726. u32 old_clusters;
  1727. struct file *file = iocb->ki_filp;
  1728. struct inode *inode = file->f_path.dentry->d_inode;
  1729. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1730. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1731. (unsigned int)nr_segs,
  1732. file->f_path.dentry->d_name.len,
  1733. file->f_path.dentry->d_name.name);
  1734. if (iocb->ki_left == 0)
  1735. return 0;
  1736. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1737. appending = file->f_flags & O_APPEND ? 1 : 0;
  1738. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1739. mutex_lock(&inode->i_mutex);
  1740. relock:
  1741. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1742. if (direct_io) {
  1743. down_read(&inode->i_alloc_sem);
  1744. have_alloc_sem = 1;
  1745. }
  1746. /* concurrent O_DIRECT writes are allowed */
  1747. rw_level = !direct_io;
  1748. ret = ocfs2_rw_lock(inode, rw_level);
  1749. if (ret < 0) {
  1750. mlog_errno(ret);
  1751. goto out_sems;
  1752. }
  1753. can_do_direct = direct_io;
  1754. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1755. iocb->ki_left, appending,
  1756. &can_do_direct, &has_refcount);
  1757. if (ret < 0) {
  1758. mlog_errno(ret);
  1759. goto out;
  1760. }
  1761. /*
  1762. * We can't complete the direct I/O as requested, fall back to
  1763. * buffered I/O.
  1764. */
  1765. if (direct_io && !can_do_direct) {
  1766. ocfs2_rw_unlock(inode, rw_level);
  1767. up_read(&inode->i_alloc_sem);
  1768. have_alloc_sem = 0;
  1769. rw_level = -1;
  1770. direct_io = 0;
  1771. goto relock;
  1772. }
  1773. /*
  1774. * To later detect whether a journal commit for sync writes is
  1775. * necessary, we sample i_size, and cluster count here.
  1776. */
  1777. old_size = i_size_read(inode);
  1778. old_clusters = OCFS2_I(inode)->ip_clusters;
  1779. /* communicate with ocfs2_dio_end_io */
  1780. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1781. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1782. VERIFY_READ);
  1783. if (ret)
  1784. goto out_dio;
  1785. count = ocount;
  1786. ret = generic_write_checks(file, ppos, &count,
  1787. S_ISBLK(inode->i_mode));
  1788. if (ret)
  1789. goto out_dio;
  1790. if (direct_io) {
  1791. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1792. ppos, count, ocount);
  1793. if (written < 0) {
  1794. /*
  1795. * direct write may have instantiated a few
  1796. * blocks outside i_size. Trim these off again.
  1797. * Don't need i_size_read because we hold i_mutex.
  1798. */
  1799. if (*ppos + count > inode->i_size)
  1800. vmtruncate(inode, inode->i_size);
  1801. ret = written;
  1802. goto out_dio;
  1803. }
  1804. } else {
  1805. current->backing_dev_info = file->f_mapping->backing_dev_info;
  1806. written = generic_file_buffered_write(iocb, iov, nr_segs, *ppos,
  1807. ppos, count, 0);
  1808. current->backing_dev_info = NULL;
  1809. }
  1810. out_dio:
  1811. /* buffered aio wouldn't have proper lock coverage today */
  1812. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1813. if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
  1814. ((file->f_flags & O_DIRECT) && has_refcount)) {
  1815. ret = filemap_fdatawrite_range(file->f_mapping, pos,
  1816. pos + count - 1);
  1817. if (ret < 0)
  1818. written = ret;
  1819. if (!ret && ((old_size != i_size_read(inode)) ||
  1820. (old_clusters != OCFS2_I(inode)->ip_clusters) ||
  1821. has_refcount)) {
  1822. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  1823. if (ret < 0)
  1824. written = ret;
  1825. }
  1826. if (!ret)
  1827. ret = filemap_fdatawait_range(file->f_mapping, pos,
  1828. pos + count - 1);
  1829. }
  1830. /*
  1831. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1832. * function pointer which is called when o_direct io completes so that
  1833. * it can unlock our rw lock. (it's the clustered equivalent of
  1834. * i_alloc_sem; protects truncate from racing with pending ios).
  1835. * Unfortunately there are error cases which call end_io and others
  1836. * that don't. so we don't have to unlock the rw_lock if either an
  1837. * async dio is going to do it in the future or an end_io after an
  1838. * error has already done it.
  1839. */
  1840. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  1841. rw_level = -1;
  1842. have_alloc_sem = 0;
  1843. }
  1844. out:
  1845. if (rw_level != -1)
  1846. ocfs2_rw_unlock(inode, rw_level);
  1847. out_sems:
  1848. if (have_alloc_sem)
  1849. up_read(&inode->i_alloc_sem);
  1850. mutex_unlock(&inode->i_mutex);
  1851. if (written)
  1852. ret = written;
  1853. mlog_exit(ret);
  1854. return ret;
  1855. }
  1856. static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
  1857. struct file *out,
  1858. struct splice_desc *sd)
  1859. {
  1860. int ret;
  1861. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, &sd->pos,
  1862. sd->total_len, 0, NULL, NULL);
  1863. if (ret < 0) {
  1864. mlog_errno(ret);
  1865. return ret;
  1866. }
  1867. return splice_from_pipe_feed(pipe, sd, pipe_to_file);
  1868. }
  1869. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1870. struct file *out,
  1871. loff_t *ppos,
  1872. size_t len,
  1873. unsigned int flags)
  1874. {
  1875. int ret;
  1876. struct address_space *mapping = out->f_mapping;
  1877. struct inode *inode = mapping->host;
  1878. struct splice_desc sd = {
  1879. .total_len = len,
  1880. .flags = flags,
  1881. .pos = *ppos,
  1882. .u.file = out,
  1883. };
  1884. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1885. (unsigned int)len,
  1886. out->f_path.dentry->d_name.len,
  1887. out->f_path.dentry->d_name.name);
  1888. if (pipe->inode)
  1889. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_PARENT);
  1890. splice_from_pipe_begin(&sd);
  1891. do {
  1892. ret = splice_from_pipe_next(pipe, &sd);
  1893. if (ret <= 0)
  1894. break;
  1895. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1896. ret = ocfs2_rw_lock(inode, 1);
  1897. if (ret < 0)
  1898. mlog_errno(ret);
  1899. else {
  1900. ret = ocfs2_splice_to_file(pipe, out, &sd);
  1901. ocfs2_rw_unlock(inode, 1);
  1902. }
  1903. mutex_unlock(&inode->i_mutex);
  1904. } while (ret > 0);
  1905. splice_from_pipe_end(pipe, &sd);
  1906. if (pipe->inode)
  1907. mutex_unlock(&pipe->inode->i_mutex);
  1908. if (sd.num_spliced)
  1909. ret = sd.num_spliced;
  1910. if (ret > 0) {
  1911. unsigned long nr_pages;
  1912. int err;
  1913. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1914. err = generic_write_sync(out, *ppos, ret);
  1915. if (err)
  1916. ret = err;
  1917. else
  1918. *ppos += ret;
  1919. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  1920. }
  1921. mlog_exit(ret);
  1922. return ret;
  1923. }
  1924. static ssize_t ocfs2_file_splice_read(struct file *in,
  1925. loff_t *ppos,
  1926. struct pipe_inode_info *pipe,
  1927. size_t len,
  1928. unsigned int flags)
  1929. {
  1930. int ret = 0, lock_level = 0;
  1931. struct inode *inode = in->f_path.dentry->d_inode;
  1932. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1933. (unsigned int)len,
  1934. in->f_path.dentry->d_name.len,
  1935. in->f_path.dentry->d_name.name);
  1936. /*
  1937. * See the comment in ocfs2_file_aio_read()
  1938. */
  1939. ret = ocfs2_inode_lock_atime(inode, in->f_vfsmnt, &lock_level);
  1940. if (ret < 0) {
  1941. mlog_errno(ret);
  1942. goto bail;
  1943. }
  1944. ocfs2_inode_unlock(inode, lock_level);
  1945. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1946. bail:
  1947. mlog_exit(ret);
  1948. return ret;
  1949. }
  1950. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1951. const struct iovec *iov,
  1952. unsigned long nr_segs,
  1953. loff_t pos)
  1954. {
  1955. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1956. struct file *filp = iocb->ki_filp;
  1957. struct inode *inode = filp->f_path.dentry->d_inode;
  1958. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1959. (unsigned int)nr_segs,
  1960. filp->f_path.dentry->d_name.len,
  1961. filp->f_path.dentry->d_name.name);
  1962. if (!inode) {
  1963. ret = -EINVAL;
  1964. mlog_errno(ret);
  1965. goto bail;
  1966. }
  1967. /*
  1968. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1969. * need locks to protect pending reads from racing with truncate.
  1970. */
  1971. if (filp->f_flags & O_DIRECT) {
  1972. down_read(&inode->i_alloc_sem);
  1973. have_alloc_sem = 1;
  1974. ret = ocfs2_rw_lock(inode, 0);
  1975. if (ret < 0) {
  1976. mlog_errno(ret);
  1977. goto bail;
  1978. }
  1979. rw_level = 0;
  1980. /* communicate with ocfs2_dio_end_io */
  1981. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1982. }
  1983. /*
  1984. * We're fine letting folks race truncates and extending
  1985. * writes with read across the cluster, just like they can
  1986. * locally. Hence no rw_lock during read.
  1987. *
  1988. * Take and drop the meta data lock to update inode fields
  1989. * like i_size. This allows the checks down below
  1990. * generic_file_aio_read() a chance of actually working.
  1991. */
  1992. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1993. if (ret < 0) {
  1994. mlog_errno(ret);
  1995. goto bail;
  1996. }
  1997. ocfs2_inode_unlock(inode, lock_level);
  1998. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1999. if (ret == -EINVAL)
  2000. mlog(0, "generic_file_aio_read returned -EINVAL\n");
  2001. /* buffered aio wouldn't have proper lock coverage today */
  2002. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  2003. /* see ocfs2_file_aio_write */
  2004. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2005. rw_level = -1;
  2006. have_alloc_sem = 0;
  2007. }
  2008. bail:
  2009. if (have_alloc_sem)
  2010. up_read(&inode->i_alloc_sem);
  2011. if (rw_level != -1)
  2012. ocfs2_rw_unlock(inode, rw_level);
  2013. mlog_exit(ret);
  2014. return ret;
  2015. }
  2016. const struct inode_operations ocfs2_file_iops = {
  2017. .setattr = ocfs2_setattr,
  2018. .getattr = ocfs2_getattr,
  2019. .permission = ocfs2_permission,
  2020. .setxattr = generic_setxattr,
  2021. .getxattr = generic_getxattr,
  2022. .listxattr = ocfs2_listxattr,
  2023. .removexattr = generic_removexattr,
  2024. .fallocate = ocfs2_fallocate,
  2025. .fiemap = ocfs2_fiemap,
  2026. };
  2027. const struct inode_operations ocfs2_special_file_iops = {
  2028. .setattr = ocfs2_setattr,
  2029. .getattr = ocfs2_getattr,
  2030. .permission = ocfs2_permission,
  2031. };
  2032. /*
  2033. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2034. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2035. */
  2036. const struct file_operations ocfs2_fops = {
  2037. .llseek = generic_file_llseek,
  2038. .read = do_sync_read,
  2039. .write = do_sync_write,
  2040. .mmap = ocfs2_mmap,
  2041. .fsync = ocfs2_sync_file,
  2042. .release = ocfs2_file_release,
  2043. .open = ocfs2_file_open,
  2044. .aio_read = ocfs2_file_aio_read,
  2045. .aio_write = ocfs2_file_aio_write,
  2046. .unlocked_ioctl = ocfs2_ioctl,
  2047. #ifdef CONFIG_COMPAT
  2048. .compat_ioctl = ocfs2_compat_ioctl,
  2049. #endif
  2050. .lock = ocfs2_lock,
  2051. .flock = ocfs2_flock,
  2052. .splice_read = ocfs2_file_splice_read,
  2053. .splice_write = ocfs2_file_splice_write,
  2054. };
  2055. const struct file_operations ocfs2_dops = {
  2056. .llseek = generic_file_llseek,
  2057. .read = generic_read_dir,
  2058. .readdir = ocfs2_readdir,
  2059. .fsync = ocfs2_sync_file,
  2060. .release = ocfs2_dir_release,
  2061. .open = ocfs2_dir_open,
  2062. .unlocked_ioctl = ocfs2_ioctl,
  2063. #ifdef CONFIG_COMPAT
  2064. .compat_ioctl = ocfs2_compat_ioctl,
  2065. #endif
  2066. .lock = ocfs2_lock,
  2067. .flock = ocfs2_flock,
  2068. };
  2069. /*
  2070. * POSIX-lockless variants of our file_operations.
  2071. *
  2072. * These will be used if the underlying cluster stack does not support
  2073. * posix file locking, if the user passes the "localflocks" mount
  2074. * option, or if we have a local-only fs.
  2075. *
  2076. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2077. * so we still want it in the case of no stack support for
  2078. * plocks. Internally, it will do the right thing when asked to ignore
  2079. * the cluster.
  2080. */
  2081. const struct file_operations ocfs2_fops_no_plocks = {
  2082. .llseek = generic_file_llseek,
  2083. .read = do_sync_read,
  2084. .write = do_sync_write,
  2085. .mmap = ocfs2_mmap,
  2086. .fsync = ocfs2_sync_file,
  2087. .release = ocfs2_file_release,
  2088. .open = ocfs2_file_open,
  2089. .aio_read = ocfs2_file_aio_read,
  2090. .aio_write = ocfs2_file_aio_write,
  2091. .unlocked_ioctl = ocfs2_ioctl,
  2092. #ifdef CONFIG_COMPAT
  2093. .compat_ioctl = ocfs2_compat_ioctl,
  2094. #endif
  2095. .flock = ocfs2_flock,
  2096. .splice_read = ocfs2_file_splice_read,
  2097. .splice_write = ocfs2_file_splice_write,
  2098. };
  2099. const struct file_operations ocfs2_dops_no_plocks = {
  2100. .llseek = generic_file_llseek,
  2101. .read = generic_read_dir,
  2102. .readdir = ocfs2_readdir,
  2103. .fsync = ocfs2_sync_file,
  2104. .release = ocfs2_dir_release,
  2105. .open = ocfs2_dir_open,
  2106. .unlocked_ioctl = ocfs2_ioctl,
  2107. #ifdef CONFIG_COMPAT
  2108. .compat_ioctl = ocfs2_compat_ioctl,
  2109. #endif
  2110. .flock = ocfs2_flock,
  2111. };