ops_inode.c 27 KB

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