ops_inode.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/lm_interface.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "eaops.h"
  28. #include "eattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "ops_dentry.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. /**
  39. * gfs2_create - Create a file
  40. * @dir: The directory in which to create the file
  41. * @dentry: The dentry of the new file
  42. * @mode: The mode of the new file
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  47. int mode, struct nameidata *nd)
  48. {
  49. struct gfs2_inode *dip = GFS2_I(dir);
  50. struct gfs2_sbd *sdp = GFS2_SB(dir);
  51. struct gfs2_holder ghs[2];
  52. struct inode *inode;
  53. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  54. for (;;) {
  55. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  56. if (!IS_ERR(inode)) {
  57. gfs2_trans_end(sdp);
  58. if (dip->i_alloc.al_rgd)
  59. gfs2_inplace_release(dip);
  60. gfs2_quota_unlock(dip);
  61. gfs2_alloc_put(dip);
  62. gfs2_glock_dq_uninit_m(2, ghs);
  63. mark_inode_dirty(inode);
  64. break;
  65. } else if (PTR_ERR(inode) != -EEXIST ||
  66. (nd && (nd->intent.open.flags & O_EXCL))) {
  67. gfs2_holder_uninit(ghs);
  68. return PTR_ERR(inode);
  69. }
  70. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  71. if (inode) {
  72. if (!IS_ERR(inode)) {
  73. gfs2_holder_uninit(ghs);
  74. break;
  75. } else {
  76. gfs2_holder_uninit(ghs);
  77. return PTR_ERR(inode);
  78. }
  79. }
  80. }
  81. d_instantiate(dentry, inode);
  82. return 0;
  83. }
  84. /**
  85. * gfs2_lookup - Look up a filename in a directory and return its inode
  86. * @dir: The directory inode
  87. * @dentry: The dentry of the new inode
  88. * @nd: passed from Linux VFS, ignored by us
  89. *
  90. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  91. *
  92. * Returns: errno
  93. */
  94. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  95. struct nameidata *nd)
  96. {
  97. struct inode *inode = NULL;
  98. dentry->d_op = &gfs2_dops;
  99. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  100. if (inode && IS_ERR(inode))
  101. return ERR_PTR(PTR_ERR(inode));
  102. if (inode)
  103. return d_splice_alias(inode, dentry);
  104. d_add(dentry, inode);
  105. return NULL;
  106. }
  107. /**
  108. * gfs2_link - Link to a file
  109. * @old_dentry: The inode to link
  110. * @dir: Add link to this directory
  111. * @dentry: The name of the link
  112. *
  113. * Link the inode in "old_dentry" into the directory "dir" with the
  114. * name in "dentry".
  115. *
  116. * Returns: errno
  117. */
  118. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  119. struct dentry *dentry)
  120. {
  121. struct gfs2_inode *dip = GFS2_I(dir);
  122. struct gfs2_sbd *sdp = GFS2_SB(dir);
  123. struct inode *inode = old_dentry->d_inode;
  124. struct gfs2_inode *ip = GFS2_I(inode);
  125. struct gfs2_holder ghs[2];
  126. int alloc_required;
  127. int error;
  128. if (S_ISDIR(inode->i_mode))
  129. return -EPERM;
  130. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  131. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  132. error = gfs2_glock_nq_m(2, ghs);
  133. if (error)
  134. goto out;
  135. error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
  136. if (error)
  137. goto out_gunlock;
  138. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  139. switch (error) {
  140. case -ENOENT:
  141. break;
  142. case 0:
  143. error = -EEXIST;
  144. default:
  145. goto out_gunlock;
  146. }
  147. error = -EINVAL;
  148. if (!dip->i_inode.i_nlink)
  149. goto out_gunlock;
  150. error = -EFBIG;
  151. if (dip->i_di.di_entries == (u32)-1)
  152. goto out_gunlock;
  153. error = -EPERM;
  154. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  155. goto out_gunlock;
  156. error = -EINVAL;
  157. if (!ip->i_inode.i_nlink)
  158. goto out_gunlock;
  159. error = -EMLINK;
  160. if (ip->i_inode.i_nlink == (u32)-1)
  161. goto out_gunlock;
  162. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  163. if (error < 0)
  164. goto out_gunlock;
  165. error = 0;
  166. if (alloc_required) {
  167. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  168. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  169. if (error)
  170. goto out_alloc;
  171. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  172. if (error)
  173. goto out_gunlock_q;
  174. al->al_requested = sdp->sd_max_dirres;
  175. error = gfs2_inplace_reserve(dip);
  176. if (error)
  177. goto out_gunlock_q;
  178. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  179. al->al_rgd->rd_length +
  180. 2 * RES_DINODE + RES_STATFS +
  181. RES_QUOTA, 0);
  182. if (error)
  183. goto out_ipres;
  184. } else {
  185. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  186. if (error)
  187. goto out_ipres;
  188. }
  189. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  190. if (error)
  191. goto out_end_trans;
  192. error = gfs2_change_nlink(ip, +1);
  193. out_end_trans:
  194. gfs2_trans_end(sdp);
  195. out_ipres:
  196. if (alloc_required)
  197. gfs2_inplace_release(dip);
  198. out_gunlock_q:
  199. if (alloc_required)
  200. gfs2_quota_unlock(dip);
  201. out_alloc:
  202. if (alloc_required)
  203. gfs2_alloc_put(dip);
  204. out_gunlock:
  205. gfs2_glock_dq_m(2, ghs);
  206. out:
  207. gfs2_holder_uninit(ghs);
  208. gfs2_holder_uninit(ghs + 1);
  209. if (!error) {
  210. atomic_inc(&inode->i_count);
  211. d_instantiate(dentry, inode);
  212. mark_inode_dirty(inode);
  213. }
  214. return error;
  215. }
  216. /**
  217. * gfs2_unlink - Unlink a file
  218. * @dir: The inode of the directory containing the file to unlink
  219. * @dentry: The file itself
  220. *
  221. * Unlink a file. Call gfs2_unlinki()
  222. *
  223. * Returns: errno
  224. */
  225. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  226. {
  227. struct gfs2_inode *dip = GFS2_I(dir);
  228. struct gfs2_sbd *sdp = GFS2_SB(dir);
  229. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  230. struct gfs2_holder ghs[3];
  231. struct gfs2_rgrpd *rgd;
  232. struct gfs2_holder ri_gh;
  233. int error;
  234. error = gfs2_rindex_hold(sdp, &ri_gh);
  235. if (error)
  236. return error;
  237. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  238. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  239. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  240. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  241. error = gfs2_glock_nq(ghs); /* parent */
  242. if (error)
  243. goto out_parent;
  244. error = gfs2_glock_nq(ghs + 1); /* child */
  245. if (error)
  246. goto out_child;
  247. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  248. if (error)
  249. goto out_rgrp;
  250. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  251. if (error)
  252. goto out_rgrp;
  253. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  254. if (error)
  255. goto out_rgrp;
  256. error = gfs2_dir_del(dip, &dentry->d_name);
  257. if (error)
  258. goto out_end_trans;
  259. error = gfs2_change_nlink(ip, -1);
  260. out_end_trans:
  261. gfs2_trans_end(sdp);
  262. gfs2_glock_dq(ghs + 2);
  263. out_rgrp:
  264. gfs2_holder_uninit(ghs + 2);
  265. gfs2_glock_dq(ghs + 1);
  266. out_child:
  267. gfs2_holder_uninit(ghs + 1);
  268. gfs2_glock_dq(ghs);
  269. out_parent:
  270. gfs2_holder_uninit(ghs);
  271. gfs2_glock_dq_uninit(&ri_gh);
  272. return error;
  273. }
  274. /**
  275. * gfs2_symlink - Create a symlink
  276. * @dir: The directory to create the symlink in
  277. * @dentry: The dentry to put the symlink in
  278. * @symname: The thing which the link points to
  279. *
  280. * Returns: errno
  281. */
  282. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  283. const char *symname)
  284. {
  285. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  286. struct gfs2_sbd *sdp = GFS2_SB(dir);
  287. struct gfs2_holder ghs[2];
  288. struct inode *inode;
  289. struct buffer_head *dibh;
  290. int size;
  291. int error;
  292. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  293. size = strlen(symname);
  294. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  295. return -ENAMETOOLONG;
  296. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  297. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  298. if (IS_ERR(inode)) {
  299. gfs2_holder_uninit(ghs);
  300. return PTR_ERR(inode);
  301. }
  302. ip = ghs[1].gh_gl->gl_object;
  303. ip->i_di.di_size = size;
  304. error = gfs2_meta_inode_buffer(ip, &dibh);
  305. if (!gfs2_assert_withdraw(sdp, !error)) {
  306. gfs2_dinode_out(ip, dibh->b_data);
  307. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  308. size);
  309. brelse(dibh);
  310. }
  311. gfs2_trans_end(sdp);
  312. if (dip->i_alloc.al_rgd)
  313. gfs2_inplace_release(dip);
  314. gfs2_quota_unlock(dip);
  315. gfs2_alloc_put(dip);
  316. gfs2_glock_dq_uninit_m(2, ghs);
  317. d_instantiate(dentry, inode);
  318. mark_inode_dirty(inode);
  319. return 0;
  320. }
  321. /**
  322. * gfs2_mkdir - Make a directory
  323. * @dir: The parent directory of the new one
  324. * @dentry: The dentry of the new directory
  325. * @mode: The mode of the new directory
  326. *
  327. * Returns: errno
  328. */
  329. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  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 error;
  337. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  338. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  339. if (IS_ERR(inode)) {
  340. gfs2_holder_uninit(ghs);
  341. return PTR_ERR(inode);
  342. }
  343. ip = ghs[1].gh_gl->gl_object;
  344. ip->i_inode.i_nlink = 2;
  345. ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  346. ip->i_di.di_flags |= GFS2_DIF_JDATA;
  347. ip->i_di.di_entries = 2;
  348. error = gfs2_meta_inode_buffer(ip, &dibh);
  349. if (!gfs2_assert_withdraw(sdp, !error)) {
  350. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  351. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  352. struct qstr str;
  353. gfs2_str2qstr(&str, ".");
  354. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  355. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  356. dent->de_inum = di->di_num; /* already GFS2 endian */
  357. dent->de_type = cpu_to_be16(DT_DIR);
  358. di->di_entries = cpu_to_be32(1);
  359. gfs2_str2qstr(&str, "..");
  360. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  361. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  362. gfs2_inum_out(dip, dent);
  363. dent->de_type = cpu_to_be16(DT_DIR);
  364. gfs2_dinode_out(ip, di);
  365. brelse(dibh);
  366. }
  367. error = gfs2_change_nlink(dip, +1);
  368. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  369. gfs2_trans_end(sdp);
  370. if (dip->i_alloc.al_rgd)
  371. gfs2_inplace_release(dip);
  372. gfs2_quota_unlock(dip);
  373. gfs2_alloc_put(dip);
  374. gfs2_glock_dq_uninit_m(2, ghs);
  375. d_instantiate(dentry, inode);
  376. mark_inode_dirty(inode);
  377. return 0;
  378. }
  379. /**
  380. * gfs2_rmdir - Remove a directory
  381. * @dir: The parent directory of the directory to be removed
  382. * @dentry: The dentry of the directory to remove
  383. *
  384. * Remove a directory. Call gfs2_rmdiri()
  385. *
  386. * Returns: errno
  387. */
  388. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  389. {
  390. struct gfs2_inode *dip = GFS2_I(dir);
  391. struct gfs2_sbd *sdp = GFS2_SB(dir);
  392. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  393. struct gfs2_holder ghs[3];
  394. struct gfs2_rgrpd *rgd;
  395. struct gfs2_holder ri_gh;
  396. int error;
  397. error = gfs2_rindex_hold(sdp, &ri_gh);
  398. if (error)
  399. return error;
  400. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  401. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  402. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  403. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  404. error = gfs2_glock_nq_m(3, ghs);
  405. if (error)
  406. goto out;
  407. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  408. if (error)
  409. goto out_gunlock;
  410. if (ip->i_di.di_entries < 2) {
  411. if (gfs2_consist_inode(ip))
  412. gfs2_dinode_print(ip);
  413. error = -EIO;
  414. goto out_gunlock;
  415. }
  416. if (ip->i_di.di_entries > 2) {
  417. error = -ENOTEMPTY;
  418. goto out_gunlock;
  419. }
  420. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  421. if (error)
  422. goto out_gunlock;
  423. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  424. gfs2_trans_end(sdp);
  425. out_gunlock:
  426. gfs2_glock_dq_m(3, ghs);
  427. out:
  428. gfs2_holder_uninit(ghs);
  429. gfs2_holder_uninit(ghs + 1);
  430. gfs2_holder_uninit(ghs + 2);
  431. gfs2_glock_dq_uninit(&ri_gh);
  432. return error;
  433. }
  434. /**
  435. * gfs2_mknod - Make a special file
  436. * @dir: The directory in which the special file will reside
  437. * @dentry: The dentry of the special file
  438. * @mode: The mode of the special file
  439. * @rdev: The device specification of the special file
  440. *
  441. */
  442. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  443. dev_t dev)
  444. {
  445. struct gfs2_inode *dip = GFS2_I(dir);
  446. struct gfs2_sbd *sdp = GFS2_SB(dir);
  447. struct gfs2_holder ghs[2];
  448. struct inode *inode;
  449. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  450. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  451. if (IS_ERR(inode)) {
  452. gfs2_holder_uninit(ghs);
  453. return PTR_ERR(inode);
  454. }
  455. gfs2_trans_end(sdp);
  456. if (dip->i_alloc.al_rgd)
  457. gfs2_inplace_release(dip);
  458. gfs2_quota_unlock(dip);
  459. gfs2_alloc_put(dip);
  460. gfs2_glock_dq_uninit_m(2, ghs);
  461. d_instantiate(dentry, inode);
  462. mark_inode_dirty(inode);
  463. return 0;
  464. }
  465. /**
  466. * gfs2_rename - Rename a file
  467. * @odir: Parent directory of old file name
  468. * @odentry: The old dentry of the file
  469. * @ndir: Parent directory of new file name
  470. * @ndentry: The new dentry of the file
  471. *
  472. * Returns: errno
  473. */
  474. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  475. struct inode *ndir, struct dentry *ndentry)
  476. {
  477. struct gfs2_inode *odip = GFS2_I(odir);
  478. struct gfs2_inode *ndip = GFS2_I(ndir);
  479. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  480. struct gfs2_inode *nip = NULL;
  481. struct gfs2_sbd *sdp = GFS2_SB(odir);
  482. struct gfs2_holder ghs[5], r_gh;
  483. struct gfs2_rgrpd *nrgd;
  484. unsigned int num_gh;
  485. int dir_rename = 0;
  486. int alloc_required;
  487. unsigned int x;
  488. int error;
  489. if (ndentry->d_inode) {
  490. nip = GFS2_I(ndentry->d_inode);
  491. if (ip == nip)
  492. return 0;
  493. }
  494. /* Make sure we aren't trying to move a dirctory into it's subdir */
  495. if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
  496. dir_rename = 1;
  497. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
  498. &r_gh);
  499. if (error)
  500. goto out;
  501. error = gfs2_ok_to_move(ip, ndip);
  502. if (error)
  503. goto out_gunlock_r;
  504. }
  505. num_gh = 1;
  506. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  507. if (odip != ndip) {
  508. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  509. num_gh++;
  510. }
  511. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  512. num_gh++;
  513. if (nip) {
  514. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  515. num_gh++;
  516. /* grab the resource lock for unlink flag twiddling
  517. * this is the case of the target file already existing
  518. * so we unlink before doing the rename
  519. */
  520. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  521. if (nrgd)
  522. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  523. }
  524. error = gfs2_glock_nq_m(num_gh, ghs);
  525. if (error)
  526. goto out_uninit;
  527. /* Check out the old directory */
  528. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  529. if (error)
  530. goto out_gunlock;
  531. /* Check out the new directory */
  532. if (nip) {
  533. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  534. if (error)
  535. goto out_gunlock;
  536. if (S_ISDIR(nip->i_inode.i_mode)) {
  537. if (nip->i_di.di_entries < 2) {
  538. if (gfs2_consist_inode(nip))
  539. gfs2_dinode_print(nip);
  540. error = -EIO;
  541. goto out_gunlock;
  542. }
  543. if (nip->i_di.di_entries > 2) {
  544. error = -ENOTEMPTY;
  545. goto out_gunlock;
  546. }
  547. }
  548. } else {
  549. error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
  550. if (error)
  551. goto out_gunlock;
  552. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  553. switch (error) {
  554. case -ENOENT:
  555. error = 0;
  556. break;
  557. case 0:
  558. error = -EEXIST;
  559. default:
  560. goto out_gunlock;
  561. };
  562. if (odip != ndip) {
  563. if (!ndip->i_inode.i_nlink) {
  564. error = -EINVAL;
  565. goto out_gunlock;
  566. }
  567. if (ndip->i_di.di_entries == (u32)-1) {
  568. error = -EFBIG;
  569. goto out_gunlock;
  570. }
  571. if (S_ISDIR(ip->i_inode.i_mode) &&
  572. ndip->i_inode.i_nlink == (u32)-1) {
  573. error = -EMLINK;
  574. goto out_gunlock;
  575. }
  576. }
  577. }
  578. /* Check out the dir to be renamed */
  579. if (dir_rename) {
  580. error = permission(odentry->d_inode, MAY_WRITE, NULL);
  581. if (error)
  582. goto out_gunlock;
  583. }
  584. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  585. if (error < 0)
  586. goto out_gunlock;
  587. error = 0;
  588. if (alloc_required) {
  589. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  590. error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  591. if (error)
  592. goto out_alloc;
  593. error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
  594. if (error)
  595. goto out_gunlock_q;
  596. al->al_requested = sdp->sd_max_dirres;
  597. error = gfs2_inplace_reserve(ndip);
  598. if (error)
  599. goto out_gunlock_q;
  600. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  601. al->al_rgd->rd_length +
  602. 4 * RES_DINODE + 4 * RES_LEAF +
  603. RES_STATFS + RES_QUOTA + 4, 0);
  604. if (error)
  605. goto out_ipreserv;
  606. } else {
  607. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  608. 5 * RES_LEAF + 4, 0);
  609. if (error)
  610. goto out_gunlock;
  611. }
  612. /* Remove the target file, if it exists */
  613. if (nip) {
  614. if (S_ISDIR(nip->i_inode.i_mode))
  615. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  616. else {
  617. error = gfs2_dir_del(ndip, &ndentry->d_name);
  618. if (error)
  619. goto out_end_trans;
  620. error = gfs2_change_nlink(nip, -1);
  621. }
  622. if (error)
  623. goto out_end_trans;
  624. }
  625. if (dir_rename) {
  626. struct qstr name;
  627. gfs2_str2qstr(&name, "..");
  628. error = gfs2_change_nlink(ndip, +1);
  629. if (error)
  630. goto out_end_trans;
  631. error = gfs2_change_nlink(odip, -1);
  632. if (error)
  633. goto out_end_trans;
  634. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  635. if (error)
  636. goto out_end_trans;
  637. } else {
  638. struct buffer_head *dibh;
  639. error = gfs2_meta_inode_buffer(ip, &dibh);
  640. if (error)
  641. goto out_end_trans;
  642. ip->i_inode.i_ctime = CURRENT_TIME;
  643. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  644. gfs2_dinode_out(ip, dibh->b_data);
  645. brelse(dibh);
  646. }
  647. error = gfs2_dir_del(odip, &odentry->d_name);
  648. if (error)
  649. goto out_end_trans;
  650. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  651. if (error)
  652. goto out_end_trans;
  653. out_end_trans:
  654. gfs2_trans_end(sdp);
  655. out_ipreserv:
  656. if (alloc_required)
  657. gfs2_inplace_release(ndip);
  658. out_gunlock_q:
  659. if (alloc_required)
  660. gfs2_quota_unlock(ndip);
  661. out_alloc:
  662. if (alloc_required)
  663. gfs2_alloc_put(ndip);
  664. out_gunlock:
  665. gfs2_glock_dq_m(num_gh, ghs);
  666. out_uninit:
  667. for (x = 0; x < num_gh; x++)
  668. gfs2_holder_uninit(ghs + x);
  669. out_gunlock_r:
  670. if (dir_rename)
  671. gfs2_glock_dq_uninit(&r_gh);
  672. out:
  673. return error;
  674. }
  675. /**
  676. * gfs2_readlink - Read the value of a symlink
  677. * @dentry: the symlink
  678. * @buf: the buffer to read the symlink data into
  679. * @size: the size of the buffer
  680. *
  681. * Returns: errno
  682. */
  683. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  684. int user_size)
  685. {
  686. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  687. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  688. unsigned int len = GFS2_FAST_NAME_SIZE;
  689. int error;
  690. error = gfs2_readlinki(ip, &buf, &len);
  691. if (error)
  692. return error;
  693. if (user_size > len - 1)
  694. user_size = len - 1;
  695. if (copy_to_user(user_buf, buf, user_size))
  696. error = -EFAULT;
  697. else
  698. error = user_size;
  699. if (buf != array)
  700. kfree(buf);
  701. return error;
  702. }
  703. /**
  704. * gfs2_follow_link - Follow a symbolic link
  705. * @dentry: The dentry of the link
  706. * @nd: Data that we pass to vfs_follow_link()
  707. *
  708. * This can handle symlinks of any size. It is optimised for symlinks
  709. * under GFS2_FAST_NAME_SIZE.
  710. *
  711. * Returns: 0 on success or error code
  712. */
  713. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  714. {
  715. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  716. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  717. unsigned int len = GFS2_FAST_NAME_SIZE;
  718. int error;
  719. error = gfs2_readlinki(ip, &buf, &len);
  720. if (!error) {
  721. error = vfs_follow_link(nd, buf);
  722. if (buf != array)
  723. kfree(buf);
  724. }
  725. return ERR_PTR(error);
  726. }
  727. /**
  728. * gfs2_permission -
  729. * @inode:
  730. * @mask:
  731. * @nd: passed from Linux VFS, ignored by us
  732. *
  733. * This may be called from the VFS directly, or from within GFS2 with the
  734. * inode locked, so we look to see if the glock is already locked and only
  735. * lock the glock if its not already been done.
  736. *
  737. * Returns: errno
  738. */
  739. static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  740. {
  741. struct gfs2_inode *ip = GFS2_I(inode);
  742. struct gfs2_holder i_gh;
  743. int error;
  744. int unlock = 0;
  745. if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
  746. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  747. if (error)
  748. return error;
  749. unlock = 1;
  750. }
  751. error = generic_permission(inode, mask, gfs2_check_acl);
  752. if (unlock)
  753. gfs2_glock_dq_uninit(&i_gh);
  754. return error;
  755. }
  756. static int setattr_size(struct inode *inode, struct iattr *attr)
  757. {
  758. struct gfs2_inode *ip = GFS2_I(inode);
  759. struct gfs2_sbd *sdp = GFS2_SB(inode);
  760. int error;
  761. if (attr->ia_size != ip->i_di.di_size) {
  762. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  763. if (error)
  764. return error;
  765. error = vmtruncate(inode, attr->ia_size);
  766. gfs2_trans_end(sdp);
  767. if (error)
  768. return error;
  769. }
  770. error = gfs2_truncatei(ip, attr->ia_size);
  771. if (error && (inode->i_size != ip->i_di.di_size))
  772. i_size_write(inode, ip->i_di.di_size);
  773. return error;
  774. }
  775. static int setattr_chown(struct inode *inode, struct iattr *attr)
  776. {
  777. struct gfs2_inode *ip = GFS2_I(inode);
  778. struct gfs2_sbd *sdp = GFS2_SB(inode);
  779. struct buffer_head *dibh;
  780. u32 ouid, ogid, nuid, ngid;
  781. int error;
  782. ouid = inode->i_uid;
  783. ogid = inode->i_gid;
  784. nuid = attr->ia_uid;
  785. ngid = attr->ia_gid;
  786. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  787. ouid = nuid = NO_QUOTA_CHANGE;
  788. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  789. ogid = ngid = NO_QUOTA_CHANGE;
  790. gfs2_alloc_get(ip);
  791. error = gfs2_quota_lock(ip, nuid, ngid);
  792. if (error)
  793. goto out_alloc;
  794. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  795. error = gfs2_quota_check(ip, nuid, ngid);
  796. if (error)
  797. goto out_gunlock_q;
  798. }
  799. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  800. if (error)
  801. goto out_gunlock_q;
  802. error = gfs2_meta_inode_buffer(ip, &dibh);
  803. if (error)
  804. goto out_end_trans;
  805. error = inode_setattr(inode, attr);
  806. gfs2_assert_warn(sdp, !error);
  807. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  808. gfs2_dinode_out(ip, dibh->b_data);
  809. brelse(dibh);
  810. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  811. gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
  812. gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
  813. }
  814. out_end_trans:
  815. gfs2_trans_end(sdp);
  816. out_gunlock_q:
  817. gfs2_quota_unlock(ip);
  818. out_alloc:
  819. gfs2_alloc_put(ip);
  820. return error;
  821. }
  822. /**
  823. * gfs2_setattr - Change attributes on an inode
  824. * @dentry: The dentry which is changing
  825. * @attr: The structure describing the change
  826. *
  827. * The VFS layer wants to change one or more of an inodes attributes. Write
  828. * that change out to disk.
  829. *
  830. * Returns: errno
  831. */
  832. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  833. {
  834. struct inode *inode = dentry->d_inode;
  835. struct gfs2_inode *ip = GFS2_I(inode);
  836. struct gfs2_holder i_gh;
  837. int error;
  838. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  839. if (error)
  840. return error;
  841. error = -EPERM;
  842. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  843. goto out;
  844. error = inode_change_ok(inode, attr);
  845. if (error)
  846. goto out;
  847. if (attr->ia_valid & ATTR_SIZE)
  848. error = setattr_size(inode, attr);
  849. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  850. error = setattr_chown(inode, attr);
  851. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  852. error = gfs2_acl_chmod(ip, attr);
  853. else
  854. error = gfs2_setattr_simple(ip, attr);
  855. out:
  856. gfs2_glock_dq_uninit(&i_gh);
  857. if (!error)
  858. mark_inode_dirty(inode);
  859. return error;
  860. }
  861. /**
  862. * gfs2_getattr - Read out an inode's attributes
  863. * @mnt: The vfsmount the inode is being accessed from
  864. * @dentry: The dentry to stat
  865. * @stat: The inode's stats
  866. *
  867. * This may be called from the VFS directly, or from within GFS2 with the
  868. * inode locked, so we look to see if the glock is already locked and only
  869. * lock the glock if its not already been done. Note that its the NFS
  870. * readdirplus operation which causes this to be called (from filldir)
  871. * with the glock already held.
  872. *
  873. * Returns: errno
  874. */
  875. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  876. struct kstat *stat)
  877. {
  878. struct inode *inode = dentry->d_inode;
  879. struct gfs2_inode *ip = GFS2_I(inode);
  880. struct gfs2_holder gh;
  881. int error;
  882. int unlock = 0;
  883. if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
  884. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  885. if (error)
  886. return error;
  887. unlock = 1;
  888. }
  889. generic_fillattr(inode, stat);
  890. if (unlock)
  891. gfs2_glock_dq_uninit(&gh);
  892. return 0;
  893. }
  894. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  895. const void *data, size_t size, int flags)
  896. {
  897. struct inode *inode = dentry->d_inode;
  898. struct gfs2_ea_request er;
  899. memset(&er, 0, sizeof(struct gfs2_ea_request));
  900. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  901. if (er.er_type == GFS2_EATYPE_UNUSED)
  902. return -EOPNOTSUPP;
  903. er.er_data = (char *)data;
  904. er.er_name_len = strlen(er.er_name);
  905. er.er_data_len = size;
  906. er.er_flags = flags;
  907. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  908. return gfs2_ea_set(GFS2_I(inode), &er);
  909. }
  910. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  911. void *data, size_t size)
  912. {
  913. struct gfs2_ea_request er;
  914. memset(&er, 0, sizeof(struct gfs2_ea_request));
  915. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  916. if (er.er_type == GFS2_EATYPE_UNUSED)
  917. return -EOPNOTSUPP;
  918. er.er_data = data;
  919. er.er_name_len = strlen(er.er_name);
  920. er.er_data_len = size;
  921. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  922. }
  923. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  924. {
  925. struct gfs2_ea_request er;
  926. memset(&er, 0, sizeof(struct gfs2_ea_request));
  927. er.er_data = (size) ? buffer : NULL;
  928. er.er_data_len = size;
  929. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  930. }
  931. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  932. {
  933. struct gfs2_ea_request er;
  934. memset(&er, 0, sizeof(struct gfs2_ea_request));
  935. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  936. if (er.er_type == GFS2_EATYPE_UNUSED)
  937. return -EOPNOTSUPP;
  938. er.er_name_len = strlen(er.er_name);
  939. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  940. }
  941. const struct inode_operations gfs2_file_iops = {
  942. .permission = gfs2_permission,
  943. .setattr = gfs2_setattr,
  944. .getattr = gfs2_getattr,
  945. .setxattr = gfs2_setxattr,
  946. .getxattr = gfs2_getxattr,
  947. .listxattr = gfs2_listxattr,
  948. .removexattr = gfs2_removexattr,
  949. };
  950. const struct inode_operations gfs2_dev_iops = {
  951. .permission = gfs2_permission,
  952. .setattr = gfs2_setattr,
  953. .getattr = gfs2_getattr,
  954. .setxattr = gfs2_setxattr,
  955. .getxattr = gfs2_getxattr,
  956. .listxattr = gfs2_listxattr,
  957. .removexattr = gfs2_removexattr,
  958. };
  959. const struct inode_operations gfs2_dir_iops = {
  960. .create = gfs2_create,
  961. .lookup = gfs2_lookup,
  962. .link = gfs2_link,
  963. .unlink = gfs2_unlink,
  964. .symlink = gfs2_symlink,
  965. .mkdir = gfs2_mkdir,
  966. .rmdir = gfs2_rmdir,
  967. .mknod = gfs2_mknod,
  968. .rename = gfs2_rename,
  969. .permission = gfs2_permission,
  970. .setattr = gfs2_setattr,
  971. .getattr = gfs2_getattr,
  972. .setxattr = gfs2_setxattr,
  973. .getxattr = gfs2_getxattr,
  974. .listxattr = gfs2_listxattr,
  975. .removexattr = gfs2_removexattr,
  976. };
  977. const struct inode_operations gfs2_symlink_iops = {
  978. .readlink = gfs2_readlink,
  979. .follow_link = gfs2_follow_link,
  980. .permission = gfs2_permission,
  981. .setattr = gfs2_setattr,
  982. .getattr = gfs2_getattr,
  983. .setxattr = gfs2_setxattr,
  984. .getxattr = gfs2_getxattr,
  985. .listxattr = gfs2_listxattr,
  986. .removexattr = gfs2_removexattr,
  987. };