ops_inode.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/swap.h>
  21. #include <linux/falloc.h>
  22. #include <asm/uaccess.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "xattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. /**
  38. * gfs2_create - Create a file
  39. * @dir: The directory in which to create the file
  40. * @dentry: The dentry of the new file
  41. * @mode: The mode of the new file
  42. *
  43. * Returns: errno
  44. */
  45. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  46. int mode, struct nameidata *nd)
  47. {
  48. struct gfs2_inode *dip = GFS2_I(dir);
  49. struct gfs2_sbd *sdp = GFS2_SB(dir);
  50. struct gfs2_holder ghs[2];
  51. struct inode *inode;
  52. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  53. for (;;) {
  54. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  55. if (!IS_ERR(inode)) {
  56. gfs2_trans_end(sdp);
  57. if (dip->i_alloc->al_rgd)
  58. gfs2_inplace_release(dip);
  59. gfs2_quota_unlock(dip);
  60. gfs2_alloc_put(dip);
  61. gfs2_glock_dq_uninit_m(2, ghs);
  62. mark_inode_dirty(inode);
  63. break;
  64. } else if (PTR_ERR(inode) != -EEXIST ||
  65. (nd && nd->flags & LOOKUP_EXCL)) {
  66. gfs2_holder_uninit(ghs);
  67. return PTR_ERR(inode);
  68. }
  69. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  70. if (inode) {
  71. if (!IS_ERR(inode)) {
  72. gfs2_holder_uninit(ghs);
  73. break;
  74. } else {
  75. gfs2_holder_uninit(ghs);
  76. return PTR_ERR(inode);
  77. }
  78. }
  79. }
  80. d_instantiate(dentry, inode);
  81. return 0;
  82. }
  83. /**
  84. * gfs2_lookup - Look up a filename in a directory and return its inode
  85. * @dir: The directory inode
  86. * @dentry: The dentry of the new inode
  87. * @nd: passed from Linux VFS, ignored by us
  88. *
  89. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  90. *
  91. * Returns: errno
  92. */
  93. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  94. struct nameidata *nd)
  95. {
  96. struct inode *inode = NULL;
  97. d_set_d_op(dentry, &gfs2_dops);
  98. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  99. if (inode && IS_ERR(inode))
  100. return ERR_CAST(inode);
  101. if (inode) {
  102. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  103. struct gfs2_holder gh;
  104. int error;
  105. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  106. if (error) {
  107. iput(inode);
  108. return ERR_PTR(error);
  109. }
  110. gfs2_glock_dq_uninit(&gh);
  111. return d_splice_alias(inode, dentry);
  112. }
  113. d_add(dentry, inode);
  114. return NULL;
  115. }
  116. /**
  117. * gfs2_link - Link to a file
  118. * @old_dentry: The inode to link
  119. * @dir: Add link to this directory
  120. * @dentry: The name of the link
  121. *
  122. * Link the inode in "old_dentry" into the directory "dir" with the
  123. * name in "dentry".
  124. *
  125. * Returns: errno
  126. */
  127. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  128. struct dentry *dentry)
  129. {
  130. struct gfs2_inode *dip = GFS2_I(dir);
  131. struct gfs2_sbd *sdp = GFS2_SB(dir);
  132. struct inode *inode = old_dentry->d_inode;
  133. struct gfs2_inode *ip = GFS2_I(inode);
  134. struct gfs2_holder ghs[2];
  135. int alloc_required;
  136. int error;
  137. if (S_ISDIR(inode->i_mode))
  138. return -EPERM;
  139. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  140. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  141. error = gfs2_glock_nq(ghs); /* parent */
  142. if (error)
  143. goto out_parent;
  144. error = gfs2_glock_nq(ghs + 1); /* child */
  145. if (error)
  146. goto out_child;
  147. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
  148. if (error)
  149. goto out_gunlock;
  150. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  151. switch (error) {
  152. case -ENOENT:
  153. break;
  154. case 0:
  155. error = -EEXIST;
  156. default:
  157. goto out_gunlock;
  158. }
  159. error = -EINVAL;
  160. if (!dip->i_inode.i_nlink)
  161. goto out_gunlock;
  162. error = -EFBIG;
  163. if (dip->i_entries == (u32)-1)
  164. goto out_gunlock;
  165. error = -EPERM;
  166. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  167. goto out_gunlock;
  168. error = -EINVAL;
  169. if (!ip->i_inode.i_nlink)
  170. goto out_gunlock;
  171. error = -EMLINK;
  172. if (ip->i_inode.i_nlink == (u32)-1)
  173. goto out_gunlock;
  174. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  175. if (error < 0)
  176. goto out_gunlock;
  177. error = 0;
  178. if (alloc_required) {
  179. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  180. if (!al) {
  181. error = -ENOMEM;
  182. goto out_gunlock;
  183. }
  184. error = gfs2_quota_lock_check(dip);
  185. if (error)
  186. goto out_alloc;
  187. al->al_requested = sdp->sd_max_dirres;
  188. error = gfs2_inplace_reserve(dip);
  189. if (error)
  190. goto out_gunlock_q;
  191. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  192. gfs2_rg_blocks(al) +
  193. 2 * RES_DINODE + RES_STATFS +
  194. RES_QUOTA, 0);
  195. if (error)
  196. goto out_ipres;
  197. } else {
  198. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  199. if (error)
  200. goto out_ipres;
  201. }
  202. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  203. if (error)
  204. goto out_end_trans;
  205. error = gfs2_change_nlink(ip, +1);
  206. out_end_trans:
  207. gfs2_trans_end(sdp);
  208. out_ipres:
  209. if (alloc_required)
  210. gfs2_inplace_release(dip);
  211. out_gunlock_q:
  212. if (alloc_required)
  213. gfs2_quota_unlock(dip);
  214. out_alloc:
  215. if (alloc_required)
  216. gfs2_alloc_put(dip);
  217. out_gunlock:
  218. gfs2_glock_dq(ghs + 1);
  219. out_child:
  220. gfs2_glock_dq(ghs);
  221. out_parent:
  222. gfs2_holder_uninit(ghs);
  223. gfs2_holder_uninit(ghs + 1);
  224. if (!error) {
  225. ihold(inode);
  226. d_instantiate(dentry, inode);
  227. mark_inode_dirty(inode);
  228. }
  229. return error;
  230. }
  231. /*
  232. * gfs2_unlink_ok - check to see that a inode is still in a directory
  233. * @dip: the directory
  234. * @name: the name of the file
  235. * @ip: the inode
  236. *
  237. * Assumes that the lock on (at least) @dip is held.
  238. *
  239. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  240. */
  241. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  242. const struct gfs2_inode *ip)
  243. {
  244. int error;
  245. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  246. return -EPERM;
  247. if ((dip->i_inode.i_mode & S_ISVTX) &&
  248. dip->i_inode.i_uid != current_fsuid() &&
  249. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  250. return -EPERM;
  251. if (IS_APPEND(&dip->i_inode))
  252. return -EPERM;
  253. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  254. if (error)
  255. return error;
  256. error = gfs2_dir_check(&dip->i_inode, name, ip);
  257. if (error)
  258. return error;
  259. return 0;
  260. }
  261. /**
  262. * gfs2_unlink - Unlink a file
  263. * @dir: The inode of the directory containing the file to unlink
  264. * @dentry: The file itself
  265. *
  266. * Unlink a file. Call gfs2_unlinki()
  267. *
  268. * Returns: errno
  269. */
  270. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  271. {
  272. struct gfs2_inode *dip = GFS2_I(dir);
  273. struct gfs2_sbd *sdp = GFS2_SB(dir);
  274. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  275. struct gfs2_holder ghs[3];
  276. struct gfs2_rgrpd *rgd;
  277. struct gfs2_holder ri_gh;
  278. int error;
  279. error = gfs2_rindex_hold(sdp, &ri_gh);
  280. if (error)
  281. return error;
  282. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  283. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  284. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  285. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  286. error = gfs2_glock_nq(ghs); /* parent */
  287. if (error)
  288. goto out_parent;
  289. error = gfs2_glock_nq(ghs + 1); /* child */
  290. if (error)
  291. goto out_child;
  292. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  293. if (error)
  294. goto out_rgrp;
  295. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  296. if (error)
  297. goto out_gunlock;
  298. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  299. if (error)
  300. goto out_gunlock;
  301. error = gfs2_dir_del(dip, &dentry->d_name);
  302. if (error)
  303. goto out_end_trans;
  304. error = gfs2_change_nlink(ip, -1);
  305. out_end_trans:
  306. gfs2_trans_end(sdp);
  307. out_gunlock:
  308. gfs2_glock_dq(ghs + 2);
  309. out_rgrp:
  310. gfs2_holder_uninit(ghs + 2);
  311. gfs2_glock_dq(ghs + 1);
  312. out_child:
  313. gfs2_holder_uninit(ghs + 1);
  314. gfs2_glock_dq(ghs);
  315. out_parent:
  316. gfs2_holder_uninit(ghs);
  317. gfs2_glock_dq_uninit(&ri_gh);
  318. return error;
  319. }
  320. /**
  321. * gfs2_symlink - Create a symlink
  322. * @dir: The directory to create the symlink in
  323. * @dentry: The dentry to put the symlink in
  324. * @symname: The thing which the link points to
  325. *
  326. * Returns: errno
  327. */
  328. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  329. const char *symname)
  330. {
  331. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  332. struct gfs2_sbd *sdp = GFS2_SB(dir);
  333. struct gfs2_holder ghs[2];
  334. struct inode *inode;
  335. struct buffer_head *dibh;
  336. int size;
  337. int error;
  338. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  339. size = strlen(symname);
  340. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  341. return -ENAMETOOLONG;
  342. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  343. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  344. if (IS_ERR(inode)) {
  345. gfs2_holder_uninit(ghs);
  346. return PTR_ERR(inode);
  347. }
  348. ip = ghs[1].gh_gl->gl_object;
  349. i_size_write(inode, size);
  350. error = gfs2_meta_inode_buffer(ip, &dibh);
  351. if (!gfs2_assert_withdraw(sdp, !error)) {
  352. gfs2_dinode_out(ip, dibh->b_data);
  353. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  354. size);
  355. brelse(dibh);
  356. }
  357. gfs2_trans_end(sdp);
  358. if (dip->i_alloc->al_rgd)
  359. gfs2_inplace_release(dip);
  360. gfs2_quota_unlock(dip);
  361. gfs2_alloc_put(dip);
  362. gfs2_glock_dq_uninit_m(2, ghs);
  363. d_instantiate(dentry, inode);
  364. mark_inode_dirty(inode);
  365. return 0;
  366. }
  367. /**
  368. * gfs2_mkdir - Make a directory
  369. * @dir: The parent directory of the new one
  370. * @dentry: The dentry of the new directory
  371. * @mode: The mode of the new directory
  372. *
  373. * Returns: errno
  374. */
  375. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  376. {
  377. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  378. struct gfs2_sbd *sdp = GFS2_SB(dir);
  379. struct gfs2_holder ghs[2];
  380. struct inode *inode;
  381. struct buffer_head *dibh;
  382. int error;
  383. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  384. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  385. if (IS_ERR(inode)) {
  386. gfs2_holder_uninit(ghs);
  387. return PTR_ERR(inode);
  388. }
  389. ip = ghs[1].gh_gl->gl_object;
  390. ip->i_inode.i_nlink = 2;
  391. i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  392. ip->i_diskflags |= GFS2_DIF_JDATA;
  393. ip->i_entries = 2;
  394. error = gfs2_meta_inode_buffer(ip, &dibh);
  395. if (!gfs2_assert_withdraw(sdp, !error)) {
  396. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  397. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  398. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  399. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  400. dent->de_inum = di->di_num; /* already GFS2 endian */
  401. dent->de_type = cpu_to_be16(DT_DIR);
  402. di->di_entries = cpu_to_be32(1);
  403. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  404. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  405. gfs2_inum_out(dip, dent);
  406. dent->de_type = cpu_to_be16(DT_DIR);
  407. gfs2_dinode_out(ip, di);
  408. brelse(dibh);
  409. }
  410. error = gfs2_change_nlink(dip, +1);
  411. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  412. gfs2_trans_end(sdp);
  413. if (dip->i_alloc->al_rgd)
  414. gfs2_inplace_release(dip);
  415. gfs2_quota_unlock(dip);
  416. gfs2_alloc_put(dip);
  417. gfs2_glock_dq_uninit_m(2, ghs);
  418. d_instantiate(dentry, inode);
  419. mark_inode_dirty(inode);
  420. return 0;
  421. }
  422. /**
  423. * gfs2_rmdiri - Remove a directory
  424. * @dip: The parent directory of the directory to be removed
  425. * @name: The name of the directory to be removed
  426. * @ip: The GFS2 inode of the directory to be removed
  427. *
  428. * Assumes Glocks on dip and ip are held
  429. *
  430. * Returns: errno
  431. */
  432. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  433. struct gfs2_inode *ip)
  434. {
  435. int error;
  436. if (ip->i_entries != 2) {
  437. if (gfs2_consist_inode(ip))
  438. gfs2_dinode_print(ip);
  439. return -EIO;
  440. }
  441. error = gfs2_dir_del(dip, name);
  442. if (error)
  443. return error;
  444. error = gfs2_change_nlink(dip, -1);
  445. if (error)
  446. return error;
  447. error = gfs2_dir_del(ip, &gfs2_qdot);
  448. if (error)
  449. return error;
  450. error = gfs2_dir_del(ip, &gfs2_qdotdot);
  451. if (error)
  452. return error;
  453. /* It looks odd, but it really should be done twice */
  454. error = gfs2_change_nlink(ip, -1);
  455. if (error)
  456. return error;
  457. error = gfs2_change_nlink(ip, -1);
  458. if (error)
  459. return error;
  460. return error;
  461. }
  462. /**
  463. * gfs2_rmdir - Remove a directory
  464. * @dir: The parent directory of the directory to be removed
  465. * @dentry: The dentry of the directory to remove
  466. *
  467. * Remove a directory. Call gfs2_rmdiri()
  468. *
  469. * Returns: errno
  470. */
  471. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  472. {
  473. struct gfs2_inode *dip = GFS2_I(dir);
  474. struct gfs2_sbd *sdp = GFS2_SB(dir);
  475. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  476. struct gfs2_holder ghs[3];
  477. struct gfs2_rgrpd *rgd;
  478. struct gfs2_holder ri_gh;
  479. int error;
  480. error = gfs2_rindex_hold(sdp, &ri_gh);
  481. if (error)
  482. return error;
  483. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  484. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  485. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  486. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  487. error = gfs2_glock_nq(ghs); /* parent */
  488. if (error)
  489. goto out_parent;
  490. error = gfs2_glock_nq(ghs + 1); /* child */
  491. if (error)
  492. goto out_child;
  493. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  494. if (error)
  495. goto out_rgrp;
  496. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  497. if (error)
  498. goto out_gunlock;
  499. if (ip->i_entries < 2) {
  500. if (gfs2_consist_inode(ip))
  501. gfs2_dinode_print(ip);
  502. error = -EIO;
  503. goto out_gunlock;
  504. }
  505. if (ip->i_entries > 2) {
  506. error = -ENOTEMPTY;
  507. goto out_gunlock;
  508. }
  509. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  510. if (error)
  511. goto out_gunlock;
  512. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  513. gfs2_trans_end(sdp);
  514. out_gunlock:
  515. gfs2_glock_dq(ghs + 2);
  516. out_rgrp:
  517. gfs2_holder_uninit(ghs + 2);
  518. gfs2_glock_dq(ghs + 1);
  519. out_child:
  520. gfs2_holder_uninit(ghs + 1);
  521. gfs2_glock_dq(ghs);
  522. out_parent:
  523. gfs2_holder_uninit(ghs);
  524. gfs2_glock_dq_uninit(&ri_gh);
  525. return error;
  526. }
  527. /**
  528. * gfs2_mknod - Make a special file
  529. * @dir: The directory in which the special file will reside
  530. * @dentry: The dentry of the special file
  531. * @mode: The mode of the special file
  532. * @rdev: The device specification of the special file
  533. *
  534. */
  535. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  536. dev_t dev)
  537. {
  538. struct gfs2_inode *dip = GFS2_I(dir);
  539. struct gfs2_sbd *sdp = GFS2_SB(dir);
  540. struct gfs2_holder ghs[2];
  541. struct inode *inode;
  542. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  543. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  544. if (IS_ERR(inode)) {
  545. gfs2_holder_uninit(ghs);
  546. return PTR_ERR(inode);
  547. }
  548. gfs2_trans_end(sdp);
  549. if (dip->i_alloc->al_rgd)
  550. gfs2_inplace_release(dip);
  551. gfs2_quota_unlock(dip);
  552. gfs2_alloc_put(dip);
  553. gfs2_glock_dq_uninit_m(2, ghs);
  554. d_instantiate(dentry, inode);
  555. mark_inode_dirty(inode);
  556. return 0;
  557. }
  558. /*
  559. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  560. * @this: move this
  561. * @to: to here
  562. *
  563. * Follow @to back to the root and make sure we don't encounter @this
  564. * Assumes we already hold the rename lock.
  565. *
  566. * Returns: errno
  567. */
  568. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  569. {
  570. struct inode *dir = &to->i_inode;
  571. struct super_block *sb = dir->i_sb;
  572. struct inode *tmp;
  573. int error = 0;
  574. igrab(dir);
  575. for (;;) {
  576. if (dir == &this->i_inode) {
  577. error = -EINVAL;
  578. break;
  579. }
  580. if (dir == sb->s_root->d_inode) {
  581. error = 0;
  582. break;
  583. }
  584. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  585. if (IS_ERR(tmp)) {
  586. error = PTR_ERR(tmp);
  587. break;
  588. }
  589. iput(dir);
  590. dir = tmp;
  591. }
  592. iput(dir);
  593. return error;
  594. }
  595. /**
  596. * gfs2_rename - Rename a file
  597. * @odir: Parent directory of old file name
  598. * @odentry: The old dentry of the file
  599. * @ndir: Parent directory of new file name
  600. * @ndentry: The new dentry of the file
  601. *
  602. * Returns: errno
  603. */
  604. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  605. struct inode *ndir, struct dentry *ndentry)
  606. {
  607. struct gfs2_inode *odip = GFS2_I(odir);
  608. struct gfs2_inode *ndip = GFS2_I(ndir);
  609. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  610. struct gfs2_inode *nip = NULL;
  611. struct gfs2_sbd *sdp = GFS2_SB(odir);
  612. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  613. struct gfs2_rgrpd *nrgd;
  614. unsigned int num_gh;
  615. int dir_rename = 0;
  616. int alloc_required = 0;
  617. unsigned int x;
  618. int error;
  619. if (ndentry->d_inode) {
  620. nip = GFS2_I(ndentry->d_inode);
  621. if (ip == nip)
  622. return 0;
  623. }
  624. error = gfs2_rindex_hold(sdp, &ri_gh);
  625. if (error)
  626. return error;
  627. if (odip != ndip) {
  628. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  629. 0, &r_gh);
  630. if (error)
  631. goto out;
  632. if (S_ISDIR(ip->i_inode.i_mode)) {
  633. dir_rename = 1;
  634. /* don't move a dirctory into it's subdir */
  635. error = gfs2_ok_to_move(ip, ndip);
  636. if (error)
  637. goto out_gunlock_r;
  638. }
  639. }
  640. num_gh = 1;
  641. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  642. if (odip != ndip) {
  643. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  644. num_gh++;
  645. }
  646. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  647. num_gh++;
  648. if (nip) {
  649. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  650. num_gh++;
  651. /* grab the resource lock for unlink flag twiddling
  652. * this is the case of the target file already existing
  653. * so we unlink before doing the rename
  654. */
  655. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  656. if (nrgd)
  657. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  658. }
  659. for (x = 0; x < num_gh; x++) {
  660. error = gfs2_glock_nq(ghs + x);
  661. if (error)
  662. goto out_gunlock;
  663. }
  664. /* Check out the old directory */
  665. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  666. if (error)
  667. goto out_gunlock;
  668. /* Check out the new directory */
  669. if (nip) {
  670. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  671. if (error)
  672. goto out_gunlock;
  673. if (S_ISDIR(nip->i_inode.i_mode)) {
  674. if (nip->i_entries < 2) {
  675. if (gfs2_consist_inode(nip))
  676. gfs2_dinode_print(nip);
  677. error = -EIO;
  678. goto out_gunlock;
  679. }
  680. if (nip->i_entries > 2) {
  681. error = -ENOTEMPTY;
  682. goto out_gunlock;
  683. }
  684. }
  685. } else {
  686. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
  687. if (error)
  688. goto out_gunlock;
  689. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  690. switch (error) {
  691. case -ENOENT:
  692. error = 0;
  693. break;
  694. case 0:
  695. error = -EEXIST;
  696. default:
  697. goto out_gunlock;
  698. };
  699. if (odip != ndip) {
  700. if (!ndip->i_inode.i_nlink) {
  701. error = -EINVAL;
  702. goto out_gunlock;
  703. }
  704. if (ndip->i_entries == (u32)-1) {
  705. error = -EFBIG;
  706. goto out_gunlock;
  707. }
  708. if (S_ISDIR(ip->i_inode.i_mode) &&
  709. ndip->i_inode.i_nlink == (u32)-1) {
  710. error = -EMLINK;
  711. goto out_gunlock;
  712. }
  713. }
  714. }
  715. /* Check out the dir to be renamed */
  716. if (dir_rename) {
  717. error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
  718. if (error)
  719. goto out_gunlock;
  720. }
  721. if (nip == NULL)
  722. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  723. error = alloc_required;
  724. if (error < 0)
  725. goto out_gunlock;
  726. error = 0;
  727. if (alloc_required) {
  728. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  729. if (!al) {
  730. error = -ENOMEM;
  731. goto out_gunlock;
  732. }
  733. error = gfs2_quota_lock_check(ndip);
  734. if (error)
  735. goto out_alloc;
  736. al->al_requested = sdp->sd_max_dirres;
  737. error = gfs2_inplace_reserve_ri(ndip);
  738. if (error)
  739. goto out_gunlock_q;
  740. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  741. gfs2_rg_blocks(al) +
  742. 4 * RES_DINODE + 4 * RES_LEAF +
  743. RES_STATFS + RES_QUOTA + 4, 0);
  744. if (error)
  745. goto out_ipreserv;
  746. } else {
  747. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  748. 5 * RES_LEAF + 4, 0);
  749. if (error)
  750. goto out_gunlock;
  751. }
  752. /* Remove the target file, if it exists */
  753. if (nip) {
  754. if (S_ISDIR(nip->i_inode.i_mode))
  755. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  756. else {
  757. error = gfs2_dir_del(ndip, &ndentry->d_name);
  758. if (error)
  759. goto out_end_trans;
  760. error = gfs2_change_nlink(nip, -1);
  761. }
  762. if (error)
  763. goto out_end_trans;
  764. }
  765. if (dir_rename) {
  766. error = gfs2_change_nlink(ndip, +1);
  767. if (error)
  768. goto out_end_trans;
  769. error = gfs2_change_nlink(odip, -1);
  770. if (error)
  771. goto out_end_trans;
  772. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  773. if (error)
  774. goto out_end_trans;
  775. } else {
  776. struct buffer_head *dibh;
  777. error = gfs2_meta_inode_buffer(ip, &dibh);
  778. if (error)
  779. goto out_end_trans;
  780. ip->i_inode.i_ctime = CURRENT_TIME;
  781. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  782. gfs2_dinode_out(ip, dibh->b_data);
  783. brelse(dibh);
  784. }
  785. error = gfs2_dir_del(odip, &odentry->d_name);
  786. if (error)
  787. goto out_end_trans;
  788. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  789. if (error)
  790. goto out_end_trans;
  791. out_end_trans:
  792. gfs2_trans_end(sdp);
  793. out_ipreserv:
  794. if (alloc_required)
  795. gfs2_inplace_release(ndip);
  796. out_gunlock_q:
  797. if (alloc_required)
  798. gfs2_quota_unlock(ndip);
  799. out_alloc:
  800. if (alloc_required)
  801. gfs2_alloc_put(ndip);
  802. out_gunlock:
  803. while (x--) {
  804. gfs2_glock_dq(ghs + x);
  805. gfs2_holder_uninit(ghs + x);
  806. }
  807. out_gunlock_r:
  808. if (r_gh.gh_gl)
  809. gfs2_glock_dq_uninit(&r_gh);
  810. out:
  811. gfs2_glock_dq_uninit(&ri_gh);
  812. return error;
  813. }
  814. /**
  815. * gfs2_follow_link - Follow a symbolic link
  816. * @dentry: The dentry of the link
  817. * @nd: Data that we pass to vfs_follow_link()
  818. *
  819. * This can handle symlinks of any size.
  820. *
  821. * Returns: 0 on success or error code
  822. */
  823. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  824. {
  825. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  826. struct gfs2_holder i_gh;
  827. struct buffer_head *dibh;
  828. unsigned int x, size;
  829. char *buf;
  830. int error;
  831. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  832. error = gfs2_glock_nq(&i_gh);
  833. if (error) {
  834. gfs2_holder_uninit(&i_gh);
  835. nd_set_link(nd, ERR_PTR(error));
  836. return NULL;
  837. }
  838. size = (unsigned int)i_size_read(&ip->i_inode);
  839. if (size == 0) {
  840. gfs2_consist_inode(ip);
  841. buf = ERR_PTR(-EIO);
  842. goto out;
  843. }
  844. error = gfs2_meta_inode_buffer(ip, &dibh);
  845. if (error) {
  846. buf = ERR_PTR(error);
  847. goto out;
  848. }
  849. x = size + 1;
  850. buf = kmalloc(x, GFP_NOFS);
  851. if (!buf)
  852. buf = ERR_PTR(-ENOMEM);
  853. else
  854. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  855. brelse(dibh);
  856. out:
  857. gfs2_glock_dq_uninit(&i_gh);
  858. nd_set_link(nd, buf);
  859. return NULL;
  860. }
  861. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  862. {
  863. char *s = nd_get_link(nd);
  864. if (!IS_ERR(s))
  865. kfree(s);
  866. }
  867. /**
  868. * gfs2_permission -
  869. * @inode:
  870. * @mask:
  871. * @nd: passed from Linux VFS, ignored by us
  872. *
  873. * This may be called from the VFS directly, or from within GFS2 with the
  874. * inode locked, so we look to see if the glock is already locked and only
  875. * lock the glock if its not already been done.
  876. *
  877. * Returns: errno
  878. */
  879. int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
  880. {
  881. struct gfs2_inode *ip;
  882. struct gfs2_holder i_gh;
  883. int error;
  884. int unlock = 0;
  885. if (flags & IPERM_FLAG_RCU)
  886. return -ECHILD;
  887. ip = GFS2_I(inode);
  888. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  889. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  890. if (error)
  891. return error;
  892. unlock = 1;
  893. }
  894. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  895. error = -EACCES;
  896. else
  897. error = generic_permission(inode, mask, flags, gfs2_check_acl);
  898. if (unlock)
  899. gfs2_glock_dq_uninit(&i_gh);
  900. return error;
  901. }
  902. static int setattr_chown(struct inode *inode, struct iattr *attr)
  903. {
  904. struct gfs2_inode *ip = GFS2_I(inode);
  905. struct gfs2_sbd *sdp = GFS2_SB(inode);
  906. struct buffer_head *dibh;
  907. u32 ouid, ogid, nuid, ngid;
  908. int error;
  909. ouid = inode->i_uid;
  910. ogid = inode->i_gid;
  911. nuid = attr->ia_uid;
  912. ngid = attr->ia_gid;
  913. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  914. ouid = nuid = NO_QUOTA_CHANGE;
  915. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  916. ogid = ngid = NO_QUOTA_CHANGE;
  917. if (!gfs2_alloc_get(ip))
  918. return -ENOMEM;
  919. error = gfs2_quota_lock(ip, nuid, ngid);
  920. if (error)
  921. goto out_alloc;
  922. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  923. error = gfs2_quota_check(ip, nuid, ngid);
  924. if (error)
  925. goto out_gunlock_q;
  926. }
  927. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  928. if (error)
  929. goto out_gunlock_q;
  930. error = gfs2_meta_inode_buffer(ip, &dibh);
  931. if (error)
  932. goto out_end_trans;
  933. if ((attr->ia_valid & ATTR_SIZE) &&
  934. attr->ia_size != i_size_read(inode)) {
  935. int error;
  936. error = vmtruncate(inode, attr->ia_size);
  937. gfs2_assert_warn(sdp, !error);
  938. }
  939. setattr_copy(inode, attr);
  940. mark_inode_dirty(inode);
  941. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  942. gfs2_dinode_out(ip, dibh->b_data);
  943. brelse(dibh);
  944. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  945. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  946. gfs2_quota_change(ip, -blocks, ouid, ogid);
  947. gfs2_quota_change(ip, blocks, nuid, ngid);
  948. }
  949. out_end_trans:
  950. gfs2_trans_end(sdp);
  951. out_gunlock_q:
  952. gfs2_quota_unlock(ip);
  953. out_alloc:
  954. gfs2_alloc_put(ip);
  955. return error;
  956. }
  957. /**
  958. * gfs2_setattr - Change attributes on an inode
  959. * @dentry: The dentry which is changing
  960. * @attr: The structure describing the change
  961. *
  962. * The VFS layer wants to change one or more of an inodes attributes. Write
  963. * that change out to disk.
  964. *
  965. * Returns: errno
  966. */
  967. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  968. {
  969. struct inode *inode = dentry->d_inode;
  970. struct gfs2_inode *ip = GFS2_I(inode);
  971. struct gfs2_holder i_gh;
  972. int error;
  973. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  974. if (error)
  975. return error;
  976. error = -EPERM;
  977. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  978. goto out;
  979. error = inode_change_ok(inode, attr);
  980. if (error)
  981. goto out;
  982. if (attr->ia_valid & ATTR_SIZE)
  983. error = gfs2_setattr_size(inode, attr->ia_size);
  984. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  985. error = setattr_chown(inode, attr);
  986. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  987. error = gfs2_acl_chmod(ip, attr);
  988. else
  989. error = gfs2_setattr_simple(ip, attr);
  990. out:
  991. gfs2_glock_dq_uninit(&i_gh);
  992. if (!error)
  993. mark_inode_dirty(inode);
  994. return error;
  995. }
  996. /**
  997. * gfs2_getattr - Read out an inode's attributes
  998. * @mnt: The vfsmount the inode is being accessed from
  999. * @dentry: The dentry to stat
  1000. * @stat: The inode's stats
  1001. *
  1002. * This may be called from the VFS directly, or from within GFS2 with the
  1003. * inode locked, so we look to see if the glock is already locked and only
  1004. * lock the glock if its not already been done. Note that its the NFS
  1005. * readdirplus operation which causes this to be called (from filldir)
  1006. * with the glock already held.
  1007. *
  1008. * Returns: errno
  1009. */
  1010. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1011. struct kstat *stat)
  1012. {
  1013. struct inode *inode = dentry->d_inode;
  1014. struct gfs2_inode *ip = GFS2_I(inode);
  1015. struct gfs2_holder gh;
  1016. int error;
  1017. int unlock = 0;
  1018. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1019. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1020. if (error)
  1021. return error;
  1022. unlock = 1;
  1023. }
  1024. generic_fillattr(inode, stat);
  1025. if (unlock)
  1026. gfs2_glock_dq_uninit(&gh);
  1027. return 0;
  1028. }
  1029. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1030. const void *data, size_t size, int flags)
  1031. {
  1032. struct inode *inode = dentry->d_inode;
  1033. struct gfs2_inode *ip = GFS2_I(inode);
  1034. struct gfs2_holder gh;
  1035. int ret;
  1036. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1037. ret = gfs2_glock_nq(&gh);
  1038. if (ret == 0) {
  1039. ret = generic_setxattr(dentry, name, data, size, flags);
  1040. gfs2_glock_dq(&gh);
  1041. }
  1042. gfs2_holder_uninit(&gh);
  1043. return ret;
  1044. }
  1045. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1046. void *data, size_t size)
  1047. {
  1048. struct inode *inode = dentry->d_inode;
  1049. struct gfs2_inode *ip = GFS2_I(inode);
  1050. struct gfs2_holder gh;
  1051. int ret;
  1052. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1053. ret = gfs2_glock_nq(&gh);
  1054. if (ret == 0) {
  1055. ret = generic_getxattr(dentry, name, data, size);
  1056. gfs2_glock_dq(&gh);
  1057. }
  1058. gfs2_holder_uninit(&gh);
  1059. return ret;
  1060. }
  1061. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1062. {
  1063. struct inode *inode = dentry->d_inode;
  1064. struct gfs2_inode *ip = GFS2_I(inode);
  1065. struct gfs2_holder gh;
  1066. int ret;
  1067. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1068. ret = gfs2_glock_nq(&gh);
  1069. if (ret == 0) {
  1070. ret = generic_removexattr(dentry, name);
  1071. gfs2_glock_dq(&gh);
  1072. }
  1073. gfs2_holder_uninit(&gh);
  1074. return ret;
  1075. }
  1076. static void empty_write_end(struct page *page, unsigned from,
  1077. unsigned to)
  1078. {
  1079. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  1080. page_zero_new_buffers(page, from, to);
  1081. flush_dcache_page(page);
  1082. mark_page_accessed(page);
  1083. if (!gfs2_is_writeback(ip))
  1084. gfs2_page_add_databufs(ip, page, from, to);
  1085. block_commit_write(page, from, to);
  1086. }
  1087. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  1088. {
  1089. unsigned start, end, next;
  1090. struct buffer_head *bh, *head;
  1091. int error;
  1092. if (!page_has_buffers(page)) {
  1093. error = __block_write_begin(page, from, to - from, gfs2_block_map);
  1094. if (unlikely(error))
  1095. return error;
  1096. empty_write_end(page, from, to);
  1097. return 0;
  1098. }
  1099. bh = head = page_buffers(page);
  1100. next = end = 0;
  1101. while (next < from) {
  1102. next += bh->b_size;
  1103. bh = bh->b_this_page;
  1104. }
  1105. start = next;
  1106. do {
  1107. next += bh->b_size;
  1108. if (buffer_mapped(bh)) {
  1109. if (end) {
  1110. error = __block_write_begin(page, start, end - start,
  1111. gfs2_block_map);
  1112. if (unlikely(error))
  1113. return error;
  1114. empty_write_end(page, start, end);
  1115. end = 0;
  1116. }
  1117. start = next;
  1118. }
  1119. else
  1120. end = next;
  1121. bh = bh->b_this_page;
  1122. } while (next < to);
  1123. if (end) {
  1124. error = __block_write_begin(page, start, end - start, gfs2_block_map);
  1125. if (unlikely(error))
  1126. return error;
  1127. empty_write_end(page, start, end);
  1128. }
  1129. return 0;
  1130. }
  1131. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1132. int mode)
  1133. {
  1134. struct gfs2_inode *ip = GFS2_I(inode);
  1135. struct buffer_head *dibh;
  1136. int error;
  1137. u64 start = offset >> PAGE_CACHE_SHIFT;
  1138. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  1139. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  1140. pgoff_t curr;
  1141. struct page *page;
  1142. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  1143. unsigned int from, to;
  1144. if (!end_offset)
  1145. end_offset = PAGE_CACHE_SIZE;
  1146. error = gfs2_meta_inode_buffer(ip, &dibh);
  1147. if (unlikely(error))
  1148. goto out;
  1149. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1150. if (gfs2_is_stuffed(ip)) {
  1151. error = gfs2_unstuff_dinode(ip, NULL);
  1152. if (unlikely(error))
  1153. goto out;
  1154. }
  1155. curr = start;
  1156. offset = start << PAGE_CACHE_SHIFT;
  1157. from = start_offset;
  1158. to = PAGE_CACHE_SIZE;
  1159. while (curr <= end) {
  1160. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  1161. AOP_FLAG_NOFS);
  1162. if (unlikely(!page)) {
  1163. error = -ENOMEM;
  1164. goto out;
  1165. }
  1166. if (curr == end)
  1167. to = end_offset;
  1168. error = write_empty_blocks(page, from, to);
  1169. if (!error && offset + to > inode->i_size &&
  1170. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1171. i_size_write(inode, offset + to);
  1172. }
  1173. unlock_page(page);
  1174. page_cache_release(page);
  1175. if (error)
  1176. goto out;
  1177. curr++;
  1178. offset += PAGE_CACHE_SIZE;
  1179. from = 0;
  1180. }
  1181. gfs2_dinode_out(ip, dibh->b_data);
  1182. mark_inode_dirty(inode);
  1183. brelse(dibh);
  1184. out:
  1185. return error;
  1186. }
  1187. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  1188. unsigned int *data_blocks, unsigned int *ind_blocks)
  1189. {
  1190. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1191. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  1192. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1193. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1194. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1195. max_data -= tmp;
  1196. }
  1197. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  1198. so it might end up with fewer data blocks */
  1199. if (max_data <= *data_blocks)
  1200. return;
  1201. *data_blocks = max_data;
  1202. *ind_blocks = max_blocks - max_data;
  1203. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1204. if (*len > max) {
  1205. *len = max;
  1206. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1207. }
  1208. }
  1209. static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1210. loff_t len)
  1211. {
  1212. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1213. struct gfs2_inode *ip = GFS2_I(inode);
  1214. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1215. loff_t bytes, max_bytes;
  1216. struct gfs2_alloc *al;
  1217. int error;
  1218. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1219. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1220. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  1221. sdp->sd_sb.sb_bsize_shift;
  1222. len = next - offset;
  1223. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1224. if (!bytes)
  1225. bytes = UINT_MAX;
  1226. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  1227. error = gfs2_glock_nq(&ip->i_gh);
  1228. if (unlikely(error))
  1229. goto out_uninit;
  1230. if (!gfs2_write_alloc_required(ip, offset, len))
  1231. goto out_unlock;
  1232. while (len > 0) {
  1233. if (len < bytes)
  1234. bytes = len;
  1235. al = gfs2_alloc_get(ip);
  1236. if (!al) {
  1237. error = -ENOMEM;
  1238. goto out_unlock;
  1239. }
  1240. error = gfs2_quota_lock_check(ip);
  1241. if (error)
  1242. goto out_alloc_put;
  1243. retry:
  1244. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1245. al->al_requested = data_blocks + ind_blocks;
  1246. error = gfs2_inplace_reserve(ip);
  1247. if (error) {
  1248. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  1249. bytes >>= 1;
  1250. goto retry;
  1251. }
  1252. goto out_qunlock;
  1253. }
  1254. max_bytes = bytes;
  1255. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  1256. al->al_requested = data_blocks + ind_blocks;
  1257. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1258. RES_RG_HDR + gfs2_rg_blocks(al);
  1259. if (gfs2_is_jdata(ip))
  1260. rblocks += data_blocks ? data_blocks : 1;
  1261. error = gfs2_trans_begin(sdp, rblocks,
  1262. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  1263. if (error)
  1264. goto out_trans_fail;
  1265. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1266. gfs2_trans_end(sdp);
  1267. if (error)
  1268. goto out_trans_fail;
  1269. len -= max_bytes;
  1270. offset += max_bytes;
  1271. gfs2_inplace_release(ip);
  1272. gfs2_quota_unlock(ip);
  1273. gfs2_alloc_put(ip);
  1274. }
  1275. goto out_unlock;
  1276. out_trans_fail:
  1277. gfs2_inplace_release(ip);
  1278. out_qunlock:
  1279. gfs2_quota_unlock(ip);
  1280. out_alloc_put:
  1281. gfs2_alloc_put(ip);
  1282. out_unlock:
  1283. gfs2_glock_dq(&ip->i_gh);
  1284. out_uninit:
  1285. gfs2_holder_uninit(&ip->i_gh);
  1286. return error;
  1287. }
  1288. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1289. u64 start, u64 len)
  1290. {
  1291. struct gfs2_inode *ip = GFS2_I(inode);
  1292. struct gfs2_holder gh;
  1293. int ret;
  1294. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1295. if (ret)
  1296. return ret;
  1297. mutex_lock(&inode->i_mutex);
  1298. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1299. if (ret)
  1300. goto out;
  1301. if (gfs2_is_stuffed(ip)) {
  1302. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1303. u64 size = i_size_read(inode);
  1304. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1305. FIEMAP_EXTENT_DATA_INLINE;
  1306. phys += sizeof(struct gfs2_dinode);
  1307. phys += start;
  1308. if (start + len > size)
  1309. len = size - start;
  1310. if (start < size)
  1311. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1312. len, flags);
  1313. if (ret == 1)
  1314. ret = 0;
  1315. } else {
  1316. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1317. gfs2_block_map);
  1318. }
  1319. gfs2_glock_dq_uninit(&gh);
  1320. out:
  1321. mutex_unlock(&inode->i_mutex);
  1322. return ret;
  1323. }
  1324. const struct inode_operations gfs2_file_iops = {
  1325. .permission = gfs2_permission,
  1326. .setattr = gfs2_setattr,
  1327. .getattr = gfs2_getattr,
  1328. .setxattr = gfs2_setxattr,
  1329. .getxattr = gfs2_getxattr,
  1330. .listxattr = gfs2_listxattr,
  1331. .removexattr = gfs2_removexattr,
  1332. .fallocate = gfs2_fallocate,
  1333. .fiemap = gfs2_fiemap,
  1334. };
  1335. const struct inode_operations gfs2_dir_iops = {
  1336. .create = gfs2_create,
  1337. .lookup = gfs2_lookup,
  1338. .link = gfs2_link,
  1339. .unlink = gfs2_unlink,
  1340. .symlink = gfs2_symlink,
  1341. .mkdir = gfs2_mkdir,
  1342. .rmdir = gfs2_rmdir,
  1343. .mknod = gfs2_mknod,
  1344. .rename = gfs2_rename,
  1345. .permission = gfs2_permission,
  1346. .setattr = gfs2_setattr,
  1347. .getattr = gfs2_getattr,
  1348. .setxattr = gfs2_setxattr,
  1349. .getxattr = gfs2_getxattr,
  1350. .listxattr = gfs2_listxattr,
  1351. .removexattr = gfs2_removexattr,
  1352. .fiemap = gfs2_fiemap,
  1353. };
  1354. const struct inode_operations gfs2_symlink_iops = {
  1355. .readlink = generic_readlink,
  1356. .follow_link = gfs2_follow_link,
  1357. .put_link = gfs2_put_link,
  1358. .permission = gfs2_permission,
  1359. .setattr = gfs2_setattr,
  1360. .getattr = gfs2_getattr,
  1361. .setxattr = gfs2_setxattr,
  1362. .getxattr = gfs2_getxattr,
  1363. .listxattr = gfs2_listxattr,
  1364. .removexattr = gfs2_removexattr,
  1365. .fiemap = gfs2_fiemap,
  1366. };