file.c 53 KB

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