file.c 64 KB

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