ops_inode.c 26 KB

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