file.c 53 KB

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